Facing issue with the code sample available on azure openai

AQ 0 Reputation points
2025-11-27T04:17:53.1233333+00:00

Hello,

I have created a RAG environment (blob storage, ai search and openai). I deployed a model on openai for embedding and linked to the ai search. And when I create a new model on foundry to create an agent, I can't replicate the model used on playground in the python code.

User's image

First question, what would be this "subscription_key"? Where can I find this?

Since I replace the keys, this code has to work as per the playground? I'm using the same model configured on playground?

Thank you

Azure OpenAI in Foundry Models

3 answers

Sort by: Most helpful
  1. AQ 0 Reputation points
    2025-11-27T14:45:20.7133333+00:00

    I was able to run the request successfully using the code below, without any errors. However, the model’s responses still differ from the outputs I see in the Azure AI Playground when using the same prompt and data source.

    from openai import AzureOpenAI
    
    # Get an Azure OpenAI chat client
    chat_client = AzureOpenAI(
        api_version = "2024-12-01-preview",
        azure_endpoint = "",
        api_key = ""
    )
    
    # Initialize prompt with system message
    prompt = [
        {"role": "system", "content": "You are a helpful AI assistant."}
    ]
    
    # Add a user input message to the prompt
    input_text = input("Enter a question: ")
    prompt.append({"role": "user", "content": input_text})
    
    # Additional parameters to apply RAG pattern using the AI Search index
    rag_params = {
        "data_sources": [
            {
                "type": "azure_search",
                "parameters": {
                    "endpoint": "",
                    "index_name": "",
                    "authentication": {
                        "type": "api_key",
                        "key": "",
                    }
                }
            }
        ],
    }
    
    # Submit the prompt with the index information
    response = chat_client.chat.completions.create(
        model="",
        messages=prompt,
        extra_body=rag_params
    )
    
    # Print the contextualized response
    completion = response.choices[0].message.content
    print(completion)
    
    

    Is there any additional parameter or configuration required in the Python API to replicate the exact behavior of the Playground (e.g., strictness, document retrieval settings, or “limit responses to your data content”)?

    Was this answer helpful?


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    4 deleted comments

    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    2 deleted comments

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.