Azure OpenAI Service: How to call Semantic Search using the .NET API

大宮 僚馬 70 Reputation points
2023-10-16T08:02:45.7433333+00:00

Calling OpenAI Service from a C# application using Azure.AI.OpenAI v1.0.0-beta.8.

I am using the semantic search option in Add your data (preview), but it is not very accurate and I do not know if I am using it correctly.

Is my API call the right way?

The Cognitive Search index was created from Azure OpenAI Studio with Upload files.

Below is the code for the API call.

ChatCompletionsOptions completionsOptions = new ChatCompletionsOptions();
completionsOptions.Temperature = (float)0.0;
completionsOptions.MaxTokens = 800;
completionsOptions.NucleusSamplingFactor = (float)1.0;
completionsOptions.FrequencyPenalty = 0;
completionsOptions.PresencePenalty = 0;

AzureCognitiveSearchIndexFieldMappingOptions options = new AzureCognitiveSearchIndexFieldMappingOptions();
options.ContentFieldNames.Add("content");
options.FilepathFieldName = "filepath";
options.TitleFieldName = "title";
options.UrlFieldName = "url";

completionsOptions.Messages.Add(new ChatMessage(ChatRole.System, "You are an AI assistant that helps people find information."));
completionsOptions.AzureExtensionsOptions = new AzureChatExtensionsOptions()
{
    Extensions =
        {
            new AzureCognitiveSearchChatExtensionConfiguration()
            {
                SearchEndpoint = new Uri(_searchEndPoint),
                SearchKey = new AzureKeyCredential(_searchKey),
                IndexName = _searchIndex,
                QueryType = AzureCognitiveSearchQueryType.Semantic,
                SemanticConfiguration = "default",
                FieldMappingOptions = options,
            },
        }
};

OpenAIClientOptions clientOptions = new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2023_09_01_Preview);
OpenAIClient client = new OpenAIClient(new Uri(_endPoint), new AzureKeyCredential(_apiKey), clientOptions);

Response<ChatCompletions> response = await client.GetChatCompletionsAsync(_model, completionsOptions);

Also attached is a capture of the index configuration.

fields

semantic configurations

Thank you in advance for your help.

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.
1,339 questions
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,081 questions
{count} votes

Accepted answer
  1. brtrach-MSFT 17,731 Reputation points Microsoft Employee Moderator
    2023-10-18T00:18:28.4866667+00:00

    @大宮 僚馬 To call the semantic search API, you need to use the OpenAIClient.SearchAsync method instead of OpenAIClient.GetChatCompletionsAsync. Here is an example of how to call the semantic search API using the OpenAIClient:

    OpenAIClientOptions clientOptions = new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2023_09_01_Preview);
    OpenAIClient client = new OpenAIClient(new Uri(_endPoint), new AzureKeyCredential(_apiKey), clientOptions);
    
    string query = "your search query";
    string indexName = "your index name";
    string semanticConfiguration = "your semantic configuration name";
    
    SearchOptions searchOptions = new SearchOptions();
    searchOptions.QueryType = OpenAIQueryType.Semantic;
    searchOptions.SemanticConfiguration = semanticConfiguration;
    
    SearchRequest searchRequest = new SearchRequest(query, indexName, searchOptions);
    
    Response response = await client.SearchAsync(searchRequest);
    
    
    

    In this example, you need to replace "your search query", "your index name", and "your semantic configuration name" with the appropriate values for your search.

    Regarding your question about the accuracy of the semantic search, it is difficult to determine the accuracy without more information about your data and configuration. However, if you are indexing documents in Japanese and the analyzer is not in Japanese, it is possible that the accuracy of the search results will be reduced. You may want to consider using a Japanese analyzer to improve the accuracy of the search results.


0 additional answers

Sort by: Most helpful

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.