UNPKG

@yuchida-tamu/podcast-gen

Version:

AI-Powered Monologue Podcast Generator

73 lines 2.38 kB
import { APIClient } from '../core/APIClient.js'; import { validateApiKey } from '../utils/errors.js'; export class OpenAIService extends APIClient { client; llmConfig; constructor(client, config) { validateApiKey(); const llmConfig = { apiKey: process.env.OPENAI_API_KEY || '', model: 'gpt-4o', maxTokens: 500, ...config, }; // Map LLM config to API config for APIClient const apiConfig = { retries: 3, timeout: 30000, baseDelay: 1000, maxDelay: 10000, }; super(apiConfig); this.llmConfig = llmConfig; this.client = client; } // Implement the abstract fetch method from APIClient async fetch(request) { const response = await this.client.chat.completions.create({ model: this.llmConfig.model, max_tokens: this.llmConfig.maxTokens, messages: [ { role: 'system', content: request.systemPrompt, }, { role: 'user', content: request.userPrompt, }, ], }); // Validate response format if (!response.choices[0]?.message?.content) { throw new Error('Invalid response format from API'); } const content = response.choices[0].message.content.trim(); return { content, usage: { promptTokens: response.usage?.prompt_tokens || 0, completionTokens: response.usage?.completion_tokens || 0, totalTokens: response.usage?.total_tokens || 0, }, }; } // Implement LLMService interface async generateContent(request) { return this.executeWithRetry(request); } async isHealthy() { try { const testRequest = { systemPrompt: 'You are a helpful assistant.', userPrompt: 'Say "healthy" if you can respond.', }; const response = await this.generateContent(testRequest); return response.content.toLowerCase().includes('healthy'); } catch (error) { return false; } } } //# sourceMappingURL=OpenAIService.js.map