fix(openai): map system prompts to top-level instructions for ChatGPT auth requests
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import type { JSONObject, LanguageModelV3CallOptions, LanguageModelV3Middleware } from '@ai-sdk/provider';
|
||||
|
||||
const isNonEmptyString = (value: unknown): value is string => typeof value === 'string' && value.trim().length > 0;
|
||||
|
||||
const getSystemInstructions = (prompt: LanguageModelV3CallOptions['prompt']): string | undefined => {
|
||||
const instructions = prompt
|
||||
.filter((message) => message.role === 'system')
|
||||
.map((message) => message.content)
|
||||
.filter(isNonEmptyString);
|
||||
|
||||
return instructions.length > 0 ? instructions.join('\n') : undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* ChatGPT's Codex backend requires top-level Responses API instructions.
|
||||
* The standard OpenAI provider otherwise serializes system prompts as input items.
|
||||
*/
|
||||
export function createOpenAiChatGptInstructionsMiddleware(): LanguageModelV3Middleware {
|
||||
return {
|
||||
specificationVersion: 'v3',
|
||||
transformParams: async ({ params }) => {
|
||||
const instructions = getSystemInstructions(params.prompt);
|
||||
if (!instructions) {
|
||||
return params;
|
||||
}
|
||||
|
||||
const providerOptions = params.providerOptions ?? {};
|
||||
const openAiProviderOptions = providerOptions.openai ?? {};
|
||||
if (isNonEmptyString(openAiProviderOptions.instructions)) {
|
||||
return params;
|
||||
}
|
||||
|
||||
return {
|
||||
...params,
|
||||
prompt: params.prompt.filter((message) => message.role !== 'system'),
|
||||
providerOptions: {
|
||||
...providerOptions,
|
||||
openai: {
|
||||
...openAiProviderOptions,
|
||||
instructions,
|
||||
} as JSONObject,
|
||||
},
|
||||
} satisfies LanguageModelV3CallOptions;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user