Slide 34
Slide 34 text
Copyright coconala Inc. All Rights Reserved.
AsyncIterableを作ってReadableのインスタンスを
生成する必要がある
34
const stream = await openAi.chat.completions.create(chatCompletionCreateParams)
const chatCompletionAsyncIterator = async function* (): AsyncIterable {
for await (const chunk of stream)
yield chunk.choices.map(v => v.delta.content).filter(v => !!v).join()
}
const readable = Stream.Readable.from(chatCompletionAsyncIterator())
res.setHeader('Content-Type', 'text/event-stream;charset=utf-8')
res.setHeader('Cache-Control', 'no-cache, no-transform')
return sendStream(event, readable)