Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -238,7 +238,6 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string
{
FinishReason = ToFinishReason(response.Candidates?.FirstOrDefault()?.FinishReason),
AdditionalProperties = null,
Choices = new[] { chatMessage }.ToList(),
CreatedAt = null,
ModelId = null,
RawRepresentation = response,
Expand All @@ -259,7 +258,6 @@ public static ChatResponseUpdate ToChatResponseUpdate(this GenerateContentRespon
return new ChatResponseUpdate
{
Contents = response.Candidates.Select(s => s.Content).SelectMany(s => s.Parts).ToList().ToAiContents(),
ChoiceIndex = 0,
AdditionalProperties = null,
FinishReason = response?.Candidates?.FirstOrDefault()?.FinishReason == FinishReason.OTHER
? ChatFinishReason.Stop
Expand Down Expand Up @@ -462,7 +460,7 @@ public static IList<AIContent> ToAiContents(this List<Part>? 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;
}
}
2 changes: 1 addition & 1 deletion src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<ProjectReference Include="..\GenerativeAI\GenerativeAI.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25161.3" />
</ItemGroup>

</Project>
16 changes: 8 additions & 8 deletions src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public void Dispose()
}

/// <inheritdoc/>
public async Task<ChatResponse> GetResponseAsync(IList<ChatMessage> chatMessages, ChatOptions? options = null,
public async Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> 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,
Expand Down Expand Up @@ -96,13 +96,13 @@ private async Task<ChatResponse> CallFunctionAsync(GenerateContentRequest reques
}

/// <inheritdoc/>
public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IList<ChatMessage> chatMessages,
public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> 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();
Expand Down
6 changes: 3 additions & 3 deletions tests/AotTest/MEAITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -241,7 +240,6 @@ public void ToAiContents_WithInlineDataPart_ReturnsDataContent()
result.FirstOrDefault().ShouldBeOfType<DataContent>();
var dataContent = (DataContent)result.FirstOrDefault();
dataContent.MediaType.ShouldBe("image/png");
dataContent.Data.ShouldNotBeNull();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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]
Expand All @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
Loading