diff --git a/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs b/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs index 6151d3b..451f182 100644 --- a/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs +++ b/src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs @@ -85,12 +85,12 @@ where p is not null return content switch { TextContent text => new Part { Text = text.Text }, - DataContent image when image.Data != null => new Part + DataContent image => new Part { InlineData = new Blob() { MimeType = image.MediaType, - Data = Convert.ToBase64String(image.Data!.Value.ToArray()), + Data = Convert.ToBase64String(image.Data.ToArray()), } }, FunctionCallContent fcc => new Part @@ -238,7 +238,6 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable s.Content).SelectMany(s => s.Parts).ToList().ToAiContents(), - ChoiceIndex = 0, AdditionalProperties = null, FinishReason = response?.Candidates?.FirstOrDefault()?.FinishReason == FinishReason.OTHER ? ChatFinishReason.Stop @@ -462,7 +460,7 @@ public static IList ToAiContents(this List? parts) public static FunctionCallContent? GetFunction(this ChatResponse response) { - var aiFunction = (FunctionCallContent?) response.Choices.SelectMany(s=>s.Contents).FirstOrDefault(s=>s is FunctionCallContent); + var aiFunction = (FunctionCallContent?) response.Messages.SelectMany(s=>s.Contents).FirstOrDefault(s=>s is FunctionCallContent); return aiFunction; } } \ No newline at end of file diff --git a/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj b/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj index 476f70e..c0cb45d 100644 --- a/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj +++ b/src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj @@ -27,7 +27,7 @@ - + \ No newline at end of file diff --git a/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs b/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs index 1c071ba..f0a86c0 100644 --- a/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs +++ b/src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs @@ -41,12 +41,12 @@ public void Dispose() } /// - public async Task GetResponseAsync(IList chatMessages, ChatOptions? options = null, + public async Task GetResponseAsync(IEnumerable messages, ChatOptions? options = null, CancellationToken cancellationToken = default) { - if (chatMessages == null) - throw new ArgumentNullException(nameof(chatMessages)); - var request = chatMessages.ToGenerateContentRequest(options); + if (messages == null) + throw new ArgumentNullException(nameof(messages)); + var request = messages.ToGenerateContentRequest(options); var response = await model.GenerateContentAsync(request, cancellationToken).ConfigureAwait(false); return await CallFunctionAsync(request, response, @@ -96,13 +96,13 @@ private async Task CallFunctionAsync(GenerateContentRequest reques } /// - public async IAsyncEnumerable GetStreamingResponseAsync(IList chatMessages, + public async IAsyncEnumerable GetStreamingResponseAsync(IEnumerable messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (chatMessages == null) - throw new ArgumentNullException(nameof(chatMessages)); - var request = chatMessages.ToGenerateContentRequest(options); + if (messages == null) + throw new ArgumentNullException(nameof(messages)); + var request = messages.ToGenerateContentRequest(options); await foreach (var response in model.StreamContentAsync(request, cancellationToken).ConfigureAwait(false)) { yield return response.ToChatResponseUpdate(); diff --git a/tests/AotTest/MEAITests.cs b/tests/AotTest/MEAITests.cs index 4953cd4..c5f7c16 100644 --- a/tests/AotTest/MEAITests.cs +++ b/tests/AotTest/MEAITests.cs @@ -21,8 +21,8 @@ public async Task ShouldWorkWithTools() var message = new ChatMessage(ChatRole.User, "What is the weather in New York in celsius?"); var response = await chatClient.GetResponseAsync(message,options:chatOptions).ConfigureAwait(false); - Console.WriteLine(response.Choices.LastOrDefault().Text); - response.Choices.LastOrDefault().Text.Contains("New York", StringComparison.InvariantCultureIgnoreCase); + Console.WriteLine(response.Text); + response.Text.Contains("New York", StringComparison.InvariantCultureIgnoreCase); } @@ -40,7 +40,7 @@ public async Task ShouldWorkWith_BookStoreService() var message = new ChatMessage(ChatRole.User, "what is written on page 96 in the book 'damdamadum'"); var response = await chatClient.GetResponseAsync(message,options:chatOptions).ConfigureAwait(false); - response.Choices.LastOrDefault().Text.ShouldContain("damdamadum",Case.Insensitive); + response.Text.ShouldContain("damdamadum",Case.Insensitive); } [FunctionTool(MeaiFunctionTool = true)] diff --git a/tests/GenerativeAI.Microsoft.Tests/MicrosoftExtension_Tests.cs b/tests/GenerativeAI.Microsoft.Tests/MicrosoftExtension_Tests.cs index d1061f4..a3940d3 100644 --- a/tests/GenerativeAI.Microsoft.Tests/MicrosoftExtension_Tests.cs +++ b/tests/GenerativeAI.Microsoft.Tests/MicrosoftExtension_Tests.cs @@ -128,7 +128,6 @@ public void ToChatResponseUpdate_ShouldMapCorrectly() result.RawRepresentation.ShouldBe(response); // result.Role.ShouldBe((ChatRole?)candidate.Content?.Role); result.Text.ShouldBe(candidate.Content.Parts[0].Text); - result.ChoiceIndex.ShouldBe(0); result.CreatedAt.ShouldBeNull(); result.AdditionalProperties.ShouldBeNull(); result.ResponseId.ShouldBeNull(); @@ -241,7 +240,6 @@ public void ToAiContents_WithInlineDataPart_ReturnsDataContent() result.FirstOrDefault().ShouldBeOfType(); var dataContent = (DataContent)result.FirstOrDefault(); dataContent.MediaType.ShouldBe("image/png"); - dataContent.Data.ShouldNotBeNull(); } [Fact] diff --git a/tests/GenerativeAI.Microsoft.Tests/Microsoft_AIFunction_Tests.cs b/tests/GenerativeAI.Microsoft.Tests/Microsoft_AIFunction_Tests.cs index afde82a..0891a94 100644 --- a/tests/GenerativeAI.Microsoft.Tests/Microsoft_AIFunction_Tests.cs +++ b/tests/GenerativeAI.Microsoft.Tests/Microsoft_AIFunction_Tests.cs @@ -37,7 +37,7 @@ public async Task ShouldWorkWithTools() var message = new ChatMessage(ChatRole.User, "What is the weather in New York?"); var response = await chatClient.GetResponseAsync(message,options:chatOptions).ConfigureAwait(false); - response.Choices.LastOrDefault().Text.Contains("New York", StringComparison.InvariantCultureIgnoreCase) + response.Text.Contains("New York", StringComparison.InvariantCultureIgnoreCase) .ShouldBeTrue(); } @@ -53,9 +53,9 @@ public async Task ShouldWorkWithComplexClasses() var message = new ChatMessage(ChatRole.User, "How does student john doe in senior grade is doing this year, enrollment start 01-01-2024 to 01-01-2025?"); var response = await chatClient.GetResponseAsync(message,options:chatOptions).ConfigureAwait(false); - response.Choices.LastOrDefault().Text.Contains("John", StringComparison.InvariantCultureIgnoreCase) + response.Text.Contains("John", StringComparison.InvariantCultureIgnoreCase) .ShouldBeTrue(); - Console.WriteLine(response.Choices.LastOrDefault().Text); + Console.WriteLine(response.Text); } [Fact] @@ -74,7 +74,7 @@ public async Task ShouldWorkWith_BookStoreService() var message = new ChatMessage(ChatRole.User, "what is written on page 96 in the book 'damdamadum'"); var response = await chatClient.GetResponseAsync(message,options:chatOptions).ConfigureAwait(false); - response.Choices.LastOrDefault().Text.ShouldContain("damdamadum",Case.Insensitive); + response.Text.ShouldContain("damdamadum",Case.Insensitive); } [System.ComponentModel.Description("Get book page content")] diff --git a/tests/GenerativeAI.Microsoft.Tests/Microsoft_ChatClient_Tests.cs b/tests/GenerativeAI.Microsoft.Tests/Microsoft_ChatClient_Tests.cs index c3f2302..64833d7 100644 --- a/tests/GenerativeAI.Microsoft.Tests/Microsoft_ChatClient_Tests.cs +++ b/tests/GenerativeAI.Microsoft.Tests/Microsoft_ChatClient_Tests.cs @@ -119,8 +119,8 @@ public async Task ShouldReturnChatCompletionOnValidInput() // Assert result.ShouldNotBeNull(); - result.Choices.ShouldNotBeNull(); - Console.WriteLine(result.Choices[0].Text); + result.Text.ShouldNotBeNullOrWhiteSpace(); + Console.WriteLine(result.Text); Console.WriteLine("CompleteAsync returned a valid ChatCompletion when given valid input.");