Skip to content

Commit

Permalink
Merge pull request #162 from sugarforever/bugfix/issue-160-first-coup…
Browse files Browse the repository at this point in the history
…le-of-chunks-missing

bugfix: issue 160
  • Loading branch information
sugarforever authored Mar 27, 2024
2 parents 71540c9 + 575f9f5 commit 9ba41c0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
9 changes: 3 additions & 6 deletions components/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const fetchStream = async (url: string, options: RequestInit) => {
const response = await fetch(url, options);
if (response.body) {
messages.value = messages.value.filter((message) => message.type !== 'loading');
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
Expand All @@ -62,13 +63,9 @@ const fetchStream = async (url: string, options: RequestInit) => {
console.log('line: ', line);
const chatMessage = JSON.parse(line);
const content = chatMessage?.message?.content;
const count = messages.value.length;
if (content) {
if (messages.value[count - 1]?.type === 'loading') {
messages.value.pop()
}
if (count > 0 && messages.value[count - 1].role === 'assistant') {
messages.value[count - 1].content += content;
if (messages.value.length > 0 && messages.value[messages.value.length - 1].role === 'assistant') {
messages.value[messages.value.length - 1].content += content;
} else {
messages.value.push({ role: 'assistant', content });
}
Expand Down
2 changes: 0 additions & 2 deletions server/api/models/chat/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export default defineEventHandler(async (event) => {
messages: [new HumanMessage(query)],
});

console.log(response);
const readableStream = Readable.from((async function* () {
for await (const chunk of response) {
if (chunk?.answer !== undefined) {
Expand All @@ -94,7 +93,6 @@ export default defineEventHandler(async (event) => {
content: chunk?.answer
}
};
console.log(message);
yield `${JSON.stringify(message)}\n\n`;
}
}
Expand Down

0 comments on commit 9ba41c0

Please sign in to comment.