From 540b196aeff26623598b3ec9d88e276a062dd483 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sat, 26 Oct 2024 22:50:54 -0700 Subject: [PATCH] add missing support for stream etc. : --- core/llm/llms/Ollama.ts | 141 +++++++++++++++++++++++++++++++--------- 1 file changed, 109 insertions(+), 32 deletions(-) diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 2894cac2e..604c382b6 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -8,6 +8,59 @@ import { stripImages } from "../images.js"; import { BaseLLM } from "../index.js"; import { streamResponse } from "../stream.js"; +interface OllamaChatMessage extends ChatMessage { + images?: string[]; +} + +// See https://github.com/ollama/ollama/blob/main/docs/modelfile.md for details on each parameter +interface ModelFileParams { + mirostat?: number; + mirostat_eta?: number; + mirostat_tau?: number; + num_ctx?: number; + repeat_last_n?: number; + repeat_penalty?: number; + temperature?: number; + seed?: number; + stop?: string | string[]; + tfs_z?: number; + num_predict?: number; + top_k?: number; + top_p?: number; + min_p?: number; + // deprecated? + num_thread?: number; + num_gqa?: number; + num_gpu?: number; +} + +// See https://github.com/ollama/ollama/blob/main/docs/api.md +interface BaseOptions { + model: string; // the model name + options?: ModelFileParams; // additional model parameters listed in the documentation for the Modelfile such as temperature + format?: "json"; // the format to return a response in. Currently, the only accepted value is json + stream?: boolean; // if false the response will be returned as a single response object, rather than a stream of objects + keep_alive?: number; // controls how long the model will stay loaded into memory following the request (default: 5m) +} + +interface GenerateOptions extends BaseOptions { + prompt: string; // the prompt to generate a response for + suffix?: string; // the text after the model response + images?: string[]; // a list of base64-encoded images (for multimodal models such as llava) + system?: string; // system message to (overrides what is defined in the Modelfile) + template?: string; // the prompt template to use (overrides what is defined in the Modelfile) + context?: string; // the context parameter returned from a previous request to /generate, this can be used to keep a short conversational memory + raw?: boolean; // if true no formatting will be applied to the prompt. You may choose to use the raw parameter if you are specifying a full templated prompt in your request to the API +} + +interface ChatOptions extends BaseOptions { + messages: OllamaChatMessage[]; // the messages of the chat, this can be used to keep a chat memory + // Not supported yet - tools: tools for the model to use if supported. Requires stream to be set to false + // And correspondingly, tool calls in OllamaChatMessage +} + + + class Ollama extends BaseLLM { static providerName: ModelProvider = "ollama"; static defaultOptions: Partial = { @@ -63,7 +116,7 @@ class Ollama extends BaseLLM { this.completionOptions.stop.push(JSON.parse(value)); } catch (e) { console.warn( - 'Error parsing stop parameter value "{value}: ${e}', + `Error parsing stop parameter value "{value}: ${e}`, ); } break; @@ -128,49 +181,73 @@ class Ollama extends BaseLLM { ); } - private _convertMessage(message: ChatMessage) { + private _getModelFileParams(options: CompletionOptions): ModelFileParams { + return { + temperature: options.temperature, + top_p: options.topP, + top_k: options.topK, + num_predict: options.maxTokens, + stop: options.stop, + num_ctx: this.contextLength, + mirostat: options.mirostat, + num_thread: options.numThreads, + min_p: options.minP, + }; + } + + private _convertMessage( + message: ChatMessage + ) { if (typeof message.content === "string") { return message; } + const images: string[] = []; + message.content.forEach((part) => { + if (part.type === "imageUrl" && part.imageUrl) { + const image = part.imageUrl?.url.split(",").at(-1); + if (image) { + images.push(image); + } + } + }); return { role: message.role, content: stripImages(message.content), - images: message.content - .filter((part) => part.type === "imageUrl") - .map((part) => part.imageUrl?.url.split(",").at(-1)), + images }; } - private _convertArgs( + private _getChatOptions( options: CompletionOptions, - prompt: string | ChatMessage[], - suffix?: string, - ) { - const finalOptions: any = { + messages: ChatMessage[] + ): ChatOptions { + return { model: this._getModel(), - raw: true, + messages: messages.map(this._convertMessage), + options: this._getModelFileParams(options), keep_alive: options.keepAlive ?? 60 * 30, // 30 minutes - suffix, - options: { - temperature: options.temperature, - top_p: options.topP, - top_k: options.topK, - num_predict: options.maxTokens, - stop: options.stop, - num_ctx: this.contextLength, - mirostat: options.mirostat, - num_thread: options.numThreads, - }, + stream: options.stream, + format: "json", }; + } - if (typeof prompt === "string") { - finalOptions.prompt = prompt; - } else { - finalOptions.messages = prompt.map(this._convertMessage); - } - - return finalOptions; + private _getGenerateOptions( + options: CompletionOptions, + prompt: string, + suffix?: string + ): GenerateOptions { + return { + model: this._getModel(), + prompt, + suffix, + raw: options.raw, + options: this._getModelFileParams(options), + keep_alive: options.keepAlive ?? 60 * 30, // 30 minutes + stream: options.stream, + format: "json" + // Not supported yet: context, images, system, template + }; } private getEndpoint(endpoint: string): URL { @@ -192,7 +269,7 @@ class Ollama extends BaseLLM { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, }, - body: JSON.stringify(this._convertArgs(options, prompt)), + body: JSON.stringify(this._getGenerateOptions(options, prompt)), }); let buffer = ""; @@ -231,7 +308,7 @@ class Ollama extends BaseLLM { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, }, - body: JSON.stringify(this._convertArgs(options, messages)), + body: JSON.stringify(this._getChatOptions(options, messages)), }); let buffer = ""; @@ -278,7 +355,7 @@ class Ollama extends BaseLLM { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, }, - body: JSON.stringify(this._convertArgs(options, prefix, suffix)), + body: JSON.stringify(this._getGenerateOptions(options, prefix, suffix)), }); let buffer = "";