UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

85 lines 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OllamaGptClient = void 0; const v4_1 = require("zod/v4"); const GptModelCapabilityException_1 = require("../exceptions/GptModelCapabilityException"); const GptModelNotFoundException_1 = require("../exceptions/GptModelNotFoundException"); const GptPlatformNotReachableException_1 = require("../exceptions/GptPlatformNotReachableException"); const Logger_1 = require("../utils/Logger"); const GptClient_1 = require("./GptClient"); const OpenAiGptClient_1 = require("./OpenAiGptClient"); /** * A GPT client for Ollama, a local LLM runner that exposes an OpenAI-compatible API. * Delegates to {@link OpenAiGptClient} for request handling, with Ollama-specific * overrides for ping and structured output. */ class OllamaGptClient extends GptClient_1.GptClient { constructor(ollamaConfig) { super(ollamaConfig); this.apiUrl = ollamaConfig.apiUrl ?? OllamaGptClient.DEFAULT_API_URL; this.delegate = new OpenAiGptClient_1.OpenAiGptClient({ type: 'OPENAI', modelName: ollamaConfig.modelName, apiKey: 'ollama', }, this.apiUrl); } async ping(options) { const signal = options?.signal ?? AbortSignal.timeout(10_000); const modelName = this.config.modelName; let resp; try { resp = await fetch(`${this.apiUrl}/api/show`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: modelName }), signal, }); } catch (error) { if (error instanceof TypeError) { Logger_1.appLogger.error('Failed to reach Ollama due to exception', error); throw new GptPlatformNotReachableException_1.GptPlatformNotReachableException(this.config.type); } throw error; } if (resp.status === 404) { throw new GptModelNotFoundException_1.GptModelNotFoundException(this.config.type, modelName); } if (!resp.ok) { throw new GptPlatformNotReachableException_1.GptPlatformNotReachableException(this.config.type); } const data = v4_1.z .object({ capabilities: v4_1.z.array(v4_1.z.string()).default([]) }) .parse(await resp.json()); const requiredCapabilities = ['completion', 'tools', 'vision']; const missingCapabilities = requiredCapabilities.filter((cap) => !data.capabilities.includes(cap)); if (missingCapabilities.length > 0) { throw new GptModelCapabilityException_1.GptModelCapabilityException(this.config.type, modelName, missingCapabilities); } } async getMessage(messages, options) { return this.delegate.getMessage(messages, options); } async getStructuredOutput(messages, zodSchema, options) { const resp = await this.getToolCalls(messages, [ { name: 'output', description: 'The schema of the expected response structure.', inputSchema: zodSchema, }, ], options); const toolCall = resp.proposedToolCalls.at(0); return { completionTokensUsed: resp.completionTokensUsed, promptTokensUsed: resp.promptTokensUsed, type: 'structured_output', output: (0, GptClient_1.parseOrLogAndThrow)(toolCall?.parameters, zodSchema), }; } async getToolCalls(messages, tools, options) { return this.delegate.getToolCalls(messages, tools, options); } } exports.OllamaGptClient = OllamaGptClient; OllamaGptClient.DEFAULT_API_URL = 'http://localhost:11434'; //# sourceMappingURL=OllamaGptClient.js.map