Skip to content
Open
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 @@ -390,7 +390,12 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
}
else if (candidate.FinishReason.HasValue)
{
var text = candidate.FinishMessage ?? candidate.FinishReason.Value.ToString();
if (candidate.FinishReason == FinishReason.MAX_TOKENS)
{
_logger.LogWarning($"Action: {nameof(GetChatCompletionsStreamingAsync)}, Reason: {candidate.FinishReason}, Agent: {agent.Name}, MaxOutputTokens: {request.GenerationConfig?.MaxOutputTokens}, Content:{textStream.GetText()}");
}

var text = ProviderHelper.ResolveStreamedContent(candidate.FinishReason.Value, candidate.FinishMessage);
responseMessage = new RoleDialogModel(AgentRole.Assistant, text)
{
CurrentAgentId = agent.Id,
Expand Down
16 changes: 16 additions & 0 deletions src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
using GenerativeAI.Types;

namespace BotSharp.Plugin.GoogleAi.Providers;

public static class ProviderHelper
{
public const string MaxTokensMessage = "AI response exceeded max output length";

/// <summary>
/// Content saved when a streamed Gemini reply ends for a reason other than STOP.
/// MAX_TOKENS uses the same notice as the non-streaming path; every other reason
/// keeps FinishMessage, or the reason name when FinishMessage is absent.
/// </summary>
public static string ResolveStreamedContent(FinishReason reason, string? finishMessage)
{
return reason == FinishReason.MAX_TOKENS
? MaxTokensMessage
: finishMessage ?? reason.ToString();
}

public static GenerativeAI.GoogleAi GetGeminiClient(string provider, string model, IServiceProvider services)
{
var aiSettings = services.GetRequiredService<GoogleAiSettings>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using BotSharp.Plugin.GoogleAi.Providers;
using GenerativeAI.Types;
using Xunit;

namespace BotSharp.Core.UnitTests.Plugins;

public class GoogleAiStreamedContentTests
{
// Streaming used to save FinishMessage, or the reason name when it was absent.
// The non-streaming path shows a notice for the same MAX_TOKENS case.
[Theory]
[InlineData("partial answer")]
[InlineData("")]
[InlineData(null)]
public void ResolveStreamedContent_MaxTokens_ReturnsSameNoticeAsNonStreaming(string? finishMessage)
{
Assert.Equal("AI response exceeded max output length",
ProviderHelper.ResolveStreamedContent(FinishReason.MAX_TOKENS, finishMessage));
}

[Theory]
[InlineData("blocked by safety")]
[InlineData("")]
[InlineData(null)]
public void ResolveStreamedContent_OtherReason_KeepsFinishMessageOrReasonName(string? finishMessage)
{
var expected = finishMessage ?? FinishReason.SAFETY.ToString();
Assert.Equal(expected, ProviderHelper.ResolveStreamedContent(FinishReason.SAFETY, finishMessage));
}
}
Loading