Skip to content

Commit

Permalink
Fix buffer for non-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
aiql-admin authored Jul 18, 2024
1 parent 6ab8da0 commit 447619f
Showing 1 changed file with 54 additions and 55 deletions.
109 changes: 54 additions & 55 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ <h5 class="font-weight-bold">{{ column.key }}</h5>
let buffer = ''

// Read the stream
read(reader, messageStore.conversation, buffer);
read(reader, messageStore.conversation, buffer, chatbotStore.stream);
} catch (error) {
snackbarStore.showErrorMessage(error.message);
}
Expand All @@ -1107,7 +1107,8 @@ <h5 class="font-weight-bold">{{ column.key }}</h5>
const read = async (
reader,
target,
buffer
buffer,
stream
) => {
// TextDecoder is a built-in object that allows you to convert a stream of bytes into a string
const decoder = new TextDecoder();
Expand All @@ -1121,75 +1122,73 @@ <h5 class="font-weight-bold">{{ column.key }}</h5>
// Convert the stream of bytes into a string
const chunks = decoder.decode(value);

// Split stream
let parts = chunks.split('\n')
if (stream) {
// Split stream
let parts = chunks.split('\n')

if (parts.length === 1) {
buffer += parts[0]
return read(reader, target, buffer);
}
if (parts.length === 1) {
buffer += parts[0]
return read(reader, target, buffer);
}

if (buffer.length > 0) {
parts[0] = buffer + parts[0];
buffer = ''
}
if (buffer.length > 0) {
parts[0] = buffer + parts[0];
buffer = ''
}

const last = parts[parts.length - 1];
if (last && last.length > 0) {
buffer = parts.pop();
}
const last = parts[parts.length - 1];
if (last && last.length > 0) {
buffer = parts.pop();
}

const jsons = parts
.map((line) => line.trim())
.filter((line) => line.length > 0)
.map((line) => {
const pos = line.indexOf(':');
const name = line.substring(0, pos);
if (name !== 'data') {
return undefined
}
const content = line.substring(pos + 1).trim()
if (content.length === 0) {
return undefined
} else if (content === "[DONE]") {
return undefined
}
try {
const parsed = JSON.parse(content)
const choices = parsed.choices
return parseChoice(parsed.choices, target)
} catch (e) {
console.log(e, line)
return undefined
}
})
// Filter out any undefined values
.filter((data) => data);
parts
.map((line) => line.trim())
.filter((line) => line.length > 0)
.forEach((line) => {
const pos = line.indexOf(':');
const name = line.substring(0, pos);
if (name !== 'data') {
return
}
const content = line.substring(pos + 1).trim()
if (content.length === 0) {
return
} else if (content === "[DONE]") {
return
}
parseJson(content, target)
})

// server cannot support stream
if (jsons.length === 0) {
const parsed = JSON.parse(chunks)
parseChoice(parsed.choices, target)
} else {
parseJson(chunks, target)
}

// Repeat the process
return read(reader, target, buffer);
return read(reader, target, buffer, stream);
};

const parseJson = (content, target) => {
try {
const parsed = JSON.parse(content)
const choices = parsed.choices
const choice = choices.map((choice) => {
// Support both steam or not
return choice.delta?.content || choice.message?.content
})
parseChoice(choice, target)
} catch (e) {
console.log(e, content)
parseChoice(content, target)
}
};

const parseChoice = (choices, target) => {
const choice = choices.map((choice) => {
// Support both steam or not
return choice.delta?.content || choice.message?.content
})
const parseChoice = (choice, target) => {
if (choice) {
if (target instanceof Array) {
target[target.length - 1].content += choice;
} else {
target = target += choice;
}
return choice
} else {
return undefined
}
};

Expand Down

0 comments on commit 447619f

Please sign in to comment.