UNPKG

@langchain/anthropic

Version:
1 lines 6.17 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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