ai-switcher
Version:
A package which helps you switch between AI APIs using configurations, so that code changes are not required.
42 lines (41 loc) • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AIClient = void 0;
const anthropic_1 = require("./clients/anthropic");
const openai_1 = require("./clients/openai");
class AIClient {
constructor(config) {
if (!config.anthropicApiKey && !config.openaiApiKey) {
throw new Error("At least one provider API key must be provided");
}
this.clients = new Map();
this.defaultProvider = config.defaultProvider || 'openai';
this.defaultModel = config.defaultModel || '';
if (config.anthropicApiKey) {
this.clients.set('anthropic', new anthropic_1.AnthropicClient(config.anthropicApiKey));
}
if (config.openaiApiKey) {
this.clients.set('openai', new openai_1.OpenAIClient(config.openaiApiKey));
}
}
getClient(provider) {
const selectedProvider = provider || this.defaultProvider;
const client = this.clients.get(selectedProvider);
if (!client) {
throw new Error(`No client configured for provider: ${selectedProvider}`);
}
return client;
}
async createCompletion(messages, options = {}) {
const client = this.getClient(options.provider);
return client.createCompletion(messages, {
...options,
model: options.model || this.defaultModel,
});
}
async createEmbedding(text, provider) {
const client = this.getClient(provider);
return client.createEmbedding(text);
}
}
exports.AIClient = AIClient;