| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 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<number[]> {
- 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<number[][]> {
- 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;
- }
- }
|