llmforge
Version:
One API, every AI model, instant switching. Change from GPT-4 to Gemini to local models with a single config update. LLMForge is the lightweight, TypeScript-first solution for multi-provider AI applications with zero vendor lock-in.
124 lines • 4.96 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunnerClient = void 0;
class RunnerClient {
constructor(config) {
this.config = config;
this.LLMClients = [];
if (!this.config.llmConfig) {
throw new Error('LLM configuration is required.');
}
this.setLLMConfigToDefault().catch(error => {
throw new Error(`Failed to set LLM configuration to default: ${error.message}`);
});
}
static async create(config) {
const client = new RunnerClient(config);
await client.loadLLMClients();
return client;
}
async setLLMConfigToDefault() {
const normalize = (llmConfig) => ({
...llmConfig,
timeout: llmConfig.timeout || 30000,
priority: llmConfig.priority || 1,
retryConfig: llmConfig.retryConfig || { maxRetries: 0, retryDelay: 1000 },
generationConfig: llmConfig.generationConfig || {
responseMimeType: 'text/plain',
temperature: 0.7,
topP: 1.0,
topK: 40,
maxOutputTokens: 1024,
stopSequences: [],
},
stream: llmConfig.stream || false,
});
if (Array.isArray(this.config.llmConfig)) {
this.config.llmConfig = this.config.llmConfig.map(normalize);
}
else {
this.config.llmConfig = normalize(this.config.llmConfig);
}
}
async loadLLMClients() {
const { Factory } = await Promise.resolve().then(() => __importStar(require('./providers/provider.factory')));
const llmConfigs = Array.isArray(this.config.llmConfig) ? this.config.llmConfig : [this.config.llmConfig];
for (const llmConfig of llmConfigs) {
const client = await new Factory(llmConfig).create();
this.LLMClients.push([llmConfig, client]);
}
this.LLMClients.sort((a, b) => a[0].priority - b[0].priority);
}
async run(content) {
if (this.LLMClients.length === 0) {
throw new Error('No LLM clients available to run the content.');
}
let isFallbackUsed = false;
let reasonForFallback = [];
for (const [llmConfig, client] of this.LLMClients) {
try {
const response = llmConfig.stream ? await client.generateContentStreamAsync({ contents: content }) : await client.generateContent({ contents: content });
return response;
}
catch (error) {
reasonForFallback.push({
client: client.constructor.name,
error: error.message,
});
isFallbackUsed = true;
}
if (!this.config.enableFallback)
break;
}
if (isFallbackUsed && this.config.enableFallback) {
return {
resp_id: undefined,
output: '',
created_at: Date.now(),
model: 'fallback',
usage: { input_tokens: 0, output_tokens: 0, total_tokens: 0 },
status: 'fallback',
fallback: {
isUsed: true,
reason: reasonForFallback.map(r => `${r.client}: ${r.error}`).join(', '),
},
};
}
throw new Error(`All LLM clients failed: ${reasonForFallback.map(r => `${r.client}: ${r.error}`).join(', ')}`);
}
}
exports.RunnerClient = RunnerClient;
//# sourceMappingURL=index.js.map