UNPKG

@meldscience/meld

Version:

pipeable one-shot prompt scripting toolkit

52 lines (46 loc) 1.28 kB
import Anthropic from '@anthropic-ai/sdk'; import { ToolError } from '../errors'; export interface AIProvider { sendPrompt(options: { model: string; systemPrompt: string; userPrompt: string; }): Promise<string>; } export class AnthropicProvider implements AIProvider { private client: Anthropic; constructor(apiKey: string) { if (!apiKey) { throw new ToolError( 'Anthropic API key is required', 'MISSING_API_KEY' ); } this.client = new Anthropic({ apiKey }); } async sendPrompt({ model, systemPrompt, userPrompt }: { model: string; systemPrompt?: string; userPrompt: string; }): Promise<string> { try { const messages: Array<{ role: 'user' | 'assistant'; content: string }> = []; if (systemPrompt) { messages.push({ role: 'assistant', content: systemPrompt }); } messages.push({ role: 'user', content: userPrompt }); const response = await this.client.messages.create({ model: model, messages: messages, max_tokens: 4096, }); return response.content[0].text; } catch (error: any) { throw new ToolError( `Anthropic API error: ${error.message}`, 'API_ERROR', error ); } } }