|
| 1 | +import { EmbeddingAdapter, EmbeddingConfig } from "../embeddings"; |
| 2 | + |
| 3 | +export interface OpenAIConfig extends EmbeddingConfig { |
| 4 | + apiKey: string; |
| 5 | + model?: string; |
| 6 | + baseURL?: string; |
| 7 | +} |
| 8 | + |
| 9 | +export class OpenAIEmbeddingAdapter extends EmbeddingAdapter { |
| 10 | + private apiKey: string; |
| 11 | + private model: string; |
| 12 | + private baseURL: string; |
| 13 | + private initialized: boolean = false; |
| 14 | + |
| 15 | + constructor(config: OpenAIConfig) { |
| 16 | + super(config); |
| 17 | + if (!config.apiKey) { |
| 18 | + throw new Error("OpenAI API key is required"); |
| 19 | + } |
| 20 | + this.apiKey = config.apiKey; |
| 21 | + this.model = config.model || "text-embedding-3-small"; |
| 22 | + this.baseURL = config.baseURL || "https://api.openai.com/v1"; |
| 23 | + } |
| 24 | + |
| 25 | + async initialize(): Promise<void> { |
| 26 | + console.log(`[OpenAI] Initializing with model: ${this.model}...`); |
| 27 | + this.isInitializing = true; |
| 28 | + if (!this.apiKey.startsWith("sk-")) { |
| 29 | + console.warn( |
| 30 | + "[OpenAI] API key doesn't start with 'sk-', this might cause issues", |
| 31 | + ); |
| 32 | + } |
| 33 | + this.initialized = true; |
| 34 | + this.isInitializing = false; |
| 35 | + console.log("[OpenAI] Adapter initialized successfully"); |
| 36 | + } |
| 37 | + |
| 38 | + async generateEmbedding(text: string): Promise<number[]> { |
| 39 | + if (!this.initialized) { |
| 40 | + throw new Error( |
| 41 | + "OpenAI adapter not initialized. Call initialize() first.", |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + if (this.isInitializing) { |
| 46 | + throw new Error( |
| 47 | + "OpenAI adapter is initializing. Please wait a moment and try again.", |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + const response = await fetch(`${this.baseURL}/embeddings`, { |
| 53 | + method: "POST", |
| 54 | + headers: { |
| 55 | + Authorization: `Bearer ${this.apiKey}`, |
| 56 | + "Content-Type": "application/json", |
| 57 | + }, |
| 58 | + body: JSON.stringify({ |
| 59 | + input: text, |
| 60 | + model: this.model, |
| 61 | + }), |
| 62 | + }); |
| 63 | + |
| 64 | + if (!response.ok) { |
| 65 | + throw new Error( |
| 66 | + `OpenAI API error: ${response.status} ${response.statusText}`, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + const data = (await response.json()) as any; |
| 71 | + |
| 72 | + if (!data.data || !data.data[0] || !data.data[0].embedding) { |
| 73 | + throw new Error("Invalid response format from OpenAI API"); |
| 74 | + } |
| 75 | + |
| 76 | + return data.data[0].embedding; |
| 77 | + } catch (error) { |
| 78 | + console.error(`[OpenAI] Error generating embedding: ${error}`); |
| 79 | + throw error; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + getName(): string { |
| 84 | + return `OpenAI (${this.model})`; |
| 85 | + } |
| 86 | + |
| 87 | + isInitialized(): boolean { |
| 88 | + return this.initialized; |
| 89 | + } |
| 90 | + |
| 91 | + async cleanup(): Promise<void> { |
| 92 | + this.initialized = false; |
| 93 | + console.log("[OpenAI] Adapter cleaned up"); |
| 94 | + } |
| 95 | +} |
0 commit comments