@langchain/anthropic
Version:
Anthropic integrations for LangChain.js
56 lines (55 loc) • 3.17 kB
JavaScript
//#region src/utils/params.ts
const ADAPTIVE_ONLY_MODEL_PREFIXES = [
"claude-opus-4-7",
"claude-opus-4-8",
"claude-opus-5",
"claude-fable-5",
"claude-mythos-5",
"claude-mythos-preview"
];
function modelStartsWithAnyPrefix(model, prefixes) {
return model ? prefixes.some((prefix) => model.startsWith(prefix)) : false;
}
function isThinkingEnabled(thinking) {
return thinking.type === "enabled" || thinking.type === "adaptive";
}
function isOpus47Model(model) {
return modelStartsWithAnyPrefix(model, ["claude-opus-4-7"]);
}
function isAdaptiveOnlyModel(model) {
return modelStartsWithAnyPrefix(model, ADAPTIVE_ONLY_MODEL_PREFIXES);
}
function getTaskBudgetBetas(model, outputConfig) {
const hasTaskBudget = outputConfig && typeof outputConfig === "object" && "task_budget" in outputConfig && outputConfig.task_budget != null;
return isOpus47Model(model) && hasTaskBudget ? ["task-budgets-2026-03-13"] : [];
}
function validateInvocationParamCompatibility(fields) {
const { model, thinking, outputConfig, topK, topP, temperature } = fields;
const adaptiveOnlyModel = isAdaptiveOnlyModel(model);
const modelName = model ?? "this model";
if (adaptiveOnlyModel && thinking.type === "enabled") throw new Error(`thinking.type="enabled" is not supported for ${modelName}; use thinking.type="adaptive" instead`);
if (adaptiveOnlyModel && typeof thinking === "object" && thinking != null && "budget_tokens" in thinking) throw new Error(`thinking.budget_tokens is not supported for ${modelName}; use outputConfig.effort instead`);
if (modelStartsWithAnyPrefix(model, ["claude-opus-5"]) && thinking.type === "disabled" && (outputConfig?.effort === "xhigh" || outputConfig?.effort === "max")) throw new Error(`thinking.type="disabled" is not supported for ${modelName} with outputConfig.effort="${outputConfig.effort}"; use thinking.type="adaptive" or omit thinking instead`);
if (adaptiveOnlyModel) {
if (topK !== void 0) throw new Error(`topK is not supported for ${modelName}; omit topK/topP/temperature or use model prompting instead`);
if (topP !== void 0 && topP !== 1) throw new Error(`topP is not supported for ${modelName} when set to non-default values`);
if (temperature !== void 0 && temperature !== 1) throw new Error(`temperature is not supported for ${modelName} when set to non-default values`);
}
if (isThinkingEnabled(thinking)) {
if (topK !== void 0) throw new Error("topK is not supported when thinking is enabled");
if (topP !== void 0) throw new Error("topP is not supported when thinking is enabled");
if (temperature !== void 0 && temperature !== 1) throw new Error("temperature is not supported when thinking is enabled");
}
}
function getSamplingParams(fields) {
const { model, thinking, topK, topP, temperature } = fields;
const output = {};
if (isThinkingEnabled(thinking) || isAdaptiveOnlyModel(model)) return output;
if (temperature !== void 0) output.temperature = temperature;
if (topK !== void 0) output.top_k = topK;
if (topP !== void 0) output.top_p = topP;
return output;
}
//#endregion
export { getSamplingParams, getTaskBudgetBetas, validateInvocationParamCompatibility };
//# sourceMappingURL=params.js.map