All files / src/llm openai.ts

100% Statements 8/8
80% Branches 4/5
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 259x       9x         3x 1x   2x 2x       2x       1x      
import OpenAI from 'openai';
import { LLMProvider } from './provider';
import { SeraphConfig } from '../config';
 
export class OpenAIProvider implements LLMProvider {
  private openai: OpenAI;
  private model: string;
 
  constructor(config: SeraphConfig) {
    if (!config.apiKey) {
      throw new Error('OpenAI API key not found in config.');
    }
    this.openai = new OpenAI({ apiKey: config.apiKey });
    this.model = config.llm?.model || 'gpt-4-turbo';
  }
 
  async generate(prompt: string): Promise<string> {
    const response = await this.openai.chat.completions.create({
      model: this.model,
      messages: [{ role: 'user', content: prompt }],
    });
    return response.choices[0].message.content || '';
  }
}