Skip to content

Commit

Permalink
Upgrade Semantic Kernel version
Browse files Browse the repository at this point in the history
  • Loading branch information
jelledruyts committed Dec 19, 2023
1 parent 24f9ea8 commit 0410efe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<PackageReference Include="Azure.Search.Documents" Version="11.5.0-beta.5" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta3" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
2 changes: 1 addition & 1 deletion src/Azure.AISearch.WebApp/Azure.AISearch.WebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.25" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta3" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.1" />
</ItemGroup>

</Project>
27 changes: 15 additions & 12 deletions src/Azure.AISearch.WebApp/Services/SemanticKernelSearchService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text;
using Azure.AISearch.WebApp.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
using Microsoft.SemanticKernel.Connectors.OpenAI;

namespace Azure.AISearch.WebApp.Services;

Expand Down Expand Up @@ -35,10 +35,11 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
var openAIGptDeployment = string.IsNullOrEmpty(request.OpenAIGptDeployment) ? this.settings.OpenAIGptDeployment : request.OpenAIGptDeployment;
ArgumentNullException.ThrowIfNull(openAIGptDeployment);
var prompt = string.IsNullOrWhiteSpace(request.CustomOrchestrationPrompt) ? this.settings.GetDefaultCustomOrchestrationPrompt() : request.CustomOrchestrationPrompt;
var kernel = Kernel.Builder
.WithAzureChatCompletionService(openAIGptDeployment, this.settings.OpenAIEndpoint, this.settings.OpenAIApiKey, true)
.Build();
var requestSettings = new OpenAIRequestSettings
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddLogging(c => c.AddDebug().SetMinimumLevel(LogLevel.Trace));
kernelBuilder.AddAzureOpenAIChatCompletion(openAIGptDeployment, this.settings.OpenAIEndpoint, this.settings.OpenAIApiKey);
var kernel = kernelBuilder.Build();
var executionSettings = new OpenAIPromptExecutionSettings
{
MaxTokens = request.MaxTokens ?? Constants.Defaults.MaxTokens,
Temperature = request.Temperature ?? Constants.Defaults.Temperature,
Expand All @@ -47,12 +48,14 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
PresencePenalty = request.PresencePenalty ?? Constants.Defaults.PresencePenalty,
StopSequences = (request.StopSequences ?? Constants.Defaults.StopSequences).Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
};
var function = kernel.CreateSemanticFunction(prompt, requestSettings);
var function = kernel.CreateFunctionFromPrompt(prompt, executionSettings);

var response = new SearchResponse();
var context = kernel.CreateNewContext();
context.Variables["query"] = request.Query;

var arguments = new KernelArguments
{
{ "query", request.Query }
};

// Query the search index for relevant data first, by passing through the original request
// to the Azure AI Search service.
var azureCognitiveSearchResponse = await this.azureCognitiveSearchService.SearchAsync(request);
Expand All @@ -74,13 +77,13 @@ public async Task<SearchResponse> SearchAsync(SearchRequest request)
}
}

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

// Run the semantic function to generate the answer.
try
{
var kernelResult = await kernel.RunAsync(context.Variables, function);
var kernelResult = await kernel.InvokeAsync(function, arguments);
var answer = kernelResult.GetValue<string>();
if (!string.IsNullOrWhiteSpace(answer))
{
Expand Down

0 comments on commit 0410efe

Please sign in to comment.