Skip to content

Commit 7893a49

Browse files
committed
Update Azure OpenAI SDK
1 parent 31ef3d5 commit 7893a49

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

src/Azure.AISearch.WebApp/Azure.AISearch.WebApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.9" />
11+
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.12" />
1212
<PackageReference Include="Azure.Search.Documents" Version="11.5.0-beta.4" />
1313
<PackageReference Include="Azure.Storage.Blobs" Version="12.17.0" />
1414
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.20" />

src/Azure.AISearch.WebApp/Services/AzureOpenAISearchService.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// TODO: This doesn't work today, the API returns messages including the
2-
// citations messages we need, but the title and filepath etc are null
3-
// so it seems the field mappings aren't fully working (perhaps because
4-
// the serialized names are wrong? ContentFieldNames is serialized as
5-
// "contentFieldNames" but UrlFieldName as "urlField" for exampel).
6-
71
using System.Text.Json;
82
using System.Text.Json.Serialization;
93
using Azure.AI.OpenAI;
@@ -36,19 +30,19 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
3630

3731
var searchResponse = new SearchResponse();
3832
var chatCompletionsOptions = new ChatCompletionsOptions { DeploymentName = this.settings.OpenAIGptDeployment };
39-
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.System, request.SystemRoleInformation));
33+
chatCompletionsOptions.Messages.Add(new ChatRequestSystemMessage(request.SystemRoleInformation));
4034

4135
if (request.History != null && request.History.Any())
4236
{
4337
var role = ChatRole.User;
4438
foreach (var item in request.History)
4539
{
46-
chatCompletionsOptions.Messages.Add(new ChatMessage(role, item));
40+
chatCompletionsOptions.Messages.Add(role == ChatRole.User ? new ChatRequestUserMessage(item) : new ChatRequestAssistantMessage(item));
4741
searchResponse.History.Add(item);
4842
role = role == ChatRole.User ? ChatRole.Assistant : ChatRole.User;
4943
}
5044
}
51-
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.User, request.Query));
45+
chatCompletionsOptions.Messages.Add(new ChatRequestUserMessage(request.Query));
5246
searchResponse.History.Add(request.Query);
5347

5448
if (request.DataSource == DataSourceType.AzureCognitiveSearch)
@@ -84,7 +78,6 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
8478
var citationIndex = 0;
8579
foreach (var citation in content.Citations)
8680
{
87-
// TODO: citation title etc are null here!
8881
answerText = answerText.Replace($"[doc{++citationIndex}]", $"<cite>{citation.Title}</cite>", StringComparison.OrdinalIgnoreCase);
8982
searchResponse.SearchResults.Add(new SearchResult
9083
{
@@ -110,9 +103,10 @@ private AzureCognitiveSearchChatExtensionConfiguration GetAzureCognitiveSearchDa
110103
ArgumentNullException.ThrowIfNull(this.settings.OpenAIEndpoint);
111104
ArgumentNullException.ThrowIfNull(this.settings.OpenAIApiKey);
112105
var useDocumentsIndex = request.SearchIndex == SearchIndexType.Documents;
113-
var configuration = new AzureCognitiveSearchChatExtensionConfiguration
106+
return new AzureCognitiveSearchChatExtensionConfiguration
114107
{
115108
SearchEndpoint = new Uri(this.settings.SearchServiceUrl),
109+
Key = this.settings.SearchServiceAdminKey,
116110
IndexName = useDocumentsIndex ? this.settings.SearchIndexNameBlobDocuments : this.settings.SearchIndexNameBlobChunks,
117111
FieldMappingOptions = new AzureCognitiveSearchIndexFieldMappingOptions
118112
{
@@ -125,14 +119,9 @@ private AzureCognitiveSearchChatExtensionConfiguration GetAzureCognitiveSearchDa
125119
ShouldRestrictResultScope = request.LimitToDataSource, // Limit responses to data from the data source only
126120
QueryType = GetQueryType(request),
127121
SemanticConfiguration = request.IsSemanticSearch ? Constants.ConfigurationNames.SemanticConfigurationNameDefault : null,
128-
EmbeddingEndpoint = request.IsVectorSearch ? new Uri(new Uri(this.settings.OpenAIEndpoint), $"openai/deployments/{this.settings.OpenAIEmbeddingDeployment}/embeddings?api-version={this.settings.OpenAIApiVersion}") : null
122+
EmbeddingEndpoint = request.IsVectorSearch ? new Uri(new Uri(this.settings.OpenAIEndpoint), $"openai/deployments/{this.settings.OpenAIEmbeddingDeployment}/embeddings?api-version={this.settings.OpenAIApiVersion}") : null,
123+
EmbeddingKey = request.IsVectorSearch ? this.settings.OpenAIApiKey : null
129124
};
130-
configuration.SetSearchKey(this.settings.SearchServiceAdminKey);
131-
if (request.IsVectorSearch)
132-
{
133-
configuration.SetEmbeddingKey(this.settings.OpenAIApiKey);
134-
}
135-
return configuration;
136125
}
137126

138127
private AzureCognitiveSearchQueryType GetQueryType(SearchRequest request)

0 commit comments

Comments
 (0)