Skip to content

Commit

Permalink
ollama: handle chunks that are not newline delimited (#3241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmorganca committed Nov 13, 2023
1 parent ab7c865 commit 9c1d800
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions langchain/src/util/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,17 @@ export async function* createOllamaStream(

const stream = IterableReadableStream.fromReadableStream(response.body);
const decoder = new TextDecoder();
let extra = "";
for await (const chunk of stream) {
try {
if (chunk !== undefined) {
const lines = decoder
.decode(chunk)
.split("\n")
.filter((v) => v.length);
for (const line of lines) {
yield JSON.parse(line);
}
const decoded = extra + decoder.decode(chunk);
const lines = decoded.split("\n");
extra = lines.pop() || "";
for (const line of lines) {
try {
yield JSON.parse(line);
} catch (e) {
console.warn(`Received a non-JSON parseable chunk: ${line}`);
}
} catch (e) {
console.warn(
`Received a non-JSON parseable chunk: ${decoder.decode(chunk)}`
);
}
}
}

0 comments on commit 9c1d800

Please sign in to comment.