🎨 use ollama official client

This commit is contained in:
Nate Sesti 2024-02-07 22:39:48 -08:00
parent 3587a96f2f
commit 4a120b70b5
6 changed files with 43 additions and 32 deletions

View file

@ -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<ChatMessage> {
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,
};
}
}

14
core/package-lock.json generated
View file

@ -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",

View file

@ -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",

View file

@ -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"

View file

@ -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",

View file

@ -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"