浏览代码

Fix JSON parsing in generate.

saul 1 年之前
父节点
当前提交
da03b7909e
共有 2 个文件被更改,包括 20 次插入16 次删除
  1. 1 1
      package.json
  2. 19 15
      src/index.ts

+ 1 - 1
package.json

@@ -1,7 +1,7 @@
 {
   "type": "module",
   "name": "ollama",
-  "version": "0.1.0",
+  "version": "0.1.1",
   "description": "Interface with an ollama instance over HTTP.",
   "main": "dist/index.js",
   "types": "dist/index.d.ts",

+ 19 - 15
src/index.ts

@@ -42,22 +42,26 @@ export class Ollama {
 		}
 
 		for await (const chunk of response.body) {
-			const res: GenerateResponse | GenerateResponseEnd = JSON.parse(chunk.toString());
-
-			if (res.done) {
-				return {
-					model: res.model,
-					createdAt: new Date(res.created_at),
-					context: res.context,
-					totalDuration: res.total_duration,
-					loadDuration: res.load_duration,
-					promptEvalCount: res.prompt_eval_count,
-					evalCount: res.eval_count,
-					evalDuration: res.eval_duration
-				};
-			}
+			const messages = chunk.toString().split("\n").filter(s => s.length !== 0);
 
-			yield res.response;
+			for (const message of messages) {
+				const res: GenerateResponse | GenerateResponseEnd = JSON.parse(message);
+
+				if (res.done) {
+					return {
+						model: res.model,
+						createdAt: new Date(res.created_at),
+						context: res.context,
+						totalDuration: res.total_duration,
+						loadDuration: res.load_duration,
+						promptEvalCount: res.prompt_eval_count,
+						evalCount: res.eval_count,
+						evalDuration: res.eval_duration
+					};
+				}
+
+				yield res.response;
+			}
 		}
 
 		throw new Error("Did not recieve done response in stream.");