Last active
May 16, 2023 07:40
-
-
Save yazinsai/cfde3548a3fc6280b6a2ed8b18e0539a to your computer and use it in GitHub Desktop.
Streams an OpenAI response, compatible with Next.js
This file contains 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
/** | |
* Usage: | |
* | |
* export const config = { | |
* runtime: "edge", | |
* }; | |
* | |
* const stream = await streamFromResponse(await openai.complete({ | |
* model: 'gpt-3.5-turbo', | |
* prompt: 'this is a test', | |
* stream: true | |
* }) | |
* | |
* return new Response(stream); | |
*/ | |
import { | |
createParser, | |
ParsedEvent, | |
ReconnectInterval, | |
} from "eventsource-parser"; | |
export const streamFromResponse = (response: any): ReadableStream => { | |
const encoder = new TextEncoder(); | |
const decoder = new TextDecoder(); | |
return new ReadableStream({ | |
async start(controller) { | |
const onParse = (event: ParsedEvent | ReconnectInterval) => { | |
if (event.type === "event") { | |
const data = event.data; | |
if (data === "[DONE]") { | |
controller.close(); | |
return; | |
} | |
try { | |
const json = JSON.parse(data); | |
const text = json.choices[0].delta.content; | |
const queue = encoder.encode(text); | |
controller.enqueue(queue); | |
} catch (e) { | |
controller.error(e); | |
} | |
} | |
}; | |
const parser = createParser(onParse); | |
for await (const chunk of response.body as any) { | |
parser.feed(decoder.decode(chunk)); | |
} | |
}, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment