mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 22:14:27 +00:00
🧾 test: Bill Advertised Tool Definitions in the Mock Model's Input Tokens (#17897)
This commit is contained in:
parent
7bb115319f
commit
24322e6f21
1 changed files with 22 additions and 2 deletions
|
|
@ -347,6 +347,19 @@ function collectToolDefinitions(agents) {
|
|||
return definitions;
|
||||
}
|
||||
|
||||
/** Text a provider bills for one advertised tool: its name, description and JSON schema.
|
||||
* A zod schema (on a LangChain tool) is skipped, since serializing it reports the
|
||||
* validator's internals rather than the schema the provider receives. */
|
||||
function toolDefinitionText(name, definition) {
|
||||
const schema = definition?.parameters ?? definition?.input_schema ?? definition?.schema;
|
||||
const plainSchema =
|
||||
schema != null && typeof schema === 'object' && typeof schema.safeParse !== 'function'
|
||||
? JSON.stringify(schema)
|
||||
: '';
|
||||
const description = typeof definition?.description === 'string' ? definition.description : '';
|
||||
return `${name}${description}${plainSchema}`;
|
||||
}
|
||||
|
||||
function collectToolNames(agents) {
|
||||
return new Set(collectToolDefinitions(agents).keys());
|
||||
}
|
||||
|
|
@ -1012,15 +1025,22 @@ class UsageEmittingFakeChatModel extends FakeChatModel {
|
|||
outputChars += typeof chunk.text === 'string' ? chunk.text.length : 0;
|
||||
yield chunk;
|
||||
}
|
||||
const { messages: promptMessages } = await getStreamAgentView({
|
||||
const { messages: promptMessages, toolDefinitions } = await getStreamAgentView({
|
||||
graph: this.graph,
|
||||
messages: messages ?? [],
|
||||
options,
|
||||
runManager,
|
||||
});
|
||||
/** A provider bills the advertised tool definitions too. The context snapshot counts
|
||||
* them inside its instruction share and reconciles against this total, so leaving
|
||||
* them out would leave too little for the messages and the tool results they hold. */
|
||||
let toolChars = 0;
|
||||
for (const [name, definition] of toolDefinitions) {
|
||||
toolChars += toolDefinitionText(name, definition).length;
|
||||
}
|
||||
const inputChars = promptMessages.reduce(
|
||||
(sum, message) => sum + getContentText(message?.content).length,
|
||||
0,
|
||||
toolChars,
|
||||
);
|
||||
const input_tokens = Math.max(1, Math.ceil(inputChars / 4));
|
||||
const output_tokens = Math.max(1, Math.ceil(outputChars / 4));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue