Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(OpenAI Chat Model Node): Add reasoning effort option to control the amount of reasoning tokens to use #13103

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,38 @@ export class LmChatOpenAi implements INodeType {
'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
type: 'number',
},
{
displayName: 'Reasoning Effort',
name: 'reasoningEffort',
default: 'medium',
description:
'Controls the amount of reasoning tokens to use. A value of "low" will favor speed and economical token usage, "high" will favor more complete reasoning at the cost of more tokens generated and slower responses.',
type: 'options',
options: [
{
name: 'Low',
value: 'low',
description: 'Favors speed and economical token usage',
},
{
name: 'Medium',
value: 'medium',
description: 'Balance between speed and reasoning accuracy',
},
{
name: 'High',
value: 'high',
description:
'Favors more complete reasoning at the cost of more tokens generated and slower responses',
},
],
displayOptions: {
show: {
// reasoning_effort is only available on o1, o1-versioned, or on o3-mini and beyond. Not on o1-mini or other GPT-models.
'/model': [{ _cnd: { regex: '(^o1([-\\d]+)?$)|(^o[3-9].*)' } }],
},
},
},
{
displayName: 'Timeout',
name: 'timeout',
Expand Down Expand Up @@ -311,6 +343,7 @@ export class LmChatOpenAi implements INodeType {
temperature?: number;
topP?: number;
responseFormat?: 'text' | 'json_object';
reasoningEffort?: 'low' | 'medium' | 'high';
};

const configuration: ClientOptions = {};
Expand All @@ -320,6 +353,15 @@ export class LmChatOpenAi implements INodeType {
configuration.baseURL = credentials.url as string;
}

// Extra options to send to OpenAI, that are not directly supported by LangChain
const modelKwargs: {
response_format?: object;
reasoning_effort?: 'low' | 'medium' | 'high';
} = {};
if (options.responseFormat) modelKwargs.response_format = { type: options.responseFormat };
if (options.reasoningEffort && ['low', 'medium', 'high'].includes(options.reasoningEffort))
modelKwargs.reasoning_effort = options.reasoningEffort;

const model = new ChatOpenAI({
openAIApiKey: credentials.apiKey as string,
modelName,
Expand All @@ -328,11 +370,7 @@ export class LmChatOpenAi implements INodeType {
maxRetries: options.maxRetries ?? 2,
configuration,
callbacks: [new N8nLlmTracing(this)],
modelKwargs: options.responseFormat
? {
response_format: { type: options.responseFormat },
}
: undefined,
modelKwargs,
onFailedAttempt: makeN8nLlmFailedAttemptHandler(this, openAiFailedAttemptHandler),
});

Expand Down
Loading