Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Gemini - Removed limitation when gemini throws exception if invalid response (BREAKING CHANGE) #8584

Merged
merged 6 commits into from
Sep 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public GeminiChatGenerationTests()
this._httpClient = new HttpClient(this._messageHandlerStub, false);
}

[Fact]
public async Task ShouldReturnEmptyMessageContentAndNullMetadataIfEmptyJsonInResponseAsync()
{
// Arrange
this._messageHandlerStub.ResponseToReturn.Content = new StringContent("{}");
var client = this.CreateChatCompletionClient();
var chatHistory = CreateSampleChatHistory();

// Act
var messages = await client.GenerateChatMessageAsync(chatHistory);

// Assert
Assert.Single(messages, item =>
item.Role == AuthorRole.Assistant &&
string.IsNullOrEmpty(item.Content) &&
item.Metadata == null);
}

[Fact]
public async Task ShouldReturnEmptyMessageContentIfNoContentInResponseAsync()
{
Expand Down Expand Up @@ -417,12 +435,12 @@ public async Task ItCanUseValueTasksSequentiallyForBearerTokenAsync()
using var httpClient = new HttpClient(multipleMessageHandlerStub, false);

var client = new GeminiChatCompletionClient(
httpClient: httpClient,
modelId: "fake-model",
apiVersion: VertexAIVersion.V1,
bearerTokenProvider: () => bearerTokenGenerator.GetBearerToken(),
location: "fake-location",
projectId: "fake-project-id");
httpClient: httpClient,
modelId: "fake-model",
apiVersion: VertexAIVersion.V1,
bearerTokenProvider: () => bearerTokenGenerator.GetBearerToken(),
location: "fake-location",
projectId: "fake-project-id");

var chatHistory = CreateSampleChatHistory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public GeminiChatStreamingTests()
this._httpClient = new HttpClient(this._messageHandlerStub, false);
}

[Fact]
public async Task ShouldReturnEmptyMessageContentAndNullMetadataIfEmptyJsonInResponseAsync()
{
// Arrange
this._messageHandlerStub.ResponseToReturn.Content = new StringContent("{}");
var client = this.CreateChatCompletionClient();
var chatHistory = CreateSampleChatHistory();

// Act
var messages = await client.StreamGenerateChatMessageAsync(chatHistory).ToListAsync();

// Assert
Assert.Single(messages, item =>
item.Role == AuthorRole.Assistant &&
string.IsNullOrEmpty(item.Content) &&
item.Metadata == null);
}

[Fact]
public async Task ShouldReturnEmptyMessageContentIfNoContentInResponseAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,10 @@ private List<GeminiChatMessageContent> ProcessChatResponse(GeminiResponse gemini

private static void ValidateGeminiResponse(GeminiResponse geminiResponse)
{
if (geminiResponse.Candidates is null || geminiResponse.Candidates.Count == 0)
if (geminiResponse.PromptFeedback?.BlockReason is not null)
{
if (geminiResponse.PromptFeedback?.BlockReason is not null)
{
// TODO: Currently SK doesn't support prompt feedback/finish status, so we just throw an exception. I told SK team that we need to support it: https://github.com/microsoft/semantic-kernel/issues/4621
throw new KernelException("Prompt was blocked due to Gemini API safety reasons.");
}

throw new KernelException("Gemini API doesn't return any data.");
// TODO: Currently SK doesn't support prompt feedback/finish status, so we just throw an exception. I told SK team that we need to support it: https://github.com/microsoft/semantic-kernel/issues/4621
throw new KernelException("Prompt was blocked due to Gemini API safety reasons.");
}
}

Expand Down Expand Up @@ -596,7 +591,9 @@ private void LogUsage(List<GeminiChatMessageContent> chatMessageContents)
}

private List<GeminiChatMessageContent> GetChatMessageContentsFromResponse(GeminiResponse geminiResponse)
=> geminiResponse.Candidates!.Select(candidate => this.GetChatMessageContentFromCandidate(geminiResponse, candidate)).ToList();
=> geminiResponse.Candidates == null ?
[new GeminiChatMessageContent(role: AuthorRole.Assistant, content: string.Empty, modelId: this._modelId)]
: geminiResponse.Candidates.Select(candidate => this.GetChatMessageContentFromCandidate(geminiResponse, candidate)).ToList();

private GeminiChatMessageContent GetChatMessageContentFromCandidate(GeminiResponse geminiResponse, GeminiResponseCandidate candidate)
{
Expand Down Expand Up @@ -630,7 +627,7 @@ private GeminiStreamingChatMessageContent GetStreamingChatContentFromChatContent
modelId: this._modelId,
calledToolResult: message.CalledToolResult,
metadata: message.Metadata,
choiceIndex: message.Metadata!.Index);
choiceIndex: message.Metadata?.Index ?? 0);
}

if (message.ToolCalls is not null)
Expand All @@ -641,14 +638,14 @@ private GeminiStreamingChatMessageContent GetStreamingChatContentFromChatContent
modelId: this._modelId,
toolCalls: message.ToolCalls,
metadata: message.Metadata,
choiceIndex: message.Metadata!.Index);
choiceIndex: message.Metadata?.Index ?? 0);
}

return new GeminiStreamingChatMessageContent(
role: message.Role,
content: message.Content,
modelId: this._modelId,
choiceIndex: message.Metadata!.Index,
choiceIndex: message.Metadata?.Index ?? 0,
metadata: message.Metadata);
}

Expand Down
Loading