This commit is contained in:
Cedric Ziel 2025-03-05 17:11:55 +01:00 committed by GitHub
commit a48715c475
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 7 deletions

View file

@ -224,13 +224,17 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
let response_format; let response_format;
if (jsonOutput) { if (jsonOutput) {
response_format = { type: 'json_object' }; response_format = { type: 'json_object' };
messages = [
{ // o1 family doesn't support system prompt
role: 'system', if (!model?.toString().toLocaleLowerCase().startsWith('o1')) {
content: 'You are a helpful assistant designed to output JSON.', messages = [
}, {
...messages, role: 'system',
]; content: 'You are a helpful assistant designed to output JSON.',
},
...messages,
];
}
} }
const hideTools = this.getNodeParameter('hideTools', i, '') as string; const hideTools = this.getNodeParameter('hideTools', i, '') as string;

View file

@ -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,
},
});
});
}); });