import { config } from '../config'; export interface EmbeddingResponse { embedding: number[]; } export class OllamaService { private baseUrl: string; private model: string; constructor() { this.baseUrl = config.ollama.url; this.model = config.ollama.embeddingModel; } async createEmbedding(text: string): Promise { try { const response = await fetch(`${this.baseUrl}/api/embeddings`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ model: this.model, prompt: text, }), }); if (!response.ok) { throw new Error(`Ollama API error: ${response.statusText}`); } const data: EmbeddingResponse = await response.json(); return data.embedding; } catch (error) { console.error('Error creating embedding:', error); throw error; } } async createEmbeddings(texts: string[]): Promise { const embeddings: number[][] = []; console.log(`Creating embeddings for ${texts.length} texts...`); for (let i = 0; i < texts.length; i++) { const text = texts[i]; const embedding = await this.createEmbedding(text); embeddings.push(embedding); // Progress indicator for large batches if ((i + 1) % 10 === 0 || i === texts.length - 1) { console.log(`Progress: ${i + 1}/${texts.length} embeddings created`); } } return embeddings; } }