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
16 changes: 8 additions & 8 deletions src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,25 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string
}

/// <summary>
/// Transforms a <see cref="GenerateContentResponse"/> into a <see cref="ChatCompletion"/>.
/// Transforms a <see cref="GenerateContentResponse"/> into a <see cref="ChatResponse"/>.
/// </summary>
/// <param name="response">The response to process.</param>
/// <returns>A <see cref="ChatCompletion"/> object, or null if the response is invalid.</returns>
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)
};
}
Expand All @@ -162,20 +162,20 @@ public static EmbedContentRequest ToGeminiEmbedContentRequest(IEnumerable<string
/// </summary>
/// <param name="response">The response to convert.</param>
/// <returns>A configured <see cref="StreamingChatCompletionUpdate"/>.</returns>
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,
FinishReason = response?.Candidates?.FirstOrDefault()?.FinishReason == FinishReason.OTHER
? ChatFinishReason.Stop
: null,
RawRepresentation = response,
ResponseId = null,
Role = ToAbstractionRole(response?.Candidates?.FirstOrDefault()?.Content?.Role),
Text = response?.Text(),
Role = ToAbstractionRole(response?.Candidates?.FirstOrDefault()?.Content?.Role)
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<ProjectReference Include="..\GenerativeAI\GenerativeAI.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI" Version="9.1.0-preview.1.25064.3" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" />
</ItemGroup>

</Project>
6 changes: 2 additions & 4 deletions src/GenerativeAI.Microsoft/GenerativeAIChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Dispose()
}

/// <inheritdoc/>
public async Task<ChatCompletion> CompleteAsync(IList<ChatMessage> chatMessages, ChatOptions? options = null,
public async Task<ChatResponse> GetResponseAsync(IList<ChatMessage> chatMessages, ChatOptions? options = null,
CancellationToken cancellationToken = default)
{
if (chatMessages == null)
Expand All @@ -39,7 +39,7 @@ public async Task<ChatCompletion> CompleteAsync(IList<ChatMessage> chatMessages,
return response.ToChatCompletion() ?? throw new Exception("Failed to generate content");
}
/// <inheritdoc/>
public async IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(IList<ChatMessage> chatMessages,
public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IList<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = new CancellationToken())
{
Expand All @@ -60,6 +60,4 @@ public async IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAs
}
else return null;
}
/// <inheritdoc/>
public ChatClientMetadata Metadata { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task ShouldThrowArgumentNullExceptionWhenChatMessagesIsNull()
var client = new GenerativeAIChatClient(adapter);

// Act & Assert
await Should.ThrowAsync<ArgumentNullException>(async () => { await client.CompleteAsync(null!); });
await Should.ThrowAsync<ArgumentNullException>(async () => { await client.GetResponseAsync((string)null!); });
Console.WriteLine("CompleteAsync threw ArgumentNullException as expected when chatMessages was null.");
}

Expand All @@ -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();
Expand All @@ -140,7 +140,7 @@ public async Task ShouldThrowArgumentNullExceptionWhenChatMessagesIsNullForStrea
// Act & Assert
await Should.ThrowAsync<ArgumentNullException>(async () =>
{
await foreach (var _ in client.CompleteStreamingAsync(null!))
await foreach (var _ in client.GetStreamingResponseAsync((string)null!))
{
// Should never get here
Console.WriteLine(_.Text ?? "null");
Expand All @@ -162,8 +162,8 @@ public async Task ShouldReturnStreamOfMessagesOnValidInput()
};

// Act
var updates = new List<StreamingChatCompletionUpdate>();
await foreach (var update in client.CompleteStreamingAsync(messages))
var updates = new List<ChatResponseUpdate>();
await foreach (var update in client.GetStreamingResponseAsync(messages))
{
updates.Add(update);
Console.WriteLine(update.Text ?? "null");
Expand Down Expand Up @@ -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<ChatClientMetadata>().ShouldBeNull();
Console.WriteLine("By default, metadata is null in GenerativeAIChatClient.");
}

#endregion
Expand Down