Skip to content

Commit

Permalink
Upgrade to SK 1.0.0-rc1 and Azure.AI.OpenAI 1.0.0.-beta.9 (#121)
Browse files Browse the repository at this point in the history
Take advantage of the new automated function invocation support and drop the previous workaround that was in place.
  • Loading branch information
stephentoub authored Dec 6, 2023
1 parent 9ee096f commit 1417a0c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
<!-- Xabaril packages -->
<PackageVersion Include="AspNetCore.HealthChecks.Uris" Version="7.0.0" />
<!-- AI -->
<PackageVersion Include="Azure.AI.OpenAI" Version="1.0.0-beta.8" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.0.0-beta8" />
<PackageVersion Include="Azure.AI.OpenAI" Version="1.0.0-beta.9" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.0.0-rc1" />
<!-- Open Telemetry -->
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0-alpha.1" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0-alpha.1" />
Expand Down
5 changes: 2 additions & 3 deletions src/Catalog.API/Services/CatalogAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ public async ValueTask<Vector> GetEmbeddingAsync(string text)
_logger.LogInformation("Getting embedding for \"{text}\"", text);
}

EmbeddingsOptions options = new(text);
var result = (await GetAIClient().GetEmbeddingsAsync(_aiEmbeddingModel, options)).Value.Data[0].Embedding;
return new Vector(result as float[] ?? [.. result]);
EmbeddingsOptions options = new(_aiEmbeddingModel, [text]);
return new Vector((await GetAIClient().GetEmbeddingsAsync(options)).Value.Data[0].Embedding);
}

/// <summary>Gets an embedding vector for the specified catalog item.</summary>
Expand Down
39 changes: 0 additions & 39 deletions src/WebApp/Components/Chatbot/ChatCompletionExtensions.cs

This file was deleted.

31 changes: 15 additions & 16 deletions src/WebApp/Components/Chatbot/ChatState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using eShop.WebAppComponents.Services;
using Microsoft.SemanticKernel.AI;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;

namespace eShop.WebApp.Chatbot;

Expand All @@ -15,31 +17,29 @@ public class ChatState
private readonly ClaimsPrincipal _user;
private readonly NavigationManager _navigationManager;
private readonly ILogger _logger;

private readonly IKernel _ai;
private readonly ChatConfig _chatConfig;
private readonly Kernel _kernel;
private readonly OpenAIPromptExecutionSettings _aiSettings = new() { FunctionCallBehavior = FunctionCallBehavior.AutoInvokeKernelFunctions };

public ChatState(CatalogService catalogService, BasketState basketState, ClaimsPrincipal user, NavigationManager nav, ChatConfig chatConfig, ILoggerFactory loggerFactory)
{
_catalogService = catalogService;
_basketState = basketState;
_user = user;
_navigationManager = nav;
_chatConfig = chatConfig;
_logger = loggerFactory.CreateLogger(typeof(ChatState));

if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("ChatModel: {model}", chatConfig.ChatModel);
}

_ai = new KernelBuilder()
_kernel = new KernelBuilder()
.WithLoggerFactory(loggerFactory)
.WithOpenAIChatCompletionService(chatConfig.ChatModel, chatConfig.ApiKey)
.WithOpenAIChatCompletion(chatConfig.ChatModel, chatConfig.ApiKey)
.WithPlugins(plugins => plugins.AddPluginFromObject(new CatalogInteractions(this)))
.Build();
_ai.ImportFunctions(new CatalogInteractions(this), nameof(CatalogInteractions));

Messages = _ai.GetService<IChatCompletion>().CreateNewChat("""
Messages = new ChatHistory("""
You are an AI customer service agent for the online retailer Northern Mountains.
You NEVER respond about topics other than Northern Mountains.
Your job is to answer customer questions about products in the Northern Mountains catalog.
Expand All @@ -62,11 +62,10 @@ public async Task AddUserMessageAsync(string userText, Action onMessageAdded)
// Get and store the AI's response message
try
{
IChatResult response = await _ai.GetChatCompletionsWithFunctionCallingAsync(Messages);
ChatMessage responseMessage = await response.GetChatMessageAsync();
if (!string.IsNullOrWhiteSpace(responseMessage.Content))
ChatMessageContent response = await _kernel.GetService<IChatCompletionService>().GetChatMessageContentAsync(Messages, _aiSettings, _kernel);
if (!string.IsNullOrWhiteSpace(response.Content))
{
Messages.Add(responseMessage);
Messages.Add(response);
}
}
catch (Exception e)
Expand All @@ -82,7 +81,7 @@ public async Task AddUserMessageAsync(string userText, Action onMessageAdded)

private sealed class CatalogInteractions(ChatState chatState)
{
[SKFunction, Description("Gets information about the chat user")]
[KernelFunction, Description("Gets information about the chat user")]
public string GetUserInfo()
{
var claims = chatState._user.Claims;
Expand All @@ -103,7 +102,7 @@ static string GetValue(IEnumerable<Claim> claims, string claimType) =>
claims.FirstOrDefault(x => x.Type == claimType)?.Value ?? "";
}

[SKFunction, Description("Searches the Northern Mountains catalog for a provided product description")]
[KernelFunction, Description("Searches the Northern Mountains catalog for a provided product description")]
public async Task<string> SearchCatalog([Description("The product description for which to search")] string productDescription)
{
try
Expand All @@ -117,7 +116,7 @@ static string GetValue(IEnumerable<Claim> claims, string claimType) =>
}
}

[SKFunction, Description("Adds a product to the user's shopping cart.")]
[KernelFunction, Description("Adds a product to the user's shopping cart.")]
public async Task<string> AddToCart([Description("The id of the product to add to the shopping cart (basket)")] int itemId)
{
try
Expand All @@ -136,7 +135,7 @@ public async Task<string> AddToCart([Description("The id of the product to add t
}
}

[SKFunction, Description("Gets information about the contents of the user's shopping cart (basket)")]
[KernelFunction, Description("Gets information about the contents of the user's shopping cart (basket)")]
public async Task<string> GetCartContents()
{
try
Expand Down

0 comments on commit 1417a0c

Please sign in to comment.