Replies: 2 comments 2 replies
-
Hello, @annilq! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this together! To correctly retrieve and concatenate the content of an
Here's an example of how you can implement this in your import { AIMessageChunk } from 'path-to-your-ai-message-chunk-module';
let finalMessage: AIMessageChunk | undefined;
const appendMessage = async (message: CreateMessage) => {
const updateMessage = await createMessage(chat.id, message) as Message;
const messageId = uuidv4();
await fetchEventSource("/api/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "text/event-stream",
},
body: JSON.stringify({
model: chat.model,
chatId: chat.id,
messages,
messageId: updateMessage.id
}),
onmessage: (message) => {
if (message.event !== "data") return;
const eventSourceMessage = JSON.parse(message.data);
if (!eventSourceMessage.data.chunk) return;
if (eventSourceMessage.event === "on_chain_stream") {
const chunk = new AIMessageChunk(eventSourceMessage.data.chunk);
finalMessage = !finalMessage ? chunk : finalMessage.concat(chunk);
if (finalMessage) {
console.log(finalMessage.content);
}
setMessages((prevState) => [
...(prevState.at(-1)?.role === "user" ? prevState : prevState.slice(0, -1)),
{
id: messageId,
role: "assistant",
content: finalMessage.content,
},
]);
}
},
onerror: (err) => {
console.log(err);
},
});
}; Make sure that both To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
Beta Was this translation helpful? Give feedback.
-
this will get the right content,but I wonder why so much empty content return to frontend,and the response speed is so slow if (eventSourceMessage.event === "on_chat_model_stream") {
if (aiMessage) {
aiMessage = aiMessage.concat(new AIMessageChunk(eventSourceMessage.data.chunk.kwargs));
} else {
aiMessage = new AIMessageChunk(eventSourceMessage.data.chunk.kwargs);
}
console.log(aiMessage?.content);
} |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I try to get the streamevent content from data chunk,but the content nest so deep ,i notice the the type of the chunk is AIMessageChunk so I want to init the AIMessageChunk the chunk and concat the follow chunk,finnaly I want to get content of the AIMessageChunk ,but the error is below
when onmessage called below error
TypeError: firstContent is not iterable
at mergeContent (webpack-internal:///(app-pages-browser)/./node_modules/@langchain/core/dist/messages/base.js:36:20)
at AIMessageChunk.concat (webpack-internal:///(app-pages-browser)/./node_modules/@langchain/core/dist/messages/ai.js:253:76)
at onmessage (webpack-internal:///(app-pages-browser)/./app/books/[id]/page.client.tsx:84:35)
at onLine (webpack-internal:///(app-pages-browser)/./node_modules/@microsoft/fetch-event-source/lib/esm/parse.js:73:67)
at onChunk (webpack-internal:///(app-pages-browser)/./node_modules/@microsoft/fetch-event-source/lib/esm/parse.js:55:13)
at getBytes (webpack-internal:///(app-pages-browser)/./node_modules/@microsoft/fetch-event-source/lib/esm/parse.js:11:9)
at async create (webpack-internal:///(app-pages-browser)/./node_modules/@microsoft/fetch-event-source/lib/esm/fetch.js:58:17)
try to fix it
System Info
[email protected] | MIT | deps: 12 | versions: 314
mac
node:v23.7
npm :10.9.2
Beta Was this translation helpful? Give feedback.
All reactions