Skip to content

Commit 0410efe

Browse files
committed
Upgrade Semantic Kernel version
1 parent 24f9ea8 commit 0410efe

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/Azure.AISearch.FunctionApp.DotNet/Azure.AISearch.FunctionApp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<PackageReference Include="Azure.Search.Documents" Version="11.5.0-beta.5" />
1212
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
1313
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
14-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
14+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
1515
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
16-
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta3" />
16+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.1" />
1717
</ItemGroup>
1818
<ItemGroup>
1919
<None Update="host.json">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
1414
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.25" />
1515
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
16-
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta3" />
16+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.1" />
1717
</ItemGroup>
1818

1919
</Project>

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Text;
22
using Azure.AISearch.WebApp.Models;
33
using Microsoft.SemanticKernel;
4-
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
4+
using Microsoft.SemanticKernel.Connectors.OpenAI;
55

66
namespace Azure.AISearch.WebApp.Services;
77

@@ -35,10 +35,11 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
3535
var openAIGptDeployment = string.IsNullOrEmpty(request.OpenAIGptDeployment) ? this.settings.OpenAIGptDeployment : request.OpenAIGptDeployment;
3636
ArgumentNullException.ThrowIfNull(openAIGptDeployment);
3737
var prompt = string.IsNullOrWhiteSpace(request.CustomOrchestrationPrompt) ? this.settings.GetDefaultCustomOrchestrationPrompt() : request.CustomOrchestrationPrompt;
38-
var kernel = Kernel.Builder
39-
.WithAzureChatCompletionService(openAIGptDeployment, this.settings.OpenAIEndpoint, this.settings.OpenAIApiKey, true)
40-
.Build();
41-
var requestSettings = new OpenAIRequestSettings
38+
var kernelBuilder = Kernel.CreateBuilder();
39+
kernelBuilder.Services.AddLogging(c => c.AddDebug().SetMinimumLevel(LogLevel.Trace));
40+
kernelBuilder.AddAzureOpenAIChatCompletion(openAIGptDeployment, this.settings.OpenAIEndpoint, this.settings.OpenAIApiKey);
41+
var kernel = kernelBuilder.Build();
42+
var executionSettings = new OpenAIPromptExecutionSettings
4243
{
4344
MaxTokens = request.MaxTokens ?? Constants.Defaults.MaxTokens,
4445
Temperature = request.Temperature ?? Constants.Defaults.Temperature,
@@ -47,12 +48,14 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
4748
PresencePenalty = request.PresencePenalty ?? Constants.Defaults.PresencePenalty,
4849
StopSequences = (request.StopSequences ?? Constants.Defaults.StopSequences).Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
4950
};
50-
var function = kernel.CreateSemanticFunction(prompt, requestSettings);
51+
var function = kernel.CreateFunctionFromPrompt(prompt, executionSettings);
5152

5253
var response = new SearchResponse();
53-
var context = kernel.CreateNewContext();
54-
context.Variables["query"] = request.Query;
55-
54+
var arguments = new KernelArguments
55+
{
56+
{ "query", request.Query }
57+
};
58+
5659
// Query the search index for relevant data first, by passing through the original request
5760
// to the Azure AI Search service.
5861
var azureCognitiveSearchResponse = await this.azureCognitiveSearchService.SearchAsync(request);
@@ -74,13 +77,13 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
7477
}
7578
}
7679

77-
// Add the sources string to the context, so that the semantic function can use it to construct the prompt.
78-
context.Variables["sources"] = sourcesBuilder.ToString();
80+
// Add the sources string to the arguments, so that the semantic function can use it to construct the prompt.
81+
arguments.Add("sources", sourcesBuilder.ToString());
7982

8083
// Run the semantic function to generate the answer.
8184
try
8285
{
83-
var kernelResult = await kernel.RunAsync(context.Variables, function);
86+
var kernelResult = await kernel.InvokeAsync(function, arguments);
8487
var answer = kernelResult.GetValue<string>();
8588
if (!string.IsNullOrWhiteSpace(answer))
8689
{

0 commit comments

Comments
 (0)