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
@@ -23,7 +23,13 @@ public sealed class AnthropicContentBlockBuilder
|
||||
|
||||
private const string DELTA_TEXT = "text_delta";
|
||||
private const string DELTA_INPUT_JSON = "input_json_delta";
|
||||
private const string DELTA_THINKING = "thinking_delta";
|
||||
/// <summary>
|
||||
/// The type of a delta which carries thinking of the model.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Both ways of streaming ask for it to tell that the model is thinking, cf. ResponseStreamLine.
|
||||
/// </remarks>
|
||||
internal const string DELTA_THINKING = "thinking_delta";
|
||||
private const string DELTA_SIGNATURE = "signature_delta";
|
||||
|
||||
private const string EMPTY_OBJECT = "{}";
|
||||
|
||||
@@ -22,7 +22,7 @@ public sealed class AnthropicMessageStreamAccumulator
|
||||
private const string EVENT_MESSAGE_STOP = "message_stop";
|
||||
|
||||
private const string DELTA_TEXT = "text_delta";
|
||||
|
||||
|
||||
private readonly Dictionary<int, AnthropicContentBlockBuilder> openBlocks = [];
|
||||
private readonly SortedDictionary<int, JsonElement> finishedBlocks = [];
|
||||
private readonly Dictionary<string, string> unparsableToolArguments = [];
|
||||
@@ -81,7 +81,7 @@ public sealed class AnthropicMessageStreamAccumulator
|
||||
this.openBlocks[line.Index] = openBlock;
|
||||
}
|
||||
|
||||
return new AnthropicStreamPart(openBlock.Append(line.Delta));
|
||||
return new AnthropicStreamPart(openBlock.Append(line.Delta)) { IsReasoning = line.Delta.Type is AnthropicContentBlockBuilder.DELTA_THINKING };
|
||||
|
||||
case EVENT_BLOCK_STOP:
|
||||
if (this.openBlocks.Remove(line.Index, out var finishedBlock))
|
||||
|
||||
@@ -24,4 +24,12 @@ public readonly record struct AnthropicStreamPart(string TextDelta, TokenUsage U
|
||||
/// all is the tool calling loop's decision, which knows which round this is.
|
||||
/// </remarks>
|
||||
public bool HasContent => this.TextDelta.Length > 0;
|
||||
|
||||
/// <summary>
|
||||
/// Whether this line carried thinking of the model, see ContentStreamChunk.IsReasoning.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The thinking itself still does not show; only that it happens does.
|
||||
/// </remarks>
|
||||
public bool IsReasoning { get; init; }
|
||||
}
|
||||
@@ -66,8 +66,8 @@ public sealed class AnthropicToolCallingAdapter(Model chatModel, IList<IMessageB
|
||||
await foreach (var serverSentEvent in streamRequestAsync(request, streamEnd, token))
|
||||
{
|
||||
var part = accumulator.Process(serverSentEvent);
|
||||
if (part.HasContent || part.Usage.IsKnown)
|
||||
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, [], Usage: part.Usage));
|
||||
if (part.HasContent || part.Usage.IsKnown || part.IsReasoning)
|
||||
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, [], Usage: part.Usage) { IsReasoning = part.IsReasoning });
|
||||
}
|
||||
|
||||
// A stream which was cut off ends the round without an outcome, cf. IToolCallingProviderAdapter.ExecuteRoundAsync:
|
||||
|
||||
@@ -19,10 +19,19 @@ public readonly record struct ResponseStreamLine(string Type, int Index, Delta D
|
||||
public AnthropicStreamMessage? Message { get; init; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool ContainsContent() => this != default && !string.IsNullOrWhiteSpace(this.Delta.Text);
|
||||
public bool ContainsContent() => this != default && (!string.IsNullOrWhiteSpace(this.Delta.Text) || this.IsThinking);
|
||||
|
||||
/// <inheritdoc />
|
||||
public ContentStreamChunk GetContent() => new(this.Delta.Text, []);
|
||||
public ContentStreamChunk GetContent() => new(this.Delta.Text ?? string.Empty, []) { IsReasoning = this.IsThinking };
|
||||
|
||||
/// <summary>
|
||||
/// Whether this line carries thinking of the model, which says that it is reasoning.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Asks the type of the delta, as the stream with tools does, cf. AnthropicMessageStreamAccumulator. The thinking
|
||||
/// itself is never read here, so the delta needs no field for it.
|
||||
/// </remarks>
|
||||
private bool IsThinking => this.Delta.Type is AnthropicContentBlockBuilder.DELTA_THINKING;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <remarks>
|
||||
|
||||
@@ -1304,6 +1304,27 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
// Yield the response:
|
||||
yield return new(string.Empty, providerResponse.GetSources());
|
||||
}
|
||||
|
||||
//
|
||||
// Find the lines which say that the model reasons: the opening of a reasoning item,
|
||||
// which is all the API tells about reasoning it keeps to itself, and the reasoning
|
||||
// which does arrive as text. None of it is shown; the chunk only says that it happens.
|
||||
//
|
||||
else if (serverSentEvent.Data.StartsWith(
|
||||
"""
|
||||
{"type":"response.reasoning
|
||||
""", StringComparison.InvariantCulture) ||
|
||||
(serverSentEvent.Data.StartsWith(
|
||||
"""
|
||||
{"type":"response.output_item.added"
|
||||
""", StringComparison.InvariantCulture) &&
|
||||
serverSentEvent.Data.Contains(
|
||||
"""
|
||||
"type":"reasoning"
|
||||
""", StringComparison.InvariantCulture)))
|
||||
{
|
||||
yield return new(string.Empty, []) { IsReasoning = true };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,16 @@ public sealed record ContentStreamChunk(string Content, IList<ISource> Sources,
|
||||
/// </remarks>
|
||||
public IReadOnlyList<SourcePassage> Passages { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Whether the model was reasoning when this chunk arrived.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The reasoning itself never travels with the chunk; nothing shows it. The chunk only says
|
||||
/// that it happens, so that the user reads why no text arrives yet. A provider which hides
|
||||
/// its reasoning inside think tags sets nothing here: that text arrives as content.
|
||||
/// </remarks>
|
||||
public bool IsReasoning { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Tells whether the model ended its turn without an answer, so that the content of the chunk is a notice of the
|
||||
/// app rather than a word of the model.
|
||||
|
||||
@@ -13,4 +13,12 @@ public sealed record ChatCompletionDelta
|
||||
|
||||
[JsonIgnore]
|
||||
public string Content => ChatCompletionContent.GetText(this.RawContent) ?? string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The reasoning text some providers stream next to the answer.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Read only to tell that the model is reasoning, see ContentStreamChunk.IsReasoning.
|
||||
/// </remarks>
|
||||
public string? ReasoningContent { get; set; }
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public record ChatCompletionDeltaStreamLine(string Id, string Object, uint Creat
|
||||
public bool ContainsContent() => this.Choices.Count > 0;
|
||||
|
||||
/// <inheritdoc />
|
||||
public ContentStreamChunk GetContent() => new(this.Choices[0].Delta.Content, []);
|
||||
public ContentStreamChunk GetContent() => new(this.Choices[0].Delta.Content, []) { IsReasoning = !string.IsNullOrEmpty(this.Choices[0].Delta.ReasoningContent) };
|
||||
|
||||
/// <inheritdoc />
|
||||
public TokenUsage GetUsage() => this.Usage?.ToTokenUsage() ?? TokenUsage.UNKNOWN;
|
||||
|
||||
@@ -21,4 +21,9 @@ public readonly record struct ChatCompletionStreamPart(string TextDelta, IList<I
|
||||
/// all is the tool calling loop's decision, which knows which round this is.
|
||||
/// </remarks>
|
||||
public bool HasContent => this.TextDelta.Length > 0 || this.Sources.Count > 0;
|
||||
|
||||
/// <summary>
|
||||
/// Whether this line carried reasoning of the model, see ContentStreamChunk.IsReasoning.
|
||||
/// </summary>
|
||||
public bool IsReasoning { get; init; }
|
||||
}
|
||||
@@ -73,7 +73,8 @@ public sealed class ChatCompletionToolCallAccumulator(Func<ServerSentEvent, ILis
|
||||
|
||||
this.hasReadAnything = true;
|
||||
|
||||
if (!string.IsNullOrEmpty(delta.ReasoningContent))
|
||||
var isReasoning = !string.IsNullOrEmpty(delta.ReasoningContent);
|
||||
if (isReasoning)
|
||||
this.reasoning.Append(delta.ReasoningContent);
|
||||
|
||||
foreach (var toolCallDelta in delta.ToolCalls ?? [])
|
||||
@@ -86,10 +87,12 @@ public sealed class ChatCompletionToolCallAccumulator(Func<ServerSentEvent, ILis
|
||||
|
||||
var textDelta = delta.Content;
|
||||
if (textDelta.Length is 0)
|
||||
return WithSources(string.Empty, sources, usage);
|
||||
return isReasoning
|
||||
? new ChatCompletionStreamPart(string.Empty, sources, usage) { IsReasoning = true }
|
||||
: WithSources(string.Empty, sources, usage);
|
||||
|
||||
this.text.Append(textDelta);
|
||||
return new ChatCompletionStreamPart(textDelta, sources, usage);
|
||||
return new ChatCompletionStreamPart(textDelta, sources, usage) { IsReasoning = isReasoning };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -76,8 +76,8 @@ public sealed class ChatCompletionToolCallingAdapter<TRequest>(
|
||||
await foreach (var serverSentEvent in streamRequestAsync(requestDto, streamEnd, token))
|
||||
{
|
||||
var part = accumulator.Process(serverSentEvent);
|
||||
if (part.HasContent || part.Usage.IsKnown)
|
||||
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage));
|
||||
if (part.HasContent || part.Usage.IsKnown || part.IsReasoning)
|
||||
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage) { IsReasoning = part.IsReasoning });
|
||||
}
|
||||
|
||||
// A stream which was cut off ends the round without an outcome, cf. IToolCallingProviderAdapter.ExecuteRoundAsync:
|
||||
|
||||
@@ -19,7 +19,11 @@ public sealed class ResponsesStreamAccumulator
|
||||
private const string EVENT_TEXT_DELTA = "response.output_text.delta";
|
||||
private const string EVENT_ANNOTATION_ADDED = "response.output_text.annotation.added";
|
||||
private const string EVENT_OUTPUT_ITEM_DONE = "response.output_item.done";
|
||||
|
||||
private const string EVENT_OUTPUT_ITEM_ADDED = "response.output_item.added";
|
||||
private const string EVENT_REASONING_SUMMARY_DELTA = "response.reasoning_summary_text.delta";
|
||||
private const string EVENT_REASONING_DELTA = "response.reasoning_text.delta";
|
||||
private const string ITEM_TYPE_REASONING = "reasoning";
|
||||
|
||||
private readonly List<JsonElement> completedOutputItems = [];
|
||||
private ResponsesResponse? completedResponse;
|
||||
|
||||
@@ -51,6 +55,18 @@ public sealed class ResponsesStreamAccumulator
|
||||
//
|
||||
if (eventType is EVENT_OUTPUT_ITEM_DONE && root.TryGetProperty("item", out var outputItem))
|
||||
this.completedOutputItems.Add(outputItem.Clone());
|
||||
|
||||
//
|
||||
// The API mostly keeps its reasoning to itself: all it says is that a reasoning item
|
||||
// opened, and then nothing until the item is done. That opening is the signal.
|
||||
//
|
||||
if (eventType is EVENT_OUTPUT_ITEM_ADDED &&
|
||||
root.TryGetProperty("item", out var addedItem) &&
|
||||
addedItem.ValueKind is JsonValueKind.Object &&
|
||||
addedItem.TryGetProperty("type", out var addedItemType) &&
|
||||
addedItemType.ValueKind is JsonValueKind.String &&
|
||||
addedItemType.GetString() is ITEM_TYPE_REASONING)
|
||||
return ResponsesStreamPart.Reasoning;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
@@ -84,7 +100,11 @@ public sealed class ResponsesStreamAccumulator
|
||||
return ResponsesStreamPart.Nothing;
|
||||
|
||||
return new ResponsesStreamPart(string.Empty, annotationLine.GetSources());
|
||||
|
||||
|
||||
// Reasoning which does arrive as text, as a summary or from an open model:
|
||||
case EVENT_REASONING_SUMMARY_DELTA or EVENT_REASONING_DELTA:
|
||||
return ResponsesStreamPart.Reasoning;
|
||||
|
||||
default:
|
||||
return ResponsesStreamPart.Nothing;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,11 @@ public readonly record struct ResponsesStreamPart(string TextDelta, IList<ISourc
|
||||
/// </summary>
|
||||
public static ResponsesStreamPart Nothing => new(string.Empty, []);
|
||||
|
||||
/// <summary>
|
||||
/// The part of a line which says that the model reasons, and nothing else.
|
||||
/// </summary>
|
||||
public static ResponsesStreamPart Reasoning => new(string.Empty, []) { IsReasoning = true };
|
||||
|
||||
/// <summary>
|
||||
/// Whether this part has anything to show at all.
|
||||
/// </summary>
|
||||
@@ -21,4 +26,9 @@ public readonly record struct ResponsesStreamPart(string TextDelta, IList<ISourc
|
||||
/// all is the tool calling loop's decision, which knows which round this is.
|
||||
/// </remarks>
|
||||
public bool HasContent => this.TextDelta.Length > 0 || this.Sources.Count > 0;
|
||||
|
||||
/// <summary>
|
||||
/// Whether this line says that the model reasons, see ContentStreamChunk.IsReasoning.
|
||||
/// </summary>
|
||||
public bool IsReasoning { get; init; }
|
||||
}
|
||||
@@ -64,8 +64,8 @@ public sealed class ResponsesToolCallingAdapter(Model chatModel, IList<object> b
|
||||
await foreach (var serverSentEvent in streamRequestAsync(request, streamEnd, token))
|
||||
{
|
||||
var part = accumulator.Process(serverSentEvent);
|
||||
if (part.HasContent || part.Usage.IsKnown)
|
||||
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage));
|
||||
if (part.HasContent || part.Usage.IsKnown || part.IsReasoning)
|
||||
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage) { IsReasoning = part.IsReasoning });
|
||||
}
|
||||
|
||||
// A stream which was cut off ends the round without an outcome, cf. IToolCallingProviderAdapter.ExecuteRoundAsync:
|
||||
|
||||
Reference in new issue
Block a user