UNPKG

claude-flow-novice

Version:

Claude Flow Novice - Advanced orchestration platform for multi-agent AI workflows with CFN Loop architecture Includes Local RuVector Accelerator and all CFN skills for complete functionality.

67 lines (66 loc) 2.41 kB
/** * OpenAI Embedding Provider */ import { EmbeddingError } from './provider.js'; export class OpenAIEmbeddingProvider { name = 'openai'; dimensions; apiKey; model; baseUrl; constructor(config){ this.apiKey = config.apiKey; this.model = config.model ?? 'text-embedding-3-small'; this.baseUrl = config.baseUrl ?? 'https://api.openai.com/v1'; this.dimensions = config.dimensions ?? 1536; } async embed(text) { const results = await this.embedBatch([ text ]); return results[0]; } async embedBatch(texts) { if (texts.length === 0) return []; try { const response = await fetch(`${this.baseUrl}/embeddings`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ model: this.model, input: texts, dimensions: this.dimensions }) }); if (!response.ok) { const error = await response.text(); throw new EmbeddingError(`OpenAI API error: ${response.status} ${error}`, this.name); } const data = await response.json(); const sorted = data.data.sort((a, b)=>a.index - b.index); return sorted.map((item)=>({ embedding: item.embedding, model: data.model, tokenCount: Math.floor(data.usage.total_tokens / texts.length) })); } catch (error) { if (error instanceof EmbeddingError) throw error; throw new EmbeddingError(`Failed to generate embeddings: ${error.message}`, this.name, error); } } } export function createOpenAIProvider(config) { const apiKey = config?.apiKey ?? process.env.OPENAI_API_KEY ?? process.env.ZAI_API_KEY; if (!apiKey) { throw new EmbeddingError('OpenAI API key required. Set OPENAI_API_KEY or ZAI_API_KEY.', 'openai'); } const baseUrl = config?.baseUrl ?? (process.env.ZAI_API_KEY ? 'https://api.zai.com/v1' : undefined); return new OpenAIEmbeddingProvider({ apiKey, baseUrl, ...config }); } //# sourceMappingURL=openai.js.map