Portal returns updated results while the API returns outdated ones

Hassan Kaleem 0 Reputation points
2025-01-06T07:37:01.02+00:00

I have created a GPT4 model in Azure OpenAI Service, and I want to add data source which is Azure AI Search (Cognitive Search Service).
The problem is at the time of deployment i cannot see any data source adding option, it is deployed first and then data source is added in the portal but when we do that .NET api return old data only portal gives us updated data with source. How do i fix this problem?

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
Developer technologies | ASP.NET | ASP.NET API
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Fang 1,060 Reputation points MVP
    2025-01-06T09:34:48.87+00:00

    hi, you can try to add extra_body section in the GPT4 API call in your client side. see below example

    import os
    from openai import AzureOpenAI  
    from azure.identity import DefaultAzureCredential, get_bearer_token_provider  
      
    endpoint = os.getenv("ENDPOINT_URL", "https://xxxxx.openai.azure.com/")
    deployment = os.getenv("DEPLOYMENT_NAME", "xxxx")
      
    # Initialize Azure OpenAI client with Entra ID authentication  
    cognitiveServicesResource = os.getenv('AZURE_COGNITIVE_SERVICES_RESOURCE', 'YOUR_COGNITIVE_SERVICES_RESOURCE')  
    token_provider = get_bearer_token_provider(  
        DefaultAzureCredential(),  
        f'{cognitiveServicesResource}.default'  
    )  
            
    client = AzureOpenAI(  
        azure_endpoint=endpoint,  
        azure_ad_token_provider=token_provider,  
        api_version='2024-05-01-preview',  
    )  
           
    completion = client.chat.completions.create(  
        model=deployment,  
        messages=[
        {
            "role": "system",
            "content": "You are an AI assistant that helps people find information."
        },
        {
            "role": "user",
            "content": "hi"
        }
    ],  
        past_messages=10,
        max_tokens=800,  
        temperature=0.7,  
        top_p=0.95,  
        frequency_penalty=0,  
        presence_penalty=0,  
        stop=None,  
        extra_body={  
            "data_sources": [  
                {  
                    "type": "azure_search",  
                    "parameters": {  
                        "endpoint": os.environ["AZURE_AI_SEARCH_ENDPOINT"],  
                        "index_name": os.environ["AZURE_AI_SEARCH_INDEX"],  
                        "authentication": {  
                            "type": "system_assigned_managed_identity"  
                        }  
                    }  
                }  
            ]  
        }  
    )
    print(completion.model_dump_json(indent=2))  
    

  2. Raymond Huynh (WICLOUD CORPORATION) 620 Reputation points Microsoft External Staff
    2025-07-25T08:13:10.76+00:00

    Hi @Hassan Kaleem ,

    Have you fixed your issue? If not, you may want to try this approach, building on the suggestions from the existing answers and comments in the thread.

    1. Check Your Data Source in the Portal

    First, make sure your Azure AI Search index is up to date and properly connected to your Azure OpenAI resource in the portal. Sometimes, the portal reflects changes faster than the API due to caching or synchronization delays.

    2. Test the API Without Data Source Integration

    As mentioned in the existing comments, revert to your previous API call that does not include the data_sources or extra_body parameter. For example:

    var requestBody = new
    {
        messages = new[]
        {
            new { role = "user", content = "hi" }
        },
        max_tokens = 1000,
        temperature = 0.7
    };
    

    If this works (even if it returns outdated data), your basic API setup is correct.

    3. Add the Data Source in Your C# API Call

    Now, try adding the data source as described in the existing answers, but ensure your C# code matches the expected structure for the latest Azure OpenAI API. Here’s how you might do it in C#:

    var requestBody = new
    {
        messages = new[]
        {
            new { role = "system", content = "You are an AI assistant that helps people find information." },
            new { role = "user", content = "hi" }
        },
        max_tokens = 800,
        temperature = 0.7,
        extra_body = new
        {
            data_sources = new[]
            {
                new
                {
                    type = "azure_search",
                    parameters = new
                    {
                        endpoint = Environment.GetEnvironmentVariable("AZURE_AI_SEARCH_ENDPOINT"),
                        index_name = Environment.GetEnvironmentVariable("AZURE_AI_SEARCH_INDEX"),
                        authentication = new
                        {
                            type = "system_assigned_managed_identity"
                        }
                    }
                }
            }
        }
    };
    

    Make sure:

    • Your environment variables are set and correct.
    • The managed identity is enabled and has the right permissions on your Azure AI Search resource.
    • The API version you are using supports the extra_body parameter.

    4. If You Encounter Errors

    • Carefully review the error message for clues about misconfiguration or missing permissions.
    • Ensure your resources are in the same region and that the identity has access to the search index.
    • If system-assigned managed identity fails, try a user-assigned managed identity and update the authentication block accordingly.

    5. Sync and Wait

    If the portal shows updated data but the API does not, try re-indexing your search data and wait a few minutes for changes to propagate.

    6. API Version

    Always use the latest supported API version for Azure OpenAI, as older versions may not support all features.

    Let me know if you need more help!


Your answer

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