All files / providers OllamaProvider.ts

47.36% Statements 9/19
0% Branches 0/12
50% Functions 2/4
47.36% Lines 9/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69    1x 4x 4x     4x 4x 4x       4x 4x         4x                                                                                                  
import { AIProvider, AIResponse, ChatOptions, Message } from '../core/interfaces';
 
export class OllamaProvider implements AIProvider {
  readonly name = 'Ollama (Local)';
  readonly type = 'ollama' as const;
 
  constructor(
    readonly modelName: string,
    private readonly endpoint: string,
    private readonly timeout: number
  ) {}
 
  async isAvailable(): Promise<boolean> {
    try {
      const resp = await fetch(`${this.endpoint}/api/tags`, {
        signal: AbortSignal.timeout(2000),
      });
      return resp.ok;
    } catch {
      return false;
    }
  }
 
  async chat(messages: Message[], options: ChatOptions = {}): Promise<AIResponse> {
    const body = {
      model: this.modelName,
      messages,
      stream: false,
      options: {
        num_predict: options.maxTokens ?? 4096,
        temperature: options.temperature ?? 0.1,
      },
    };
 
    const resp = await fetch(`${this.endpoint}/api/chat`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(this.timeout),
    });
 
    Iif (!resp.ok) {
      const err = await resp.text();
      throw new Error(`Ollama ${resp.status}: ${err}`);
    }
 
    const data = await resp.json() as {
      message: { content: string };
      model: string;
      prompt_eval_count?: number;
      eval_count?: number;
    };
 
    return {
      content: data.message?.content ?? '',
      model: data.model,
      usage: {
        promptTokens: data.prompt_eval_count ?? 0,
        completionTokens: data.eval_count ?? 0,
      },
    };
  }
 
  async complete(prompt: string, options?: ChatOptions): Promise<string> {
    const resp = await this.chat([{ role: 'user', content: prompt }], options);
    return resp.content;
  }
}