How to pass API key to OpenAI provider? #1545
-
I wasn't able to find anything in docs about this. This is what I attempted: import { streamText } from 'ai';
import { OpenAI } from '@ai-sdk/openai';
export const prerender = false;
export async function POST(context) {
const { prompt, messages }: { prompt: string; messages: any[] } =
await context.request.json();
const openai = new OpenAI({
apiKey: import.meta.env.OPENAI_API_KEY
});
const result = await streamText({
model: openai('gpt-4'),
system: 'You are a helpful assistant.',
messages,
});
return result.toAIStreamResponse();
} This doesn't work, error: What am I missing?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
I think you are not using the right function try :
|
Beta Was this translation helpful? Give feedback.
It uses the environment variable "OPENAI_API_KEY" by default. However, not all environments automatically add it from the system environment (if it's set there) or from a
.env
file.