UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

104 lines (103 loc) 3.63 kB
import { BaseLLM } from "./BaseLLM.js"; export class OpenRouterProvider extends BaseLLM { constructor() { super(OpenRouterProvider.providerConfig); } async executePrompt(prompt, options = {}) { if (!this.isConfigured()) { throw new Error("OpenRouter provider is not configured"); } const response = await fetch("https://openrouter.ai/api/v1/chat/completions", { method: "POST", headers: { Authorization: `Bearer ${this.config.apiKey}`, "HTTP-Referer": "https://contaigents.com", "X-Title": "Contaigents", "Content-Type": "application/json", }, body: JSON.stringify({ model: this.config.model, messages: [ { role: "user", content: prompt, }, ], temperature: options.temperature ?? 0.7, max_tokens: options.maxTokens, }), }); if (!response.ok) { const error = await response.json(); throw new Error(`OpenRouter API error: ${error.error?.message || "Unknown error"}`); } const data = await response.json(); return { content: data.choices[0].message.content, raw: data, }; } async executeConversation(messages, options = {}) { if (!(await this.isConfigured())) { throw new Error("OpenRouter provider is not configured"); } // Convert ConversationMessage to OpenRouter format (OpenAI-compatible) const openRouterMessages = messages.map(msg => ({ role: msg.role, content: msg.content })); const response = await fetch("https://openrouter.ai/api/v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.config.apiKey}`, "HTTP-Referer": "https://contaigents.com", "X-Title": "Contaigents CLI", }, body: JSON.stringify({ model: this.config.model, messages: openRouterMessages, temperature: options.temperature ?? 0.7, max_tokens: options.maxTokens, }), }); if (!response.ok) { const error = await response.json(); throw new Error(`OpenRouter API error: ${error.error?.message || "Unknown error"}`); } const data = await response.json(); return { content: data.choices[0].message.content, raw: data, }; } } OpenRouterProvider.providerConfig = { name: "OpenRouter", configFields: [ { name: "apiKey", label: "API Key", type: "password", required: true, }, { name: "model", label: "Model", type: "select", required: true, options: [ "deepseek/deepseek-r1-zero:free", "openai/gpt-4-turbo-preview", "anthropic/claude-3-opus", "anthropic/claude-3-sonnet", "meta-llama/llama-2-70b-chat", "mistral-ai/mistral-large", "mistral-ai/mistral-medium", "google/gemini-pro", "google/palm-2-chat-bison", ], default: "openai/gpt-4-turbo-preview", }, ], };