UNPKG

@langchain/anthropic

Version:
1 lines 6.14 kB
{"version":3,"file":"params.cjs","names":[],"sources":["../../src/utils/params.ts"],"sourcesContent":["import { AnthropicBeta } from \"@anthropic-ai/sdk/resources\";\n\nimport {\n AnthropicInvocationParams,\n AnthropicOutputConfig,\n AnthropicThinkingConfigParam,\n} from \"../types.js\";\n\ntype InvocationCompatibilityFields = {\n model?: string;\n thinking: AnthropicThinkingConfigParam;\n outputConfig?: AnthropicOutputConfig;\n topK?: number;\n topP?: number;\n temperature?: number;\n};\n\nconst ADAPTIVE_ONLY_MODEL_PREFIXES = [\n \"claude-opus-4-7\",\n \"claude-opus-4-8\",\n \"claude-opus-5\",\n \"claude-fable-5\",\n \"claude-mythos-5\",\n \"claude-mythos-preview\",\n] as const;\n\nfunction modelStartsWithAnyPrefix(\n model: string | undefined,\n prefixes: readonly string[]\n): boolean {\n return model ? prefixes.some((prefix) => model.startsWith(prefix)) : false;\n}\n\nfunction isThinkingEnabled(thinking: AnthropicThinkingConfigParam): boolean {\n return thinking.type === \"enabled\" || thinking.type === \"adaptive\";\n}\n\nexport function isOpus47Model(model?: string): boolean {\n return modelStartsWithAnyPrefix(model, [\"claude-opus-4-7\"]);\n}\n\nexport function isAdaptiveOnlyModel(model?: string): boolean {\n return modelStartsWithAnyPrefix(model, ADAPTIVE_ONLY_MODEL_PREFIXES);\n}\n\nexport function getTaskBudgetBetas(\n model?: string,\n outputConfig?: AnthropicOutputConfig\n): AnthropicBeta[] {\n const hasTaskBudget =\n outputConfig &&\n typeof outputConfig === \"object\" &&\n \"task_budget\" in outputConfig &&\n outputConfig.task_budget != null;\n\n return isOpus47Model(model) && hasTaskBudget\n ? ([\"task-budgets-2026-03-13\"] as AnthropicBeta[])\n : [];\n}\n\nexport function validateInvocationParamCompatibility(\n fields: InvocationCompatibilityFields\n): void {\n const { model, thinking, outputConfig, topK, topP, temperature } = fields;\n const adaptiveOnlyModel = isAdaptiveOnlyModel(model);\n const modelName = model ?? \"this model\";\n\n if (adaptiveOnlyModel && thinking.type === \"enabled\") {\n throw new Error(\n `thinking.type=\"enabled\" is not supported for ${modelName}; use thinking.type=\"adaptive\" instead`\n );\n }\n if (\n adaptiveOnlyModel &&\n typeof thinking === \"object\" &&\n thinking != null &&\n \"budget_tokens\" in thinking\n ) {\n throw new Error(\n `thinking.budget_tokens is not supported for ${modelName}; use outputConfig.effort instead`\n );\n }\n if (\n modelStartsWithAnyPrefix(model, [\"claude-opus-5\"]) &&\n thinking.type === \"disabled\" &&\n (outputConfig?.effort === \"xhigh\" || outputConfig?.effort === \"max\")\n ) {\n throw new Error(\n `thinking.type=\"disabled\" is not supported for ${modelName} with outputConfig.effort=\"${outputConfig.effort}\"; use thinking.type=\"adaptive\" or omit thinking instead`\n );\n }\n if (adaptiveOnlyModel) {\n if (topK !== undefined) {\n throw new Error(\n `topK is not supported for ${modelName}; omit topK/topP/temperature or use model prompting instead`\n );\n }\n if (topP !== undefined && topP !== 1) {\n throw new Error(\n `topP is not supported for ${modelName} when set to non-default values`\n );\n }\n if (temperature !== undefined && temperature !== 1) {\n throw new Error(\n `temperature is not supported for ${modelName} when set to non-default values`\n );\n }\n }\n\n if (isThinkingEnabled(thinking)) {\n if (topK !== undefined) {\n throw new Error(\"topK is not supported when thinking is enabled\");\n }\n if (topP !== undefined) {\n throw new Error(\"topP is not supported when thinking is enabled\");\n }\n if (temperature !== undefined && temperature !== 1) {\n throw new Error(\"temperature is not supported when thinking is enabled\");\n }\n }\n}\n\nexport function getSamplingParams(\n fields: InvocationCompatibilityFields\n): Pick<AnthropicInvocationParams, \"temperature\" | \"top_k\" | \"top_p\"> {\n const { model, thinking, topK, topP, temperature } = fields;\n const output: Pick<\n AnthropicInvocationParams,\n \"temperature\" | \"top_k\" | \"top_p\"\n > = {};\n\n if (isThinkingEnabled(thinking) || isAdaptiveOnlyModel(model)) {\n return output;\n }\n\n if (temperature !== undefined) {\n output.temperature = temperature;\n }\n if (topK !== undefined) {\n output.top_k = topK;\n }\n if (topP !==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