Skip to content

Commit

Permalink
Update generate.ts
Browse files Browse the repository at this point in the history
support stream
  • Loading branch information
babaohuang committed Dec 14, 2023
1 parent bfd633f commit 5db31c8
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/pages/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ export const post: APIRoute = async(context) => {
}), { status: 400 })
}

if (!messages) {
return new Response(JSON.stringify({
error: {
message: 'No input text.',
},
}), { status: 400 })
}

if (sitePassword && !(sitePassword === pass || passList.includes(pass))) {
return new Response(JSON.stringify({
error: {
Expand All @@ -48,17 +40,18 @@ export const post: APIRoute = async(context) => {
// Start chat and send message with streaming
const stream = await startChatAndSendMessageStream(history, newMessage)

let accumulatedText = ''

// Handle the stream
for await (const chunk of stream) {
// Directly append the chunk text without any JSON conversion
accumulatedText += chunk.text()
}
const responseStream = new ReadableStream({
async start(controller) {
for await (const chunk of stream) {
const text = await chunk.text()
const queue = new TextEncoder().encode(text)
controller.enqueue(queue)
}
controller.close()
},
})

// Since the accumulatedText is already plain text, we don't need to parse it as JSON
// Just send the accumulated text back to the client
return new Response(accumulatedText, { status: 200, headers: { 'Content-Type': 'text/plain' } })
return new Response(responseStream, { status: 200, headers: { 'Content-Type': 'text/plain' } })
} catch (error) {
console.error(error)
return new Response(JSON.stringify({
Expand Down

0 comments on commit 5db31c8

Please sign in to comment.