mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-18 04:18:00 +00:00
Record what the tool conversation adds to each request
This commit is contained in:
4 files changed
+110
-18
No files matched your search
@@ -19,9 +19,13 @@ public sealed class AnthropicToolCallingAdapter(Model chatModel, IList<IMessageB
|
|||||||
{
|
{
|
||||||
private readonly List<IMessageBase> internalMessages = [];
|
private readonly List<IMessageBase> internalMessages = [];
|
||||||
private readonly List<AnthropicToolResultContent> pendingToolResults = [];
|
private readonly List<AnthropicToolResultContent> pendingToolResults = [];
|
||||||
|
private readonly List<string> recordedRequestTexts = [];
|
||||||
private readonly List<AnthropicTool> tools = runnableTools.Select(x => ProviderToolAdapters.ToAnthropicTool(x.Definition)).ToList();
|
private readonly List<AnthropicTool> tools = runnableTools.Select(x => ProviderToolAdapters.ToAnthropicTool(x.Definition)).ToList();
|
||||||
private AnthropicResponse? lastResponse;
|
private AnthropicResponse? lastResponse;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IReadOnlyList<string> RecordedRequestTexts => this.recordedRequestTexts;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<ToolCallingRound?> ExecuteRoundAsync(string? finalResponseInstruction, bool includeTools, CancellationToken token = default)
|
public async Task<ToolCallingRound?> ExecuteRoundAsync(string? finalResponseInstruction, bool includeTools, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
@@ -76,13 +80,30 @@ public sealed class AnthropicToolCallingAdapter(Model chatModel, IList<IMessageB
|
|||||||
// returned unchanged for the model to continue from them.
|
// returned unchanged for the model to continue from them.
|
||||||
//
|
//
|
||||||
this.internalMessages.Add(new AnthropicMessage([..this.lastResponse.Content]));
|
this.internalMessages.Add(new AnthropicMessage([..this.lastResponse.Content]));
|
||||||
|
|
||||||
|
//
|
||||||
|
// And they are counted exactly as they arrived, for the same reason: a thinking block is
|
||||||
|
// sent back whole, so what it costs is what it says, not what we could read out of it.
|
||||||
|
//
|
||||||
|
foreach (var contentBlock in this.lastResponse.Content)
|
||||||
|
this.recordedRequestTexts.Add(contentBlock.GetRawText());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void RecordToolResult(string callId, string content, bool isError = false) => this.pendingToolResults.Add(new AnthropicToolResultContent
|
public void RecordToolResult(string callId, string content, bool isError = false)
|
||||||
{
|
{
|
||||||
ToolUseId = callId,
|
this.pendingToolResults.Add(new AnthropicToolResultContent
|
||||||
Content = content,
|
{
|
||||||
IsError = isError,
|
ToolUseId = callId,
|
||||||
});
|
Content = content,
|
||||||
|
IsError = isError,
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
// Noted here rather than when the results are flushed into their message: the round they
|
||||||
|
// belong to is over, and whoever asks in the meantime has to see what it cost.
|
||||||
|
//
|
||||||
|
if (!string.IsNullOrWhiteSpace(content))
|
||||||
|
this.recordedRequestTexts.Add(content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -22,9 +22,13 @@ public sealed class ChatCompletionToolCallingAdapter<TRequest>(
|
|||||||
: IToolCallingProviderAdapter where TRequest : ChatCompletionAPIRequest
|
: IToolCallingProviderAdapter where TRequest : ChatCompletionAPIRequest
|
||||||
{
|
{
|
||||||
private readonly List<IMessageBase> internalMessages = [];
|
private readonly List<IMessageBase> internalMessages = [];
|
||||||
|
private readonly List<string> recordedRequestTexts = [];
|
||||||
private ChatCompletionResponseMessage? lastResponseMessage;
|
private ChatCompletionResponseMessage? lastResponseMessage;
|
||||||
private List<ChatCompletionToolCall> lastToolCalls = [];
|
private List<ChatCompletionToolCall> lastToolCalls = [];
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IReadOnlyList<string> RecordedRequestTexts => this.recordedRequestTexts;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<ToolCallingRound?> ExecuteRoundAsync(string? finalResponseInstruction, bool includeTools, CancellationToken token = default)
|
public async Task<ToolCallingRound?> ExecuteRoundAsync(string? finalResponseInstruction, bool includeTools, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
@@ -79,23 +83,55 @@ public sealed class ChatCompletionToolCallingAdapter<TRequest>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void RecordAssistantTurn() => this.internalMessages.Add(new AssistantToolCallMessage
|
public void RecordAssistantTurn()
|
||||||
{
|
{
|
||||||
Content = this.lastResponseMessage?.RawContent,
|
this.internalMessages.Add(new AssistantToolCallMessage
|
||||||
ReasoningContent = this.lastResponseMessage?.ReasoningContent,
|
{
|
||||||
ToolCalls = this.lastToolCalls,
|
Content = this.lastResponseMessage?.RawContent,
|
||||||
});
|
ReasoningContent = this.lastResponseMessage?.ReasoningContent,
|
||||||
|
ToolCalls = this.lastToolCalls,
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
// The text of the message, not the message: this adapter builds the message itself, so it
|
||||||
|
// knows which of its fields carry words rather than wire format. The name of a call travels
|
||||||
|
// with its arguments because the model is charged for both.
|
||||||
|
//
|
||||||
|
this.Record(this.lastResponseMessage?.Content);
|
||||||
|
this.Record(this.lastResponseMessage?.ReasoningContent);
|
||||||
|
foreach (var toolCall in this.lastToolCalls)
|
||||||
|
this.Record($"{toolCall.Function?.Name}{toolCall.Function?.Arguments}");
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Chat Completions has no error flag on a tool message, so a failure travels in the content
|
/// Chat Completions has no error flag on a tool message, so a failure travels in the content
|
||||||
/// like any other result.
|
/// like any other result.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public void RecordToolResult(string callId, string content, bool isError = false) => this.internalMessages.Add(new ToolResultMessage
|
public void RecordToolResult(string callId, string content, bool isError = false)
|
||||||
{
|
{
|
||||||
Content = content,
|
this.internalMessages.Add(new ToolResultMessage
|
||||||
ToolCallId = callId,
|
{
|
||||||
});
|
Content = content,
|
||||||
|
ToolCallId = callId,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.Record(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Notes one piece of text as part of what the next round sends.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Empty pieces are left out rather than noted as nothing. A round without text and a round
|
||||||
|
/// without reasoning are the normal case here, and a list of empty strings would be carried
|
||||||
|
/// through the whole counting for no answer it could change.
|
||||||
|
/// </remarks>
|
||||||
|
private void Record(string? text)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(text))
|
||||||
|
this.recordedRequestTexts.Add(text);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Normalizes the tool calls of one response.
|
/// Normalizes the tool calls of one response.
|
||||||
|
|||||||
@@ -16,8 +16,12 @@ public sealed class ResponsesToolCallingAdapter(Model chatModel, IList<object> b
|
|||||||
Func<ResponsesAPIRequest, CancellationToken, Task<ResponsesResponse?>> executeRequestAsync) : IToolCallingProviderAdapter
|
Func<ResponsesAPIRequest, CancellationToken, Task<ResponsesResponse?>> executeRequestAsync) : IToolCallingProviderAdapter
|
||||||
{
|
{
|
||||||
private readonly List<object> internalItems = [];
|
private readonly List<object> internalItems = [];
|
||||||
|
private readonly List<string> recordedRequestTexts = [];
|
||||||
private ResponsesResponse? lastResponse;
|
private ResponsesResponse? lastResponse;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IReadOnlyList<string> RecordedRequestTexts => this.recordedRequestTexts;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The tools offered to the model: the provider-native ones plus our local functions.
|
/// The tools offered to the model: the provider-native ones plus our local functions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -77,7 +81,17 @@ public sealed class ResponsesToolCallingAdapter(Model chatModel, IList<object> b
|
|||||||
// Every output item, not just the function calls: the API rejects a continuation whose
|
// Every output item, not just the function calls: the API rejects a continuation whose
|
||||||
// reasoning items are missing.
|
// reasoning items are missing.
|
||||||
foreach (var outputItem in this.lastResponse.Output)
|
foreach (var outputItem in this.lastResponse.Output)
|
||||||
|
{
|
||||||
this.internalItems.Add(outputItem);
|
this.internalItems.Add(outputItem);
|
||||||
|
|
||||||
|
//
|
||||||
|
// The item as it came in, because that is how it goes back out. Reading the text out
|
||||||
|
// of it would mean knowing every item type the API has, including the ones it gains
|
||||||
|
// later -- and a reasoning item nobody recognized would then cost nothing here while
|
||||||
|
// costing its tokens on the wire.
|
||||||
|
//
|
||||||
|
this.recordedRequestTexts.Add(outputItem.GetRawText());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -85,11 +99,17 @@ public sealed class ResponsesToolCallingAdapter(Model chatModel, IList<object> b
|
|||||||
/// The Responses API has no error flag on a function call output, so a failure travels in the
|
/// The Responses API has no error flag on a function call output, so a failure travels in the
|
||||||
/// output like any other result.
|
/// output like any other result.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public void RecordToolResult(string callId, string content, bool isError = false) => this.internalItems.Add(new ResponsesFunctionCallOutputItem
|
public void RecordToolResult(string callId, string content, bool isError = false)
|
||||||
{
|
{
|
||||||
CallId = callId,
|
this.internalItems.Add(new ResponsesFunctionCallOutputItem
|
||||||
Output = content,
|
{
|
||||||
});
|
CallId = callId,
|
||||||
|
Output = content,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(content))
|
||||||
|
this.recordedRequestTexts.Add(content);
|
||||||
|
}
|
||||||
|
|
||||||
private static IList<object> BuildEffectiveProviderTools(IList<object> providerTools, IReadOnlyList<(ToolDefinition Definition, IToolImplementation Implementation)> runnableTools)
|
private static IList<object> BuildEffectiveProviderTools(IList<object> providerTools, IReadOnlyList<(ToolDefinition Definition, IToolImplementation Implementation)> runnableTools)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -49,4 +49,19 @@ public interface IToolCallingProviderAdapter
|
|||||||
/// the others carry the failure in the content, which is where it has to be legible anyway.
|
/// the others carry the failure in the content, which is where it has to be legible anyway.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void RecordToolResult(string callId, string content, bool isError = false);
|
public void RecordToolResult(string callId, string content, bool isError = false);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The texts which everything recorded so far adds to the request of every following round.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Kept by the adapter rather than by the loop, because the adapter is the only place which
|
||||||
|
/// knows what actually travels. The loop hands over arguments and results and would count
|
||||||
|
/// those; what the Responses API additionally demands back -- its reasoning items -- never
|
||||||
|
/// passes through the loop at all, and a conversation whose largest part is invisible is the
|
||||||
|
/// very thing this is here to rule out.<br/><br/>
|
||||||
|
/// These texts exist for as long as the adapter does, which is one streaming call. Nothing of
|
||||||
|
/// this reaches the next request the user sends: the accumulated conversation goes away with
|
||||||
|
/// the adapter.
|
||||||
|
/// </remarks>
|
||||||
|
public IReadOnlyList<string> RecordedRequestTexts { get; }
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user