mirror of
https://github.com/langgenius/webapp-conversation.git
synced 2025-12-21 13:56:38 +08:00
feat: support steaming
This commit is contained in:
@ -1,6 +1,5 @@
|
|||||||
import { type NextRequest } from 'next/server'
|
import { type NextRequest } from 'next/server'
|
||||||
import { client, getInfo } from '@/app/api/utils/common'
|
import { client, getInfo } from '@/app/api/utils/common'
|
||||||
import { OpenAIStream } from '@/app/api/utils/stream'
|
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
const body = await request.json()
|
const body = await request.json()
|
||||||
@ -12,6 +11,5 @@ export async function POST(request: NextRequest) {
|
|||||||
} = body
|
} = body
|
||||||
const { user } = getInfo(request)
|
const { user } = getInfo(request)
|
||||||
const res = await client.createChatMessage(inputs, query, user, responseMode, conversationId)
|
const res = await client.createChatMessage(inputs, query, user, responseMode, conversationId)
|
||||||
const stream = await OpenAIStream(res as any)
|
return new Response(res.data as any)
|
||||||
return new Response(stream as any)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,25 +0,0 @@
|
|||||||
export async function OpenAIStream(res: { body: any }) {
|
|
||||||
const reader = res.body.getReader()
|
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams
|
|
||||||
// https://github.com/whichlight/chatgpt-api-streaming/blob/master/pages/api/OpenAIStream.ts
|
|
||||||
start(controller) {
|
|
||||||
return pump()
|
|
||||||
function pump() {
|
|
||||||
return reader.read().then(({ done, value }: any) => {
|
|
||||||
// When no more data needs to be consumed, close the stream
|
|
||||||
if (done) {
|
|
||||||
controller.close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Enqueue the next data chunk into our target stream
|
|
||||||
controller.enqueue(value)
|
|
||||||
return pump()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return stream
|
|
||||||
}
|
|
||||||
@ -26,7 +26,7 @@
|
|||||||
"axios": "^1.3.5",
|
"axios": "^1.3.5",
|
||||||
"classnames": "^2.3.2",
|
"classnames": "^2.3.2",
|
||||||
"copy-to-clipboard": "^3.3.3",
|
"copy-to-clipboard": "^3.3.3",
|
||||||
"dify-client": "1.0.3",
|
"dify-client": "2.0.0",
|
||||||
"eslint": "8.36.0",
|
"eslint": "8.36.0",
|
||||||
"eslint-config-next": "13.2.4",
|
"eslint-config-next": "13.2.4",
|
||||||
"eventsource-parser": "^1.0.0",
|
"eventsource-parser": "^1.0.0",
|
||||||
|
|||||||
@ -64,7 +64,7 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
|||||||
lines.forEach((message) => {
|
lines.forEach((message) => {
|
||||||
if (!message)
|
if (!message)
|
||||||
return
|
return
|
||||||
bufferObj = JSON.parse(message) // remove data: and parse as json
|
bufferObj = JSON.parse(message.substring(6)) // remove data: and parse as json
|
||||||
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
|
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
|
||||||
conversationId: bufferObj.conversation_id,
|
conversationId: bufferObj.conversation_id,
|
||||||
messageId: bufferObj.id,
|
messageId: bufferObj.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user