Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.47k stars 4.8k forks source link

Why can't OpenAI access the Azure Search Resource? [FIXED] #45261

Open santiagogun opened 3 months ago

santiagogun commented 3 months ago

Library name and version

Azure.AI.OpenAI" Version="2.0.0-beta.2"

Query/Question

Hello, so I am trying to call the OpenAI client while using AzureAISearch as a Data source. The problem I am having is that the resource does not seem to be used whenever I call OpenAI and the response given by it is that it does not has any information on the topic. In the Chat playground I am able to make this part work and it properly gives me an answer surrounding the topic fed to the LLM by AzureAISearch.

Basically, the OpenAIClient works properly but I cannot get answers from the Data source. It is also important to note that I have tried the code with API keys, and it still does not work. I also tried this code using the sample given in the Chat playground step by step, and still nothing. This is how my code is looking right now.

and I am including all the packages shown in the sample.

            ChatClient chatClient = azureClient.GetChatClient("StackBuddAY");

            // Setup chat completion options with Azure Search data source
            ChatCompletionOptions options = new ();
#pragma warning disable AOAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
            options.AddDataSource(new AzureSearchChatDataSource()
            {
                Endpoint = new Uri("https://stackbuddysearchresource.search.windows.net"),
                IndexName = "tags",
                Authentication = DataSourceAuthentication.FromUserManagedIdentity(AppConfig.MIClientId), 
            });
#pragma warning restore AOAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

            // Create chat completion request
            ChatCompletion completion = chatClient.CompleteChat(
                new List<ChatMessage>()
                {
                    new UserChatMessage("Do you have any specific documentation about the MSI team from managed Identities that has explicitly been provided to you? ?")
                });

            // Process and print the response
#pragma warning disable AOAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
            AzureChatMessageContext onYourDataContext = completion.GetAzureMessageContext();
#pragma warning restore AOAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
            FunctionLogger.Instance.LogInformation("CHAT MESSAGES FOR TEST");
            FunctionLogger.Instance.LogInformation(completion.Role + ": " + completion.Content[0].Text);
            if (onYourDataContext?.Intent is not null)
            {
                FunctionLogger.Instance.LogInformation($"Intent: {onYourDataContext.Intent}");
            }

            foreach (AzureChatCitation citation in onYourDataContext?.Citations ?? new List<AzureChatCitation>())
            {
                FunctionLogger.Instance.LogInformation($"Citation: {citation.Content}");
            }

            return Task.CompletedTask;

Environment

I am running this in .NET 8.0 and in an Azure function

github-actions[bot] commented 3 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jpalvarezl @ralph-msft @trrwilson.

rgknp commented 1 month ago

Any update here? I am running into exact same issue. Azure Search data source works in Chat playground but returns "The requested information is not found in the retrieved data. Please try another query or topic." when called from the app using the SDK.

rgknp commented 1 month ago

@santiagogun I see you marked the issue as fixed. Can you please help me understand how this was fixed?

DeyNik commented 1 month ago

Hi @santiagogun , I ran into the same issue, can you help me resolve it please?

fabioty commented 1 month ago

Change:

            ChatCompletion completion = chatClient.CompleteChat(
                new List<ChatMessage>()
                {
                                new UserChatMessage(chatMessage)
                }
            );

To:

            ChatCompletion completion = chatClient.CompleteChat(
                new List<ChatMessage>()
                {
                                new UserChatMessage(chatMessage)
                },
                options
            );