import OpenAI from 'openai'; /** * Streams the response from OpenAI, word by word, given a prompt. */ async function streamOpenAiResponse(prompt: string) { // To avoid this being instantiated before the dotenv package has loaded the .env file, // from the right directory, we have to instantiate it here. // This is a hack, and this should be institated outside this function. However, // in the current state of the code, this makes no difference, as the openAI // object is only used once. const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY as string, }); return await openai.chat.completions.create({ model: process.env.OPENAI_MODEL_ID as string, messages: [{ role: 'user', content: prompt }], stream: true, }); } export { streamOpenAiResponse };