From cff3b28bd5e56048143276274b8317634a6c180b Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 16 Feb 2025 23:16:45 -0500 Subject: [PATCH] Update Microsoft.Extensions.AI to 9.3.0-preview.1.25114.11 --- .../Extensions/MicrosoftExtensions.cs | 16 ++++++++-------- .../GenerativeAI.Microsoft.csproj | 2 +- .../GenerativeAIChatClient.cs | 6 ++---- .../GenerativeAIChatClient_Tests.cs | 14 +++++++------- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs b/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs index d938f643..4626079d 100644 --- a/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs +++ b/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs @@ -134,25 +134,25 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable - /// Transforms a into a . + /// Transforms a into a . /// /// The response to process. /// A object, or null if the response is invalid. - public static ChatCompletion? ToChatCompletion(this GenerateContentResponse? response) + public static ChatResponse? ToChatCompletion(this GenerateContentResponse? response) { if (response is null) return null; var chatMessage = ToChatMessage(response); - return new ChatCompletion(chatMessage) + return new ChatResponse(chatMessage) { FinishReason = ToFinishReason(response.Candidates?.FirstOrDefault()?.FinishReason), AdditionalProperties = null, Choices = new[] {chatMessage}.ToList(), - CompletionId = null, CreatedAt = null, ModelId = null, RawRepresentation = response, + ResponseId = null, Usage = ParseContentResponseUsage(response) }; } @@ -162,11 +162,10 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable /// The response to convert. /// A configured . - public static StreamingChatCompletionUpdate ToStreamingChatCompletionUpdate(this GenerateContentResponse? response) + public static ChatResponseUpdate ToStreamingChatCompletionUpdate(this GenerateContentResponse? response) { - return new StreamingChatCompletionUpdate + return new ChatResponseUpdate { - CompletionId = null, ChoiceIndex = 0, // Default to 0 as GenerativeAI doesn't support multiple choices CreatedAt = null, AdditionalProperties = null, @@ -174,8 +173,9 @@ public static StreamingChatCompletionUpdate ToStreamingChatCompletionUpdate(this ? ChatFinishReason.Stop : null, RawRepresentation = response, + ResponseId = null, + Role = ToAbstractionRole(response?.Candidates?.FirstOrDefault()?.Content?.Role), Text = response?.Text(), - Role = ToAbstractionRole(response?.Candidates?.FirstOrDefault()?.Content?.Role) }; } diff --git a/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj b/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj index feb62f2f..ed657fe4 100644 --- a/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj +++ b/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj @@ -26,7 +26,7 @@ - + \ No newline at end of file diff --git a/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs b/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs index 7864f496..49022b6a 100644 --- a/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs +++ b/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs @@ -29,7 +29,7 @@ public void Dispose() } /// - public async Task CompleteAsync(IList chatMessages, ChatOptions? options = null, + public async Task GetResponseAsync(IList chatMessages, ChatOptions? options = null, CancellationToken cancellationToken = default) { if (chatMessages == null) @@ -39,7 +39,7 @@ public async Task CompleteAsync(IList chatMessages, return response.ToChatCompletion() ?? throw new Exception("Failed to generate content"); } /// - public async IAsyncEnumerable CompleteStreamingAsync(IList chatMessages, + public async IAsyncEnumerable GetStreamingResponseAsync(IList chatMessages, ChatOptions? options = null, CancellationToken cancellationToken = new CancellationToken()) { @@ -60,6 +60,4 @@ public async IAsyncEnumerable CompleteStreamingAs } else return null; } - /// - public ChatClientMetadata Metadata { get; } } \ No newline at end of file diff --git a/tests/GenerativeAI.Microsoft.Tests/GenerativeAIChatClient_Tests.cs b/tests/GenerativeAI.Microsoft.Tests/GenerativeAIChatClient_Tests.cs index 78869454..8116ee20 100644 --- a/tests/GenerativeAI.Microsoft.Tests/GenerativeAIChatClient_Tests.cs +++ b/tests/GenerativeAI.Microsoft.Tests/GenerativeAIChatClient_Tests.cs @@ -93,7 +93,7 @@ public async Task ShouldThrowArgumentNullExceptionWhenChatMessagesIsNull() var client = new GenerativeAIChatClient(adapter); // Act & Assert - await Should.ThrowAsync(async () => { await client.CompleteAsync(null!); }); + await Should.ThrowAsync(async () => { await client.GetResponseAsync((string)null!); }); Console.WriteLine("CompleteAsync threw ArgumentNullException as expected when chatMessages was null."); } @@ -115,7 +115,7 @@ public async Task ShouldReturnChatCompletionOnValidInput() // For demonstration, we assume GenerateContentAsync(...) works. // Act - var result = await client.CompleteAsync(messages); + var result = await client.GetResponseAsync(messages); // Assert result.ShouldNotBeNull(); @@ -140,7 +140,7 @@ public async Task ShouldThrowArgumentNullExceptionWhenChatMessagesIsNullForStrea // Act & Assert await Should.ThrowAsync(async () => { - await foreach (var _ in client.CompleteStreamingAsync(null!)) + await foreach (var _ in client.GetStreamingResponseAsync((string)null!)) { // Should never get here Console.WriteLine(_.Text ?? "null"); @@ -162,8 +162,8 @@ public async Task ShouldReturnStreamOfMessagesOnValidInput() }; // Act - var updates = new List(); - await foreach (var update in client.CompleteStreamingAsync(messages)) + var updates = new List(); + await foreach (var update in client.GetStreamingResponseAsync(messages)) { updates.Add(update); Console.WriteLine(update.Text ?? "null"); @@ -222,8 +222,8 @@ public void MetadataShouldBeNullByDefault() var client = new GenerativeAIChatClient(adapter); // Assert - client.Metadata.ShouldBeNull(); - Console.WriteLine("By default, Metadata is null in GenerativeAIChatClient."); + client.GetService().ShouldBeNull(); + Console.WriteLine("By default, metadata is null in GenerativeAIChatClient."); } #endregion