diff --git a/core/core.ts b/core/core.ts index c6b336c7d..6c8d20537 100644 --- a/core/core.ts +++ b/core/core.ts @@ -474,7 +474,7 @@ export class Core { config.models.find((model) => model.title?.startsWith(msg.data.title)); try { if (model) { - return model.listModels(); + return await model.listModels(); } else { if (msg.data.title === "Ollama") { const models = await new Ollama({ model: "" }).listModels(); diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index dcaa078ba..a1b691ec2 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -37,31 +37,29 @@ interface ModelFileParams { // See https://github.com/ollama/ollama/blob/main/docs/api.md interface BaseOptions { - model: string; // the model name + 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) + 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 + 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 + 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 = { @@ -196,9 +194,7 @@ class Ollama extends BaseLLM { }; } - private _convertMessage( - message: ChatMessage - ) { + private _convertMessage(message: ChatMessage) { if (typeof message.content === "string") { return message; } @@ -215,13 +211,13 @@ class Ollama extends BaseLLM { return { role: message.role, content: stripImages(message.content), - images + images, }; } private _getChatOptions( options: CompletionOptions, - messages: ChatMessage[] + messages: ChatMessage[], ): ChatOptions { return { model: this._getModel(), @@ -236,7 +232,7 @@ class Ollama extends BaseLLM { private _getGenerateOptions( options: CompletionOptions, prompt: string, - suffix?: string + suffix?: string, ): GenerateOptions { return { model: this._getModel(), @@ -271,7 +267,7 @@ class Ollama extends BaseLLM { Authorization: `Bearer ${this.apiKey}`, }, body: JSON.stringify(this._getGenerateOptions(options, prompt)), - signal + signal, }); let buffer = ""; @@ -312,7 +308,7 @@ class Ollama extends BaseLLM { Authorization: `Bearer ${this.apiKey}`, }, body: JSON.stringify(this._getChatOptions(options, messages)), - signal + signal, }); let buffer = ""; @@ -354,7 +350,6 @@ class Ollama extends BaseLLM { signal: AbortSignal, options: CompletionOptions, ): AsyncGenerator { - const response = await this.fetch(this.getEndpoint("api/generate"), { method: "POST", headers: { @@ -362,7 +357,7 @@ class Ollama extends BaseLLM { Authorization: `Bearer ${this.apiKey}`, }, body: JSON.stringify(this._getGenerateOptions(options, prefix, suffix)), - signal + signal, }); let buffer = ""; @@ -400,7 +395,11 @@ class Ollama extends BaseLLM { }, ); const data = await response.json(); - return data.models.map((model: any) => model.name); + if (response.ok) { + return data.models.map((model: any) => model.name); + } else { + throw new Error("Ollama is not running"); + } } } diff --git a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx index d16587427..cf201843c 100644 --- a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx +++ b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx @@ -15,7 +15,7 @@ import OllamaModelDownload from "../components/OllamaModelDownload"; import { OllamaStatus } from "../components/OllamaStatus"; import { useSubmitOnboarding } from "../hooks"; -const OLLAMA_CHECK_INTERVAL_MS = 3000; +const OLLAMA_CHECK_INTERVAL_MS = 10000; function OnboardingLocalTab() { const dispatch = useDispatch(); @@ -59,11 +59,15 @@ function OnboardingLocalTab() { try { const result = await ideMessenger.request("llm/listModels", { title: ONBOARDING_LOCAL_MODEL_TITLE, - }) - + }); if (result.status === "success") { - setDownloadedOllamaModels(result.content); - setIsOllamaConnected(true); + if (Array.isArray(result.content)) { + setDownloadedOllamaModels(result.content); + setIsOllamaConnected(true); + } else { + setDownloadedOllamaModels([]); + setIsOllamaConnected(false); + } } else { throw new Error("Failed to fetch models"); } diff --git a/gui/src/context/IdeMessenger.ts b/gui/src/context/IdeMessenger.ts index 0b5329c61..c5c4f9b3e 100644 --- a/gui/src/context/IdeMessenger.ts +++ b/gui/src/context/IdeMessenger.ts @@ -138,13 +138,13 @@ export class IdeMessenger implements IIdeMessenger { const handler = (event: any) => { if (event.data.messageId === messageId) { window.removeEventListener("message", handler); - resolve(event.data.data); + resolve(event.data.data as WebviewMessengerResult); } }; window.addEventListener("message", handler); this.post(messageType, data, messageId); - }) as any; + }); } async *streamRequest(