Skip to content

Commit

Permalink
fix: ollama api response parse issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei committed Jan 25, 2025
1 parent 985bda0 commit 2fd90cb
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions react-native/src/api/ollama-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,24 @@ export const invokeOllamaWithCallBack = async (
}
const reader = body.getReader();
const decoder = new TextDecoder();
let lastChunk = '';
while (true) {
const { done, value } = await reader.read();
const chunk = decoder.decode(value, { stream: true });
if (!chunk) {
break;
}
const parsed = parseStreamData(chunk);
const parsed = parseStreamData(chunk, lastChunk);
if (parsed.error) {
callback(parsed.error, true, true);
break;
}
completeMessage += parsed.content;
if (parsed.dataChunk) {
lastChunk = parsed.dataChunk;
} else {
lastChunk = '';
}
if (parsed.usage && parsed.usage.inputTokens) {
callback(completeMessage, true, false, parsed.usage);
} else {
Expand All @@ -86,10 +92,10 @@ export const invokeOllamaWithCallBack = async (
});
};

const parseStreamData = (chunk: string) => {
const parseStreamData = (chunk: string, lastChunk: string = '') => {
let content = '';
let usage: Usage | undefined;
const dataChunks = chunk.split('\n');
const dataChunks = (lastChunk + chunk).split('\n');
for (let dataChunk of dataChunks) {
if (!dataChunk.trim()) {
continue;
Expand All @@ -113,8 +119,12 @@ const parseStreamData = (chunk: string) => {
};
}
} catch (error) {
console.info('parse error:', error, chunk);
return { error: chunk };
if (lastChunk.length > 0) {
return { error: error + chunk };
}
if (content.length > 0) {
return { content, dataChunk, usage };
}
}
}
return { content, usage };
Expand Down

0 comments on commit 2fd90cb

Please sign in to comment.