mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 07:33:48 +00:00
Added status texts for prompt processing and reasoning to the chat (#1205)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
1 parent
6cdb0bc551
commit
28b88580a9
29 files changed
+504
-30
No files matched your search
@@ -0,0 +1,120 @@
|
||||
using AIStudio.Chat;
|
||||
using AIStudio.Provider;
|
||||
|
||||
namespace AIStudio.Tests.Chat;
|
||||
|
||||
/// <summary>
|
||||
/// Checks when an answer tells the user that the model is thinking.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Some providers say that their model reasons, others send nothing to read for a while, and some models write
|
||||
/// their thoughts into think tags. All of them have to end in the same status, and the status has to go however
|
||||
/// the stream ends.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class ContentTextThinkingTests
|
||||
{
|
||||
private const string MODEL_ID = "test-model";
|
||||
|
||||
[Test]
|
||||
public void NothingThinksBeforeTheFirstChunk()
|
||||
{
|
||||
var answer = new ContentText { InitialRemoteWait = true };
|
||||
Assert.That(answer.IsThinking, Is.False, "Until the first chunk, the chat says that it processes the prompt.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReasoningLastsUntilTheFirstText()
|
||||
{
|
||||
var answer = new ContentText { InitialRemoteWait = true };
|
||||
|
||||
Apply(answer, Reasoning());
|
||||
var whileReasoning = answer.IsThinking;
|
||||
|
||||
Apply(answer, new ContentStreamChunk(string.Empty, []));
|
||||
var afterAChunkWithoutText = answer.IsThinking;
|
||||
|
||||
Apply(answer, new ContentStreamChunk("The answer.", []));
|
||||
var afterTheText = answer.IsThinking;
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(whileReasoning, Is.True);
|
||||
Assert.That(afterAChunkWithoutText, Is.True, "A chunk without text, such as one with the usage, does not end the reasoning.");
|
||||
Assert.That(afterTheText, Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AChunkWithoutTextLetsTheModelThink()
|
||||
{
|
||||
var answer = new ContentText { InitialRemoteWait = true };
|
||||
Apply(answer, new ContentStreamChunk(string.Empty, []));
|
||||
|
||||
Assert.That(answer.IsThinking, Is.True, "A provider which does not say that its model reasons still sends nothing to read yet.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThinkTagsLetTheModelThinkUntilTheAnswer()
|
||||
{
|
||||
var answer = new ContentText { InitialRemoteWait = true };
|
||||
|
||||
Apply(answer, new ContentStreamChunk("<think>Let me", []));
|
||||
var whileTheBlockIsOpen = answer.IsThinking;
|
||||
|
||||
Apply(answer, new ContentStreamChunk(" consider this.</think>", []));
|
||||
var afterTheBlock = answer.IsThinking;
|
||||
|
||||
Apply(answer, new ContentStreamChunk("\n\n", []));
|
||||
var afterTheLineBreaks = answer.IsThinking;
|
||||
|
||||
Apply(answer, new ContentStreamChunk("The answer.", []));
|
||||
var afterTheAnswer = answer.IsThinking;
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(whileTheBlockIsOpen, Is.True);
|
||||
Assert.That(afterTheBlock, Is.True, "A closed think block is no answer yet.");
|
||||
Assert.That(afterTheLineBreaks, Is.True, "Neither are the line breaks after it.");
|
||||
Assert.That(afterTheAnswer, Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReasoningLaterInTheAnswerShowsAgain()
|
||||
{
|
||||
var answer = new ContentText { InitialRemoteWait = true };
|
||||
|
||||
Apply(answer, new ContentStreamChunk("Let me look that up.", []));
|
||||
var whileWriting = answer.IsThinking;
|
||||
|
||||
Apply(answer, Reasoning());
|
||||
var whileReasoningAgain = answer.IsThinking;
|
||||
|
||||
Apply(answer, new ContentStreamChunk(" Here it is.", []));
|
||||
var afterMoreText = answer.IsThinking;
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(whileWriting, Is.False);
|
||||
Assert.That(whileReasoningAgain, Is.True, "After a tool, the model may reason again before it goes on writing.");
|
||||
Assert.That(afterMoreText, Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheEndOfTheStreamEndsTheThinking()
|
||||
{
|
||||
var answer = new ContentText { InitialRemoteWait = true };
|
||||
Apply(answer, Reasoning());
|
||||
|
||||
// The job service of the chat ends a stream this way, without a word about reasoning:
|
||||
answer.IsStreaming = false;
|
||||
|
||||
Assert.That(answer.IsThinking, Is.False);
|
||||
}
|
||||
|
||||
private static ContentStreamChunk Reasoning() => new(string.Empty, []) { IsReasoning = true };
|
||||
|
||||
private static void Apply(ContentText answer, ContentStreamChunk chunk) => answer.ApplyStreamChunk(chunk, MODEL_ID, blockCount: 2);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using AIStudio.Chat;
|
||||
|
||||
namespace AIStudio.Tests.Chat;
|
||||
|
||||
/// <summary>
|
||||
/// Checks how the chat tells the answer of a model apart from the think block which the model may open it with.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The chat asks with every chunk of an answer whether there is anything to read yet, to tell the user that the
|
||||
/// model is thinking until then. It must come to the same result as the text which the chat shows.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class ThinkTagsTests
|
||||
{
|
||||
[TestCase("", ExpectedResult = false)]
|
||||
[TestCase(" \n", ExpectedResult = false)]
|
||||
[TestCase("The answer.", ExpectedResult = true)]
|
||||
[TestCase("<think>Let me consider", ExpectedResult = false, Description = "An open think block is all thinking.")]
|
||||
[TestCase("<think>Let me consider this.</think>", ExpectedResult = false)]
|
||||
[TestCase("<think>Let me consider this.</think>\n\n", ExpectedResult = false, Description = "The line breaks after the block are no answer yet.")]
|
||||
[TestCase("<think>Let me consider this.</think>\n\nThe answer.", ExpectedResult = true)]
|
||||
public bool TellsWhetherThereIsAnythingToRead(string text) => text.HasTextBeyondThinkTags;
|
||||
|
||||
[TestCase("", ExpectedResult = "")]
|
||||
[TestCase("The answer.", ExpectedResult = "The answer.")]
|
||||
[TestCase("<think>Let me consider", ExpectedResult = "")]
|
||||
[TestCase("<think>Let me consider this.</think>\n\nThe answer.", ExpectedResult = "\n\nThe answer.")]
|
||||
public string KeepsOnlyWhatFollowsTheThinkBlock(string text) => text.RemoveThinkTags();
|
||||
|
||||
[Test]
|
||||
public void AnAnswerWithoutThinkBlockIsNotCopied()
|
||||
{
|
||||
const string ANSWER = "The answer.";
|
||||
Assert.That(ANSWER.RemoveThinkTags(), Is.SameAs(ANSWER), "The chat removes the think tags with every render of a streamed answer.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Text.Json;
|
||||
|
||||
using AIStudio.Provider;
|
||||
using AIStudio.Provider.OpenAI;
|
||||
|
||||
using AnthropicResponseStreamLine = AIStudio.Provider.Anthropic.ResponseStreamLine;
|
||||
|
||||
namespace AIStudio.Tests.Provider;
|
||||
|
||||
/// <summary>
|
||||
/// Checks that the lines of a stream without tools say when the model reasons, so that the chat can tell the user.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The stream with tools reads the same lines through its accumulators, which have tests of their own. Both have to
|
||||
/// agree: whether a chat may use tools must not change what the user reads while the model thinks.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class ReasoningSignalTests
|
||||
{
|
||||
[Test]
|
||||
public void AThinkingDeltaOfAnthropicSaysThatTheModelReasons()
|
||||
{
|
||||
var line = ReadAnthropic("""{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"Let me consider this."}}""");
|
||||
var chunk = line.GetContent();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(line.ContainsContent(), Is.True, "The line has to reach the chat, although it carries no text.");
|
||||
Assert.That(chunk.IsReasoning, Is.True);
|
||||
Assert.That(chunk.Content, Is.Empty, "The thinking itself does not show.");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ATextDeltaOfAnthropicIsNoReasoning()
|
||||
{
|
||||
var chunk = ReadAnthropic("""{"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"The answer."}}""").GetContent();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(chunk.IsReasoning, Is.False);
|
||||
Assert.That(chunk.Content, Is.EqualTo("The answer."));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReasoningContentOfAChatCompletionSaysThatTheModelReasons()
|
||||
{
|
||||
var chunk = ReadChatCompletion("""{"id":"chatcmpl-1","object":"chat.completion.chunk","created":1,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"reasoning_content":"Thinking about it."}}]}""");
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(chunk.IsReasoning, Is.True);
|
||||
Assert.That(chunk.Content, Is.Empty, "The reasoning itself does not show.");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ContentOfAChatCompletionIsNoReasoning()
|
||||
{
|
||||
var chunk = ReadChatCompletion("""{"id":"chatcmpl-1","object":"chat.completion.chunk","created":1,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"content":"The answer."}}]}""");
|
||||
Assert.That(chunk.IsReasoning, Is.False);
|
||||
}
|
||||
|
||||
private static AnthropicResponseStreamLine ReadAnthropic(string data) => JsonSerializer.Deserialize<AnthropicResponseStreamLine>(data, ProviderJsonOptions.OPTIONS);
|
||||
|
||||
private static ContentStreamChunk ReadChatCompletion(string data) => JsonSerializer.Deserialize<ChatCompletionDeltaStreamLine>(data, ProviderJsonOptions.OPTIONS)!.GetContent();
|
||||
}
|
||||
@@ -63,6 +63,25 @@ public sealed class AnthropicMessageStreamAccumulatorTests
|
||||
Assert.That(shown, Is.False, "What the model thinks stays between it and the next round.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThinkingSaysThatTheModelReasons()
|
||||
{
|
||||
var accumulator = new AnthropicMessageStreamAccumulator();
|
||||
var parts = Lines(
|
||||
"""{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}""",
|
||||
"""{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"Let me consider this."}}""",
|
||||
"""{"type":"content_block_start","index":1,"content_block":{"type":"text","text":""}}""",
|
||||
"""{"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"The answer."}}""")
|
||||
.Select(accumulator.Process)
|
||||
.ToList();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(parts[1].IsReasoning, Is.True, "A thinking delta tells the user why no text arrives yet.");
|
||||
Assert.That(parts[3].IsReasoning, Is.False, "Text is the answer, not reasoning.");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AThinkingBlockKeepsItsSignature()
|
||||
{
|
||||
|
||||
@@ -131,6 +131,24 @@ public sealed class ChatCompletionToolCallAccumulatorTests
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReasoningSaysThatTheModelReasons()
|
||||
{
|
||||
var accumulator = new ChatCompletionToolCallAccumulator();
|
||||
var parts = Lines(
|
||||
"""{"choices":[{"index":0,"delta":{"reasoning_content":"Thinking about it."}}]}""",
|
||||
"""{"choices":[{"index":0,"delta":{"content":"The answer."}}]}""")
|
||||
.Select(accumulator.Process)
|
||||
.ToList();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(parts[0].IsReasoning, Is.True, "A reasoning delta tells the user why no text arrives yet.");
|
||||
Assert.That(parts[0].HasContent, Is.False, "The reasoning itself still does not show.");
|
||||
Assert.That(parts[1].IsReasoning, Is.False, "Text is the answer, not reasoning.");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AToolWhichTakesNothingGetsAnEmptyObject()
|
||||
{
|
||||
|
||||
@@ -46,6 +46,28 @@ public sealed class ResponsesStreamAccumulatorTests
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnOpeningReasoningItemSaysThatTheModelReasons()
|
||||
{
|
||||
var accumulator = new ResponsesStreamAccumulator();
|
||||
var parts = Lines(
|
||||
"""{"type":"response.output_item.added","output_index":0,"item":{"id":"rs_1","type":"reasoning","summary":[]}}""",
|
||||
"""{"type":"response.reasoning_summary_text.delta","item_id":"rs_1","delta":"Weighing the options."}""",
|
||||
"""{"type":"response.output_item.added","output_index":1,"item":{"id":"msg_1","type":"message","content":[]}}""",
|
||||
"""{"type":"response.output_text.delta","delta":"The answer."}""")
|
||||
.Select(accumulator.Process)
|
||||
.ToList();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(parts[0].IsReasoning, Is.True, "The opening of a reasoning item is all the API says about reasoning it keeps to itself.");
|
||||
Assert.That(parts[1].IsReasoning, Is.True, "A summary of the reasoning says the same.");
|
||||
Assert.That(parts[1].HasContent, Is.False, "The reasoning itself does not show.");
|
||||
Assert.That(parts[2].IsReasoning, Is.False, "A message which opens is not reasoning.");
|
||||
Assert.That(parts[3].IsReasoning, Is.False, "Text is the answer, not reasoning.");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheTextIsShownWhileItIsBeingWritten()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user