Created
April 4, 2023 03:44
-
-
Save gregonarash/2ccc3dc921bc2228f6913e6c4df0d780 to your computer and use it in GitHub Desktop.
This is copy of reply by Lee Robinson on this thread here https://github.com/vercel/next.js/issues/9965#issuecomment-1489481795
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copy of reply by Lee Robinson | |
// https://github.com/vercel/next.js/issues/9965#issuecomment-1489481795 | |
// app/api/route.ts | |
import { Configuration, OpenAIApi } from 'openai'; | |
export const runtime = 'nodejs'; | |
// This is required to enable streaming | |
export const dynamic = 'force-dynamic'; | |
export async function GET() { | |
const configuration = new Configuration({ | |
apiKey: process.env.OPENAI_API_KEY, | |
}); | |
const openai = new OpenAIApi(configuration); | |
let responseStream = new TransformStream(); | |
const writer = responseStream.writable.getWriter(); | |
const encoder = new TextEncoder(); | |
writer.write(encoder.encode('Vercel is a platform for....')); | |
try { | |
const openaiRes = await openai.createCompletion( | |
{ | |
model: 'text-davinci-002', | |
prompt: 'Vercel is a platform for', | |
max_tokens: 100, | |
temperature: 0, | |
stream: true, | |
}, | |
{ responseType: 'stream' } | |
); | |
// @ts-ignore | |
openaiRes.data.on('data', async (data: Buffer) => { | |
const lines = data | |
.toString() | |
.split('\n') | |
.filter((line: string) => line.trim() !== ''); | |
for (const line of lines) { | |
const message = line.replace(/^data: /, ''); | |
if (message === '[DONE]') { | |
console.log('Stream completed'); | |
writer.close(); | |
return; | |
} | |
try { | |
const parsed = JSON.parse(message); | |
await writer.write(encoder.encode(`${parsed.choices[0].text}`)); | |
} catch (error) { | |
console.error('Could not JSON parse stream message', message, error); | |
} | |
} | |
}); | |
} catch (error) { | |
console.error('An error occurred during OpenAI request', error); | |
writer.write(encoder.encode('An error occurred during OpenAI request')); | |
writer.close(); | |
} | |
return new Response(responseStream.readable, { | |
headers: { | |
'Content-Type': 'text/event-stream', | |
Connection: 'keep-alive', | |
'Cache-Control': 'no-cache, no-transform', | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment