feat(Anthropic Chat Model Node): Fetch models dynamically & support thinking (#13543)

This commit is contained in:
oleg
2025-02-27 15:40:58 +01:00
committed by GitHub
parent 615a42afd5
commit 461df371f7
5 changed files with 316 additions and 30 deletions

View File

@@ -14,6 +14,7 @@ import {
import { getConnectionHintNoticeField } from '@utils/sharedFields';
import { searchModels } from './methods/searchModels';
import { makeN8nLlmFailedAttemptHandler } from '../n8nLlmFailedAttemptHandler';
import { N8nLlmTracing } from '../N8nLlmTracing';
@@ -69,15 +70,23 @@ const modelField: INodeProperties = {
default: 'claude-2',
};
const MIN_THINKING_BUDGET = 1024;
const DEFAULT_MAX_TOKENS = 4096;
export class LmChatAnthropic implements INodeType {
methods = {
listSearch: {
searchModels,
},
};
description: INodeTypeDescription = {
displayName: 'Anthropic Chat Model',
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
name: 'lmChatAnthropic',
icon: 'file:anthropic.svg',
group: ['transform'],
version: [1, 1.1, 1.2],
defaultVersion: 1.2,
version: [1, 1.1, 1.2, 1.3],
defaultVersion: 1.3,
description: 'Language Model Anthropic',
defaults: {
name: 'Anthropic Chat Model',
@@ -135,7 +144,43 @@ export class LmChatAnthropic implements INodeType {
),
displayOptions: {
show: {
'@version': [{ _cnd: { gte: 1.2 } }],
'@version': [{ _cnd: { lte: 1.2 } }],
},
},
},
{
displayName: 'Model',
name: 'model',
type: 'resourceLocator',
default: {
mode: 'list',
value: 'claude-3-7-sonnet-20250219',
cachedResultName: 'Claude 3.7 Sonnet',
},
required: true,
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
placeholder: 'Select a model...',
typeOptions: {
searchListMethod: 'searchModels',
searchable: true,
},
},
{
displayName: 'ID',
name: 'id',
type: 'string',
placeholder: 'Claude Sonnet',
},
],
description:
'The model. Choose from the list, or specify an ID. <a href="https://docs.anthropic.com/claude/docs/models-overview">Learn more</a>.',
displayOptions: {
show: {
'@version': [{ _cnd: { gte: 1.3 } }],
},
},
},
@@ -150,7 +195,7 @@ export class LmChatAnthropic implements INodeType {
{
displayName: 'Maximum Number of Tokens',
name: 'maxTokensToSample',
default: 4096,
default: DEFAULT_MAX_TOKENS,
description: 'The maximum number of tokens to generate in the completion',
type: 'number',
},
@@ -162,6 +207,11 @@ export class LmChatAnthropic implements INodeType {
description:
'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
type: 'number',
displayOptions: {
hide: {
thinking: [true],
},
},
},
{
displayName: 'Top K',
@@ -171,6 +221,11 @@ export class LmChatAnthropic implements INodeType {
description:
'Used to remove "long tail" low probability responses. Defaults to -1, which disables it.',
type: 'number',
displayOptions: {
hide: {
thinking: [true],
},
},
},
{
displayName: 'Top P',
@@ -180,6 +235,30 @@ export class LmChatAnthropic implements INodeType {
description:
'Controls diversity via nucleus sampling: 0.5 means half of all likelihood-weighted options are considered. We generally recommend altering this or temperature but not both.',
type: 'number',
displayOptions: {
hide: {
thinking: [true],
},
},
},
{
displayName: 'Enable Thinking',
name: 'thinking',
type: 'boolean',
default: false,
description: 'Whether to enable thinking mode for the model',
},
{
displayName: 'Thinking Budget (Tokens)',
name: 'thinkingBudget',
type: 'number',
default: MIN_THINKING_BUDGET,
description: 'The maximum number of tokens to use for thinking',
displayOptions: {
show: {
thinking: [true],
},
},
},
],
},
@@ -189,13 +268,21 @@ export class LmChatAnthropic implements INodeType {
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('anthropicApi');
const modelName = this.getNodeParameter('model', itemIndex) as string;
const version = this.getNode().typeVersion;
const modelName =
version >= 1.3
? (this.getNodeParameter('model.value', itemIndex) as string)
: (this.getNodeParameter('model', itemIndex) as string);
const options = this.getNodeParameter('options', itemIndex, {}) as {
maxTokensToSample?: number;
temperature: number;
topK: number;
topP: number;
topK?: number;
topP?: number;
thinking?: boolean;
thinkingBudget?: number;
};
let invocationKwargs = {};
const tokensUsageParser = (llmOutput: LLMResult['llmOutput']) => {
const usage = (llmOutput?.usage as { input_tokens: number; output_tokens: number }) ?? {
@@ -208,6 +295,27 @@ export class LmChatAnthropic implements INodeType {
totalTokens: usage.input_tokens + usage.output_tokens,
};
};
if (options.thinking) {
invocationKwargs = {
thinking: {
type: 'enabled',
// If thinking is enabled, we need to set a budget.
// We fallback to 1024 as that is the minimum
budget_tokens: options.thinkingBudget ?? MIN_THINKING_BUDGET,
},
// The default Langchain max_tokens is -1 (no limit) but Anthropic requires a number
// higher than budget_tokens
max_tokens: options.maxTokensToSample ?? DEFAULT_MAX_TOKENS,
// These need to be unset when thinking is enabled.
// Because the invocationKwargs will override the model options
// we can pass options to the model and then override them here
top_k: undefined,
top_p: undefined,
temperature: undefined,
};
}
const model = new ChatAnthropic({
anthropicApiKey: credentials.apiKey as string,
modelName,
@@ -217,6 +325,7 @@ export class LmChatAnthropic implements INodeType {
topP: options.topP,
callbacks: [new N8nLlmTracing(this, { tokensUsageParser })],
onFailedAttempt: makeN8nLlmFailedAttemptHandler(this),
invocationKwargs,
});
return {