From 91418365634dcc2c1eb5608878aadb6c88c004fc Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Fri, 25 Apr 2025 14:42:28 -0700 Subject: [PATCH 1/3] feat-cache-tools-config --- core/config/types.ts | 1 + core/index.d.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/core/config/types.ts b/core/config/types.ts index a23dcfeec..e9b7ca3fd 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -852,6 +852,7 @@ declare global { export interface CacheBehavior { cacheSystemMessage?: boolean; cacheConversation?: boolean; + cacheToolsConfig?: boolean; } export interface ClientCertificateOptions { diff --git a/core/index.d.ts b/core/index.d.ts index 6faff1bab..5a9707e3f 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -927,6 +927,7 @@ export interface RequestOptions { export interface CacheBehavior { cacheSystemMessage?: boolean; cacheConversation?: boolean; + cacheToolsConfig?: boolean; } export interface ClientCertificateOptions { From 820acc869b99c5b7b8c3cb4cb2f4c653ab4bf76a Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Tue, 29 Apr 2025 12:37:23 -0700 Subject: [PATCH 2/3] tools config caching for bedrock --- core/llm/llms/Bedrock.ts | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/core/llm/llms/Bedrock.ts b/core/llm/llms/Bedrock.ts index 430e4a93b..055d456d2 100644 --- a/core/llm/llms/Bedrock.ts +++ b/core/llm/llms/Bedrock.ts @@ -5,6 +5,7 @@ import { ConverseStreamCommandOutput, InvokeModelCommand, Message, + ToolConfiguration, } from "@aws-sdk/client-bedrock-runtime"; import { fromNodeProviderChain } from "@aws-sdk/credential-providers"; @@ -294,6 +295,7 @@ class Bedrock extends BaseLLM { !!systemMessage && this.cacheBehavior?.cacheSystemMessage; const enablePromptCaching = shouldCacheSystemMessage || this.cacheBehavior?.cacheConversation; + const shouldCacheToolsConfig = this.cacheBehavior?.cacheToolsConfig; // Add header for prompt caching if (enablePromptCaching) { @@ -305,28 +307,34 @@ class Bedrock extends BaseLLM { const supportsTools = PROVIDER_TOOL_SUPPORT.bedrock?.(options.model || "") ?? false; + + let toolConfig = supportsTools && options.tools + ? { + tools: options.tools.map((tool) => ({ + toolSpec: { + name: tool.function.name, + description: tool.function.description, + inputSchema: { + json: tool.function.parameters, + }, + }, + })), + } as ToolConfiguration + : undefined; + + if (toolConfig?.tools && shouldCacheToolsConfig) { + toolConfig.tools.push({ cachePoint: { type: "default" } }); + } + return { modelId: options.model, - messages: convertedMessages, system: systemMessage ? shouldCacheSystemMessage ? [{ text: systemMessage }, { cachePoint: { type: "default" } }] : [{ text: systemMessage }] : undefined, - toolConfig: - supportsTools && options.tools - ? { - tools: options.tools.map((tool) => ({ - toolSpec: { - name: tool.function.name, - description: tool.function.description, - inputSchema: { - json: tool.function.parameters, - }, - }, - })), - } - : undefined, + toolConfig: toolConfig, + messages: convertedMessages, inferenceConfig: { maxTokens: options.maxTokens, temperature: options.temperature, From 6b8345e792968b3d1b6d164e2a52aaac87ca17ea Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Thu, 8 May 2025 21:08:13 -0700 Subject: [PATCH 3/3] Introduce promptCaching option --- core/config/types.ts | 1 - core/index.d.ts | 2 +- core/llm/llms/Bedrock.ts | 6 +++--- packages/config-yaml/src/schemas/models.ts | 1 + 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/config/types.ts b/core/config/types.ts index e9b7ca3fd..a23dcfeec 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -852,7 +852,6 @@ declare global { export interface CacheBehavior { cacheSystemMessage?: boolean; cacheConversation?: boolean; - cacheToolsConfig?: boolean; } export interface ClientCertificateOptions { diff --git a/core/index.d.ts b/core/index.d.ts index 5a9707e3f..ba00f345c 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -927,7 +927,6 @@ export interface RequestOptions { export interface CacheBehavior { cacheSystemMessage?: boolean; cacheConversation?: boolean; - cacheToolsConfig?: boolean; } export interface ClientCertificateOptions { @@ -1024,6 +1023,7 @@ export interface BaseCompletionOptions { toolChoice?: ToolChoice; reasoning?: boolean; reasoningBudgetTokens?: number; + promptCaching?: boolean; } export interface ModelCapability { diff --git a/core/llm/llms/Bedrock.ts b/core/llm/llms/Bedrock.ts index 055d456d2..7ca0f801c 100644 --- a/core/llm/llms/Bedrock.ts +++ b/core/llm/llms/Bedrock.ts @@ -292,10 +292,10 @@ class Bedrock extends BaseLLM { const convertedMessages = this._convertMessages(messages); const shouldCacheSystemMessage = - !!systemMessage && this.cacheBehavior?.cacheSystemMessage; + !!systemMessage && this.cacheBehavior?.cacheSystemMessage || this.completionOptions.promptCaching; const enablePromptCaching = - shouldCacheSystemMessage || this.cacheBehavior?.cacheConversation; - const shouldCacheToolsConfig = this.cacheBehavior?.cacheToolsConfig; + shouldCacheSystemMessage || this.cacheBehavior?.cacheConversation || this.completionOptions.promptCaching; + const shouldCacheToolsConfig = this.completionOptions.promptCaching; // Add header for prompt caching if (enablePromptCaching) { diff --git a/packages/config-yaml/src/schemas/models.ts b/packages/config-yaml/src/schemas/models.ts index 2a1eb0a68..575799936 100644 --- a/packages/config-yaml/src/schemas/models.ts +++ b/packages/config-yaml/src/schemas/models.ts @@ -49,6 +49,7 @@ export const completionOptionsSchema = z.object({ n: z.number().optional(), reasoning: z.boolean().optional(), reasoningBudgetTokens: z.number().optional(), + promptCaching: z.boolean().optional(), }); export type CompletionOptions = z.infer;