From bb0511882e2318afcc50fb06f5b1e6aba0338bbc Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 17 Feb 2025 00:38:25 -0500 Subject: [PATCH] Update Microsoft.Extensions.AI to 9.3.0-preview.1.25114.11 --- ...tSharp.Plugin.MicrosoftExtensionsAI.csproj | 2 +- ...osoftExtensionsAIChatCompletionProvider.cs | 29 +++++++++---------- ...osoftExtensionsAITextCompletionProvider.cs | 2 +- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/BotSharp.Plugin.MicrosoftExtensionsAI.csproj b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/BotSharp.Plugin.MicrosoftExtensionsAI.csproj index 66a9a1d9..b2667f35 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/BotSharp.Plugin.MicrosoftExtensionsAI.csproj +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/BotSharp.Plugin.MicrosoftExtensionsAI.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs index 67f1e513..8619ecad 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAIChatCompletionProvider.cs @@ -38,7 +38,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio IServiceProvider services) { _client = client; - _model = _client.Metadata.ModelId; + _model = _client.GetService()?.ModelId; _logger = logger; _services = services; } @@ -71,14 +71,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio if (agentService.RenderFunction(agent, function)) { var property = agentService.RenderFunctionProperty(agent, function); - (options.Tools ??= []).Add(new NopAIFunction(new(function.Name) - { - Description = function.Description, - Parameters = property?.Properties.RootElement.Deserialize>()?.Select(p => new AIFunctionParameterMetadata(p.Key) - { - Schema = p.Value, - }).ToList() ?? [], - })); + (options.Tools ??= []).Add(new NopAIFunction(function.Name, function.Description, JsonSerializer.SerializeToElement(property))); } } } @@ -111,7 +104,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio messages.Add(new(ChatRole.Assistant, [ new FunctionCallContent(x.FunctionName, x.FunctionName, JsonSerializer.Deserialize>(x.FunctionArgs ?? "{}")), - new FunctionResultContent(x.FunctionName, x.FunctionName, x.Content) + new FunctionResultContent(x.FunctionName, x.Content) ])); } else if (x.Role == AgentRole.System || x.Role == AgentRole.Assistant) @@ -127,17 +120,17 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio { if (!string.IsNullOrEmpty(file.FileData)) { - contents.Add(new ImageContent(file.FileData)); + contents.Add(new DataContent(file.FileData)); } else if (!string.IsNullOrEmpty(file.FileStorageUrl)) { var contentType = FileUtility.GetFileContentType(file.FileStorageUrl); var bytes = fileStorage!.GetFileBytes(file.FileStorageUrl); - contents.Add(new ImageContent(bytes, contentType)); + contents.Add(new DataContent(bytes, contentType)); } else if (!string.IsNullOrEmpty(file.FileUrl)) { - contents.Add(new ImageContent(file.FileUrl)); + contents.Add(new DataContent(file.FileUrl)); } } } @@ -146,7 +139,7 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio } } - var completion = await _client.CompleteAsync(messages); + var completion = await _client.GetResponseAsync(messages); RoleDialogModel result = new(AgentRole.Assistant, string.Concat(completion.Message.Contents.OfType())) { @@ -175,9 +168,13 @@ public sealed class MicrosoftExtensionsAIChatCompletionProvider : IChatCompletio public Task GetChatCompletionsStreamingAsync(Agent agent, List conversations, Func onMessageReceived) => throw new NotImplementedException(); - private sealed class NopAIFunction(AIFunctionMetadata metadata) : AIFunction + private sealed class NopAIFunction(string name, string description, JsonElement schema) : AIFunction { - public override AIFunctionMetadata Metadata { get; } = metadata; + public override string Name => name; + + public override string Description => description; + + public override JsonElement JsonSchema => schema; protected override Task InvokeCoreAsync(IEnumerable> arguments, CancellationToken cancellationToken) => throw new NotSupportedException(); diff --git a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs index 152213c6..94d26678 100644 --- a/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MicrosoftExtensionsAI/MicrosoftExtensionsAITextCompletionProvider.cs @@ -49,7 +49,7 @@ public sealed class MicrosoftExtensionsAITextCompletionProvider : ITextCompletio await Task.WhenAll(hooks.Select(hook => hook.BeforeGenerating(agent, [userMessage]))); _tokenStatistics.StartTimer(); - var completion = await _chatClient.CompleteAsync(text); + var completion = await _chatClient.GetResponseAsync(text); var result = string.Concat(completion.Message.Contents.OfType()); _tokenStatistics.StopTimer();