diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs index 7ef687598..5898226b6 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -350,7 +350,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.AzureOpenAI/Providers/ProviderHelper.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs index 259a8dd2f..919c266dd 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs @@ -1,10 +1,22 @@ using Azure.AI.OpenAI; +using OpenAI.Chat; using System.ClientModel; namespace BotSharp.Plugin.AzureOpenAI.Providers; public 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 AzureOpenAIClient 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..83008c83a 100644 --- a/tests/BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj +++ b/tests/BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj @@ -47,6 +47,8 @@ + + diff --git a/tests/BotSharp.Core.UnitTests/Plugins/AzureOpenAiStreamedContentTests.cs b/tests/BotSharp.Core.UnitTests/Plugins/AzureOpenAiStreamedContentTests.cs new file mode 100644 index 000000000..24a9f4d49 --- /dev/null +++ b/tests/BotSharp.Core.UnitTests/Plugins/AzureOpenAiStreamedContentTests.cs @@ -0,0 +1,25 @@ +using BotSharp.Plugin.AzureOpenAI.Providers; +using OpenAI.Chat; +using Xunit; + +namespace BotSharp.Core.UnitTests.Plugins; + +public class AzureOpenAiStreamedContentTests +{ + [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)); + } +}