From 353a16b2adbce031e33e10e86e49c45be4cd1289 Mon Sep 17 00:00:00 2001 From: Cedric Ziel Date: Fri, 13 Dec 2024 08:32:52 +0000 Subject: [PATCH] fix(OpenAI Node): Don't send system prompt for O1 model --- .../OpenAi/actions/text/message.operation.ts | 18 +++-- .../vendors/OpenAi/test/OpenAi.node.test.ts | 72 ++++++++++++++++++- 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts index 231fdb4b77..9b92fbf20c 100644 --- a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts +++ b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/text/message.operation.ts @@ -220,13 +220,17 @@ export async function execute(this: IExecuteFunctions, i: number): Promise { @@ -620,4 +620,72 @@ describe('OpenAi, Text resource', () => { }, }); }); + + it('message => json output, o1 models should not receive system prompt', async () => { + (transport.apiRequest as jest.Mock).mockResolvedValueOnce({ + choices: [{ message: { tool_calls: undefined } }], + }); + + await text.message.execute.call( + createExecuteFunctionsMock({ + modelId: 'o1-mini', + messages: { + values: [{ role: 'user', content: 'message' }], + }, + + jsonOutput: true, + + options: {}, + }), + 0, + ); + + expect(transport.apiRequest).toHaveBeenCalledWith('POST', '/chat/completions', { + body: { + messages: [{ content: 'message', role: 'user' }], + model: 'o1-mini', + response_format: { + type: 'json_object', + }, + tools: undefined, + }, + }); + }); + + it('message => json output, older models should receive system prompt', async () => { + (transport.apiRequest as jest.Mock).mockResolvedValueOnce({ + choices: [{ message: { tool_calls: undefined } }], + }); + + await text.message.execute.call( + createExecuteFunctionsMock({ + modelId: 'gpt-model', + messages: { + values: [{ role: 'user', content: 'message' }], + }, + + jsonOutput: true, + + options: {}, + }), + 0, + ); + + expect(transport.apiRequest).toHaveBeenCalledWith('POST', '/chat/completions', { + body: { + messages: [ + { + role: 'system', + content: 'You are a helpful assistant designed to output JSON.', + }, + { content: 'message', role: 'user' }, + ], + model: 'gpt-model', + response_format: { + type: 'json_object', + }, + tools: undefined, + }, + }); + }); });