-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Patch Ollama Streaming chunk issues #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Replace stream/sync chats with Langchain interface for now connect #499 ref: #495 (comment)
server/utils/AiProviders/ollama/index.jsIn the #convertToLangchainPrototypes(chats = []) {
const {
HumanMessage,
SystemMessage,
AIMessage,
} = require("langchain/schema");
const langchainChats = [];
const roleToMessageMap = {
'system': SystemMessage,
'user': HumanMessage,
'assistant': AIMessage
};
for (const chat of chats) {
const MessageClass = roleToMessageMap[chat.role];
if (MessageClass) {
langchainChats.push(new MessageClass({ content: chat.content }));
}
}
return langchainChats;
}server/utils/chats/stream.jsIn the try {
for await (const chunk of stream) {
const content = chunk.hasOwnProperty("content") ? chunk.content : chunk;
fullText += content;
writeResponseChunk(response, {
uuid,
sources: [],
type: "textResponseChunk",
textResponse: content,
close: false,
error: false,
});
}
} catch (error) {
console.error(`Error handling stream: ${error}`);
// Handle the error appropriately
} |
| for (const chat of chats) { | ||
| switch (chat.role) { | ||
| case "system": | ||
| langchainChats.push(new SystemMessage({ content: chat.content })); | ||
| break; | ||
| case "user": | ||
| langchainChats.push(new HumanMessage({ content: chat.content })); | ||
| break; | ||
| case "assistant": | ||
| langchainChats.push(new AIMessage({ content: chat.content })); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced the switch statement in the #convertToLangchainPrototypes method with a map object for better readability and performance.
| for (const chat of chats) { | |
| switch (chat.role) { | |
| case "system": | |
| langchainChats.push(new SystemMessage({ content: chat.content })); | |
| break; | |
| case "user": | |
| langchainChats.push(new HumanMessage({ content: chat.content })); | |
| break; | |
| case "assistant": | |
| langchainChats.push(new AIMessage({ content: chat.content })); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| const roleToMessageMap = { | |
| 'system': SystemMessage, | |
| 'user': HumanMessage, | |
| 'assistant': AIMessage | |
| }; | |
| for (const chat of chats) { | |
| const MessageClass = roleToMessageMap[chat.role]; | |
| if (MessageClass) { | |
| langchainChats.push(new MessageClass({ content: chat.content })); | |
| } | |
| } |
Replace stream/sync chats with Langchain interface for now connect Mintplex-Labs#499 ref: Mintplex-Labs#495 (comment)
Replace stream/sync chats with Langchain interface for now
connect #499
ref: #495 (comment)