From 4a120b70b5c433f5cf5134ec7d845467d574ccab Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 7 Feb 2024 22:39:48 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20use=20ollama=20official=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/llm/llms/Ollama.ts | 46 +++++++++-------------------- core/package-lock.json | 14 +++++++++ core/package.json | 1 + core/yarn.lock | 12 ++++++++ extensions/vscode/package-lock.json | 1 + extensions/vscode/yarn.lock | 1 + 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 225d2c560..bf956b402 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -1,3 +1,4 @@ +import * as ollama from "ollama"; import { BaseLLM } from ".."; import { ChatMessage, @@ -174,40 +175,21 @@ class Ollama extends BaseLLM { messages: ChatMessage[], options: CompletionOptions ): AsyncGenerator { - const response = await this.fetch(`${this.apiBase}/api/chat`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(this._convertArgs(options, messages)), + const client = new ollama.Ollama({ + host: this.apiBase, + fetch: this.fetch.bind(this), + }); + const response = await client.chat({ + ...this._convertArgs(options, messages), + stream: true, }); - let buffer = ""; - for await (const value of streamResponse(response)) { - // Append the received chunk to the buffer - buffer += value; - // Split the buffer into individual JSON chunks - const chunks = buffer.split("\n"); - buffer = chunks.pop() ?? ""; - - for (let i = 0; i < chunks.length; i++) { - const chunk = chunks[i]; - if (chunk.trim() !== "") { - try { - const j = JSON.parse(chunk); - if (j.message?.content) { - yield { - role: "assistant", - content: j.message.content, - }; - } else if (j.error) { - throw new Error(j.error); - } - } catch (e) { - throw new Error(`Error parsing Ollama response: ${e} ${chunk}`); - } - } - } + for await (const chunk of response) { + if (!chunk?.message?.content) continue; + yield { + role: "assistant", + content: chunk.message.content, + }; } } diff --git a/core/package-lock.json b/core/package-lock.json index 394c301d1..b1bc88eb5 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -20,6 +20,7 @@ "llm-code-highlighter": "^0.0.3", "node-fetch": "^3.3.2", "node-html-markdown": "^1.3.0", + "ollama": "^0.4.6", "openai": "^4.20.1", "replicate": "^0.23.0", "socket.io-client": "^4.7.3", @@ -5413,6 +5414,14 @@ "resolved": "https://registry.npmjs.org/obliterator/-/obliterator-2.0.4.tgz", "integrity": "sha512-lgHwxlxV1qIg1Eap7LgIeoBWIMFibOjbrYPIPJZcI1mmGAI2m3lNYpK12Y+GBdPQ0U1hRwSord7GIaawz962qQ==" }, + "node_modules/ollama": { + "version": "0.4.6", + "resolved": "https://registry.npmjs.org/ollama/-/ollama-0.4.6.tgz", + "integrity": "sha512-/Im2atcM9hAxOgEi7mc5pG2G+MeN4jFo1bubfCzAd8bZT6nQ3he5tr+jypGufau9+WQKY0MHhTajqKTNfnlZQA==", + "dependencies": { + "whatwg-fetch": "^3.6.20" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -6912,6 +6921,11 @@ "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, + "node_modules/whatwg-fetch": { + "version": "3.6.20", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz", + "integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==" + }, "node_modules/whatwg-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", diff --git a/core/package.json b/core/package.json index 50e0fdf21..59631af6b 100644 --- a/core/package.json +++ b/core/package.json @@ -29,6 +29,7 @@ "llm-code-highlighter": "^0.0.3", "node-fetch": "^3.3.2", "node-html-markdown": "^1.3.0", + "ollama": "^0.4.6", "openai": "^4.20.1", "replicate": "^0.23.0", "socket.io-client": "^4.7.3", diff --git a/core/yarn.lock b/core/yarn.lock index 247947ed2..f0cf77790 100644 --- a/core/yarn.lock +++ b/core/yarn.lock @@ -3070,6 +3070,13 @@ obliterator@^2.0.1, obliterator@^2.0.2: resolved "https://registry.npmjs.org/obliterator/-/obliterator-2.0.4.tgz" integrity sha512-lgHwxlxV1qIg1Eap7LgIeoBWIMFibOjbrYPIPJZcI1mmGAI2m3lNYpK12Y+GBdPQ0U1hRwSord7GIaawz962qQ== +ollama@^0.4.6: + version "0.4.6" + resolved "https://registry.npmjs.org/ollama/-/ollama-0.4.6.tgz" + integrity sha512-/Im2atcM9hAxOgEi7mc5pG2G+MeN4jFo1bubfCzAd8bZT6nQ3he5tr+jypGufau9+WQKY0MHhTajqKTNfnlZQA== + dependencies: + whatwg-fetch "^3.6.20" + once@^1.3.0, once@^1.3.1, once@^1.4.0: version "1.4.0" resolved "https://registry.npmjs.org/once/-/once-1.4.0.tgz" @@ -3941,6 +3948,11 @@ webidl-conversions@^3.0.0: resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz" integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== +whatwg-fetch@^3.6.20: + version "3.6.20" + resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz" + integrity sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg== + whatwg-url@^5.0.0: version "5.0.0" resolved "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz" diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index a648a4260..274fb3c3e 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -94,6 +94,7 @@ "llm-code-highlighter": "^0.0.3", "node-fetch": "^3.3.2", "node-html-markdown": "^1.3.0", + "ollama": "^0.4.6", "openai": "^4.20.1", "replicate": "^0.23.0", "socket.io-client": "^4.7.3", diff --git a/extensions/vscode/yarn.lock b/extensions/vscode/yarn.lock index 9a1bcbe1f..62b3ffb4d 100644 --- a/extensions/vscode/yarn.lock +++ b/extensions/vscode/yarn.lock @@ -2307,6 +2307,7 @@ core-util-is@1.0.2: llm-code-highlighter "^0.0.3" node-fetch "^3.3.2" node-html-markdown "^1.3.0" + ollama "^0.4.6" openai "^4.20.1" replicate "^0.23.0" socket.io-client "^4.7.3"