All files / src/llm anthropic.ts

90% Statements 9/10
100% Branches 4/4
100% Functions 2/2
90% Lines 9/10

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 25 26 27 28 29 30 319x       9x         3x 1x   2x 2x       2x           1x 1x            
import Anthropic from '@anthropic-ai/sdk';
import { LLMProvider } from './provider';
import { SeraphConfig } from '../config';
 
export class AnthropicProvider implements LLMProvider {
  private anthropic: Anthropic;
  private model: string;
 
  constructor(config: SeraphConfig) {
    if (!config.apiKey) {
      throw new Error('Anthropic API key not found in config.');
    }
    this.anthropic = new Anthropic({ apiKey: config.apiKey });
    this.model = config.llm?.model || 'claude-3-opus-20240229';
  }
 
  async generate(prompt: string): Promise<string> {
    const response = await this.anthropic.messages.create({
      model: this.model,
      max_tokens: 1024,
      messages: [{ role: 'user', content: prompt }],
    });
    
    if (response.content[0].type === 'text') {
      return response.content[0].text;
    }
 
    return '';
  }
}