diff --git a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs index 8afe42097..fcf094338 100644 --- a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs @@ -317,7 +317,12 @@ public async Task GetChatCompletionsStreamingAsync(Agent agent, var allText = textStream.GetText(); _logger.LogCritical($"Text Content: {allText}"); - responseMessage = new RoleDialogModel(AgentRole.Assistant, allText) + if (choice.FinishReason == ChatFinishReason.Length) + { + _logger.LogWarning($"Action: {nameof(GetChatCompletionsStreamingAsync)}, Reason: {choice.FinishReason}, Agent: {agent.Name}, MaxOutputTokens: {options.MaxOutputTokenCount}, Content:{allText}"); + } + + responseMessage = new RoleDialogModel(AgentRole.Assistant, ProviderHelper.ResolveStreamedContent(choice.FinishReason.Value, allText)) { CurrentAgentId = agent.Id, MessageId = messageId, diff --git a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/ProviderHelper.cs b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/ProviderHelper.cs index acd6653be..1b1ac8a7f 100644 --- a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/ProviderHelper.cs +++ b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/ProviderHelper.cs @@ -1,10 +1,22 @@ using OpenAI; +using OpenAI.Chat; using System.ClientModel; namespace BotSharp.Plugin.DeepSeek.Providers; public static class ProviderHelper { + public const string MaxTokensMessage = "AI response exceeded max output length"; + + /// + /// Content of the final streamed message. A reply cut off by the output limit gets the same notice as the + /// non-streaming path instead of silently passing the truncated text off as a complete answer. + /// + public static string ResolveStreamedContent(ChatFinishReason reason, string collectedText) + { + return reason == ChatFinishReason.Length ? MaxTokensMessage : collectedText; + } + public static OpenAIClient GetClient(string provider, string model, IServiceProvider services) { var settingsService = services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/Chat/ChatCompletionProvider.cs index 3ea6b37ce..31a62c68c 100644 --- a/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/Chat/ChatCompletionProvider.cs @@ -317,7 +317,12 @@ public async Task GetChatCompletionsStreamingAsync(Agent agent, var allText = textStream.GetText(); _logger.LogCritical($"Text Content: {allText}"); - responseMessage = new RoleDialogModel(AgentRole.Assistant, allText) + if (choice.FinishReason == ChatFinishReason.Length) + { + _logger.LogWarning($"Action: {nameof(GetChatCompletionsStreamingAsync)}, Reason: {choice.FinishReason}, Agent: {agent.Name}, MaxOutputTokens: {options.MaxOutputTokenCount}, Content:{allText}"); + } + + responseMessage = new RoleDialogModel(AgentRole.Assistant, ProviderHelper.ResolveStreamedContent(choice.FinishReason.Value, allText)) { CurrentAgentId = agent.Id, MessageId = messageId, diff --git a/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/ProviderHelper.cs b/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/ProviderHelper.cs index 6a1cef5f8..4f6277299 100644 --- a/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/ProviderHelper.cs +++ b/src/Plugins/BotSharp.Plugin.LiteLLM/Providers/ProviderHelper.cs @@ -1,10 +1,22 @@ using OpenAI; +using OpenAI.Chat; using System.ClientModel; namespace BotSharp.Plugin.LiteLLM.Providers; public static class ProviderHelper { + public const string MaxTokensMessage = "AI response exceeded max output length"; + + /// + /// Content of the final streamed message. A reply cut off by the output limit gets the same notice as the + /// non-streaming path instead of silently passing the truncated text off as a complete answer. + /// + public static string ResolveStreamedContent(ChatFinishReason reason, string collectedText) + { + return reason == ChatFinishReason.Length ? MaxTokensMessage : collectedText; + } + public static OpenAIClient GetClient(string provider, string model, IServiceProvider services) { var settingsService = services.GetRequiredService(); diff --git a/tests/BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj b/tests/BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj index fc1b8c1c2..27439749e 100644 --- a/tests/BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj +++ b/tests/BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj @@ -51,6 +51,10 @@ + + + + diff --git a/tests/BotSharp.Core.UnitTests/Plugins/DeepSeekAIStreamedContentTests.cs b/tests/BotSharp.Core.UnitTests/Plugins/DeepSeekAIStreamedContentTests.cs new file mode 100644 index 000000000..372bb3fda --- /dev/null +++ b/tests/BotSharp.Core.UnitTests/Plugins/DeepSeekAIStreamedContentTests.cs @@ -0,0 +1,25 @@ +using BotSharp.Plugin.DeepSeek.Providers; +using OpenAI.Chat; +using Xunit; + +namespace BotSharp.Core.UnitTests.Plugins; + +public class DeepSeekAIStreamedContentTests +{ + [Theory] + [InlineData("partial answer")] + [InlineData("")] + public void ResolveStreamedContent_Stop_ReturnsCollectedText(string text) + { + Assert.Equal(text, ProviderHelper.ResolveStreamedContent(ChatFinishReason.Stop, text)); + } + + // Streaming used to hand back the truncated text as if it were a complete answer; the non-streaming path shows a notice. + [Theory] + [InlineData("partial answer")] + [InlineData("")] + public void ResolveStreamedContent_Length_ReturnsSameNoticeAsNonStreaming(string text) + { + Assert.Equal("AI response exceeded max output length", ProviderHelper.ResolveStreamedContent(ChatFinishReason.Length, text)); + } +} diff --git a/tests/BotSharp.Core.UnitTests/Plugins/LiteLLMStreamedContentTests.cs b/tests/BotSharp.Core.UnitTests/Plugins/LiteLLMStreamedContentTests.cs new file mode 100644 index 000000000..e6de76940 --- /dev/null +++ b/tests/BotSharp.Core.UnitTests/Plugins/LiteLLMStreamedContentTests.cs @@ -0,0 +1,25 @@ +using BotSharp.Plugin.LiteLLM.Providers; +using OpenAI.Chat; +using Xunit; + +namespace BotSharp.Core.UnitTests.Plugins; + +public class LiteLLMStreamedContentTests +{ + [Theory] + [InlineData("partial answer")] + [InlineData("")] + public void ResolveStreamedContent_Stop_ReturnsCollectedText(string text) + { + Assert.Equal(text, ProviderHelper.ResolveStreamedContent(ChatFinishReason.Stop, text)); + } + + // Streaming used to hand back the truncated text as if it were a complete answer; the non-streaming path shows a notice. + [Theory] + [InlineData("partial answer")] + [InlineData("")] + public void ResolveStreamedContent_Length_ReturnsSameNoticeAsNonStreaming(string text) + { + Assert.Equal("AI response exceeded max output length", ProviderHelper.ResolveStreamedContent(ChatFinishReason.Length, text)); + } +}