|
| 1 | +import axios from "axios" |
| 2 | +import { ModelInfo, ollamaDefaultModelInfo } from "@roo-code/types" |
| 3 | +import { z } from "zod" |
| 4 | + |
| 5 | +const OllamaModelDetailsSchema = z.object({ |
| 6 | + family: z.string(), |
| 7 | + families: z.array(z.string()), |
| 8 | + format: z.string(), |
| 9 | + parameter_size: z.string(), |
| 10 | + parent_model: z.string(), |
| 11 | + quantization_level: z.string(), |
| 12 | +}) |
| 13 | + |
| 14 | +const OllamaModelSchema = z.object({ |
| 15 | + details: OllamaModelDetailsSchema, |
| 16 | + digest: z.string(), |
| 17 | + model: z.string(), |
| 18 | + modified_at: z.string(), |
| 19 | + name: z.string(), |
| 20 | + size: z.number(), |
| 21 | +}) |
| 22 | + |
| 23 | +const OllamaModelInfoResponseSchema = z.object({ |
| 24 | + modelfile: z.string(), |
| 25 | + parameters: z.string(), |
| 26 | + template: z.string(), |
| 27 | + details: OllamaModelDetailsSchema, |
| 28 | + model_info: z.record(z.string(), z.any()), |
| 29 | + capabilities: z.array(z.string()).optional(), |
| 30 | +}) |
| 31 | + |
| 32 | +const OllamaModelsResponseSchema = z.object({ |
| 33 | + models: z.array(OllamaModelSchema), |
| 34 | +}) |
| 35 | + |
| 36 | +type OllamaModelsResponse = z.infer<typeof OllamaModelsResponseSchema> |
| 37 | + |
| 38 | +type OllamaModelInfoResponse = z.infer<typeof OllamaModelInfoResponseSchema> |
| 39 | + |
| 40 | +export const parseOllamaModel = (rawModel: OllamaModelInfoResponse): ModelInfo => { |
| 41 | + const contextKey = Object.keys(rawModel.model_info).find((k) => k.includes("context_length")) |
| 42 | + const contextWindow = contextKey ? rawModel.model_info[contextKey] : undefined |
| 43 | + |
| 44 | + const modelInfo: ModelInfo = Object.assign({}, ollamaDefaultModelInfo, { |
| 45 | + description: `Family: ${rawModel.details.family}, Context: ${contextWindow}, Size: ${rawModel.details.parameter_size}`, |
| 46 | + contextWindow: contextWindow || ollamaDefaultModelInfo.contextWindow, |
| 47 | + supportsPromptCache: true, |
| 48 | + supportsImages: rawModel.capabilities?.includes("vision"), |
| 49 | + supportsComputerUse: false, |
| 50 | + maxTokens: contextWindow || ollamaDefaultModelInfo.contextWindow, |
| 51 | + }) |
| 52 | + |
| 53 | + return modelInfo |
| 54 | +} |
| 55 | + |
| 56 | +export async function getOllamaModels(baseUrl = "http://localhost:11434"): Promise<Record<string, ModelInfo>> { |
| 57 | + const models: Record<string, ModelInfo> = {} |
| 58 | + |
| 59 | + // clearing the input can leave an empty string; use the default in that case |
| 60 | + baseUrl = baseUrl === "" ? "http://localhost:11434" : baseUrl |
| 61 | + |
| 62 | + try { |
| 63 | + if (!URL.canParse(baseUrl)) { |
| 64 | + return models |
| 65 | + } |
| 66 | + |
| 67 | + const response = await axios.get<OllamaModelsResponse>(`${baseUrl}/api/tags`) |
| 68 | + const parsedResponse = OllamaModelsResponseSchema.safeParse(response.data) |
| 69 | + let modelInfoPromises = [] |
| 70 | + |
| 71 | + if (parsedResponse.success) { |
| 72 | + for (const ollamaModel of parsedResponse.data.models) { |
| 73 | + modelInfoPromises.push( |
| 74 | + axios |
| 75 | + .post<OllamaModelInfoResponse>(`${baseUrl}/api/show`, { |
| 76 | + model: ollamaModel.model, |
| 77 | + }) |
| 78 | + .then((ollamaModelInfo) => { |
| 79 | + models[ollamaModel.name] = parseOllamaModel(ollamaModelInfo.data) |
| 80 | + }), |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + await Promise.all(modelInfoPromises) |
| 85 | + } else { |
| 86 | + console.error(`Error parsing Ollama models response: ${JSON.stringify(parsedResponse.error, null, 2)}`) |
| 87 | + } |
| 88 | + } catch (error) { |
| 89 | + if (error.code === "ECONNREFUSED") { |
| 90 | + console.info(`Failed connecting to Ollama at ${baseUrl}`) |
| 91 | + } else { |
| 92 | + console.warn(`Error fetching Ollama models: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return models |
| 97 | +} |
0 commit comments