admin管理员组

文章数量:1292053

Whenever executing the below code it is failing as error 400, any help would be appreciated or any other way to do it.

Error 400: Service request failed. Status: 400 (Bad Request)

We are using latest version of Azure.AI.OpenAI as below.

<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />

Here is the code.

var client = new AzureOpenAIClient(new Uri("https://[your-url].openai.azure/"), new AzureKeyCredential("open-ai-api-key")); //
ChatClient chatClient = client.GetChatClient("btGPT4");
#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.


Azure.AI.OpenAI.Chat.AzureSearchChatDataSource azureSearchChatDataSource = new()
{
    Authentication = DataSourceAuthentication.FromApiKey("[search_admin_key]"),
    IndexName = "test-gpt4",
    Endpoint = new Uri("https://[your url].search.windows")
};

ChatCompletionOptions chatOptions = new();
chatOptions.AddDataSource(azureSearchChatDataSource);
chatOptions.MaxOutputTokenCount = 100;

string systemPrompt = "You are an AI assistant that helps people find information.";

ChatCompletion completion = chatClient.CompleteChat(
                        [
                            new SystemChatMessage(systemPrompt),
                        new UserChatMessage(query),
                    ], chatOptions);

return completion.Content[0].Text;
#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.

Whenever executing the below code it is failing as error 400, any help would be appreciated or any other way to do it.

Error 400: Service request failed. Status: 400 (Bad Request)

We are using latest version of Azure.AI.OpenAI as below.

<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />

Here is the code.

var client = new AzureOpenAIClient(new Uri("https://[your-url].openai.azure/"), new AzureKeyCredential("open-ai-api-key")); //
ChatClient chatClient = client.GetChatClient("btGPT4");
#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.


Azure.AI.OpenAI.Chat.AzureSearchChatDataSource azureSearchChatDataSource = new()
{
    Authentication = DataSourceAuthentication.FromApiKey("[search_admin_key]"),
    IndexName = "test-gpt4",
    Endpoint = new Uri("https://[your url].search.windows")
};

ChatCompletionOptions chatOptions = new();
chatOptions.AddDataSource(azureSearchChatDataSource);
chatOptions.MaxOutputTokenCount = 100;

string systemPrompt = "You are an AI assistant that helps people find information.";

ChatCompletion completion = chatClient.CompleteChat(
                        [
                            new SystemChatMessage(systemPrompt),
                        new UserChatMessage(query),
                    ], chatOptions);

return completion.Content[0].Text;
#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.
Share Improve this question edited Feb 19 at 8:44 Suresh Chikkam 3,4732 gold badges4 silver badges12 bronze badges Recognized by Microsoft Azure Collective asked Feb 13 at 10:38 jkyadavjkyadav 1,2826 gold badges18 silver badges32 bronze badges 2
  • AzureSearchChatDataSource class is experimental, and it may not be supported for all GPT models. If you cannot resolve the issue, consider embedding Azure Search results manually into the prompt. – Suresh Chikkam Commented Feb 13 at 13:01
  • @SureshChikkam I know it is an experimental feature that's why that comment added in the code. Why I moved to this is because when I pass the azure search result manually it is not returning correct answers, even thought if I upload same file on the azure portal and try it there seems returning correct result. Any insights? – jkyadav Commented Feb 13 at 14:36
Add a comment  | 

1 Answer 1

Reset to default 0

I upload same file on the azure portal and try it there seems returning correct result.

Portal provides search results as "context" in the prompt. So, Here AI model is primed with relevant data to answer the question accurately.

  • When you pass the search results manually, these preprocessing and fine-tuning steps are likely missing, leading to poorer performance.

Instead of using AzureSearchChatDataSource, you can preprocess search results and include them directly in the system prompt.

string searchResults = "Result 1: ...; Result 2: ...;"; 

string systemPrompt = "You are an AI assistant that helps people find information using the following context:\n"
                    + searchResults
                    + "\nAnswer the user's query using this context.";

string userQuery = "What is the main topic discussed in the file?";

var chatOptions = new ChatCompletionsOptions
{
    MaxTokens = 100,
    Temperature = 0.7
};

chatOptions.Messages.Add(new ChatMessage(ChatRole.System, systemPrompt));
chatOptions.Messages.Add(new ChatMessage(ChatRole.User, userQuery));

Response<ChatCompletions> response = await client.GetChatCompletionsAsync("btGPT4", chatOptions);

Console.WriteLine(response.Value.Choices[0].Message.Content);

If the search results are too verbose, summarize them before passing them to the OpenAI model.

string summarizePrompt = "Summarize the following text for use in answering questions:\n" + searchResults;

var summarizeOptions = new ChatCompletionsOptions
{
    MaxTokens = 200,
    Temperature = 0.5
};

summarizeOptions.Messages.Add(new ChatMessage(ChatRole.System, "You are a summarizer."));
summarizeOptions.Messages.Add(new ChatMessage(ChatRole.User, summarizePrompt));

Response<ChatCompletions> summaryResponse = await client.GetChatCompletionsAsync("btGPT4", summarizeOptions);

string summarizedContext = summaryResponse.Value.Choices[0].Message.Content;

I have this above scenario with RAG-Based Approach (Manual Retrieval).

Query Azure Cognitive Search for relevant documents. Retrieve and preprocess the search results. Build a structured and optimized prompt then pass the prompt to Azure OpenAI for completion.

Here is my build prompt:

System: You are an AI assistant that provides accurate answers based on the given context.

Context:
1. Document 1: [Summary or key text here].
2. Document 2: [Summary or key text here].

Question: [User query here]

Always base your answers on the provided context and explicitly say if the information is not available.

Integrating Cognitive Search with Azure OpenAI:

var searchClient = new SearchClient(new Uri("https://<search-name>.search.windows"),
                                    "index-name",
                                    new AzureKeyCredential("<search-api-key>"));

var options = new SearchOptions { Size = 3 }; // Fetch top 3 results
options.QueryType = SearchQueryType.Full;
options.Select.Add("content"); // Fetch content field

var searchResults = searchClient.Search<SearchDocument>("query", options);


var context = "Context:\n";
foreach (var result in searchResults.GetResults())
{
    context += $"- {result.Document["content"]}\n";
}


var client = new AzureOpenAIClient(new Uri("https://<openai-resource-name>.openai.azure/"),
                                   new AzureKeyCredential("<openai-api-key>"));

var chatOptions = new ChatCompletionsOptions
{
    Messages =
    {
        new ChatMessage(ChatRole.System, "You are an AI assistant that answers based on the provided context."),
        new ChatMessage(ChatRole.User, context + "Question: " + query)
    },
    MaxTokens = 500
};

var response = client.GetChatCompletions("deployment-name", chatOptions);
Console.WriteLine(response.Choices[0].Message.Content);

RAG-based approach is more reliable because it gives you full control over how search results are fetched, processed, and formatted.

本文标签: aspnet coreAzure ai throwing service request failed error 400Stack Overflow