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.
189 lines • 7.92 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 __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeminiClient = void 0;
const http_client_1 = require("../core/http-client");
const expo_1 = require("../strategies/retry/expo");
const ai_builder_1 = require("../builder/ai.builder");
const google_stream_1 = require("../strategies/stream/google.stream");
const logger_1 = require("../utils/logger");
/**
*
* "generationConfig": {
"temperature": 0.45,
"thinkingConfig": {
"thinkingBudget": -1,
},
"responseMimeType": "text/plain",
},
*/
class GeminiClient {
constructor(config) {
var _a, _b, _c, _d, _e;
this.endpoint = 'https://generativelanguage.googleapis.com';
this.retryHandler = new expo_1.RetryHandler({
maxRetries: (_b = (_a = config.retryConfig) === null || _a === void 0 ? void 0 : _a.maxRetries) !== null && _b !== void 0 ? _b : 3,
retryDelay: (_d = (_c = config.retryConfig) === null || _c === void 0 ? void 0 : _c.retryDelay) !== null && _d !== void 0 ? _d : 1000,
});
this.httpClient = new http_client_1.HttpClient(config, (_e = config.baseUrl) !== null && _e !== void 0 ? _e : this.endpoint, this.retryHandler);
this.streamProcessor = new google_stream_1.GoogleStreamProcessor();
}
convertFromGeminiResponse(response) {
var _a, _b, _c, _d;
return {
resp_id: response.responseId,
model: response.modelVersion,
output: ((_a = response.candidates[0]) === null || _a === void 0 ? void 0 : _a.content.parts.map(part => ('text' in part ? part.text : '')).join('')) || '',
usage: {
input_tokens: (_b = response.usageMetadata) === null || _b === void 0 ? void 0 : _b.promptTokenCount,
output_tokens: (_c = response.usageMetadata) === null || _c === void 0 ? void 0 : _c.candidatesTokenCount,
total_tokens: (_d = response.usageMetadata) === null || _d === void 0 ? void 0 : _d.totalTokenCount,
},
status: 'success',
};
}
async generateContent(request) {
ai_builder_1.MessageValidator.validateContents(request.contents);
const endpoint = `/v1beta/models/${this.httpClient.getConfig().model}:generateContent?key=${this.httpClient.getConfig().apiKey}`;
const data = await this.httpClient.request({
method: 'POST',
body: JSON.stringify(request),
}, false, endpoint);
return this.convertFromGeminiResponse(data);
}
convertReadableToResponse(readable) {
return new Response(readable, {
headers: new Headers(),
status: 200,
statusText: 'OK',
});
}
async generateContentStream(request, options) {
ai_builder_1.MessageValidator.validateContents(request.contents);
const endpoint = `/v1beta/models/${this.httpClient.getConfig().model}:streamGenerateContent`;
const response = await this.httpClient.streamRequest({
method: 'POST',
body: JSON.stringify(request),
}, endpoint);
return this.streamProcessor.processStream(this.convertReadableToResponse(response), options);
}
async *generateContentStreamAsync(request) {
logger_1.logger.info('Generating content stream asynchronously');
try {
ai_builder_1.MessageValidator.validateContents(request.contents);
const endpoint = `/v1beta/models/${this.httpClient.getConfig().model}:streamGenerateContent?key=${this.httpClient.getConfig().apiKey}`;
const response = await this.httpClient.streamRequest({
method: 'POST',
body: JSON.stringify(request),
}, endpoint);
yield* this.streamProcessor.createAsyncGenerator(this.convertReadableToResponse(response));
}
catch (error) {
logger_1.logger.error('Error in generateContentStreamAsync:', error);
throw error;
}
}
async chat(message, options = {}) {
var _a;
const contents = ai_builder_1.ContentBuilder.textOnly(message);
const request = {
contents,
generationConfig: (_a = options.configs) === null || _a === void 0 ? void 0 : _a.generationConfig,
tools: options.tools,
};
if (options.systemInstruction) {
request.systemInstruction = {
parts: [{ text: options.systemInstruction }],
};
}
const response = await this.generateContent(request);
return response;
}
async chatStream(message, options = {}) {
var _a;
const contents = ai_builder_1.ContentBuilder.textOnly(message);
const request = {
contents,
generationConfig: (_a = options.configs) === null || _a === void 0 ? void 0 : _a.generationConfig,
tools: options.tools,
};
if (options.systemInstruction) {
request.systemInstruction = {
parts: [{ text: options.systemInstruction }],
};
}
const response = await this.generateContentStream(request, options.streamOptions);
return response;
}
async continueConversation(contents, options = {}) {
const request = {
contents,
generationConfig: options.generationConfig,
tools: options.tools,
};
const response = await this.generateContent(request);
return response;
}
async callFunction(message, tools, options = {}) {
const request = {
contents: ai_builder_1.ContentBuilder.textOnly(message),
tools,
generationConfig: options.generationConfig,
};
if (options.systemInstruction) {
request.systemInstruction = {
parts: [{ text: options.systemInstruction }],
};
}
return this.generateContent(request);
}
// Thinking mode
async generateWithThinking(message, thinkingBudget = -1, // -1 for dynamic thinking
options = {}) {
const request = {
contents: ai_builder_1.ContentBuilder.textOnly(message),
generationConfig: {
thinkingConfig: {
thinkingBudget,
},
},
};
if (options.systemInstruction) {
request.systemInstruction = {
parts: [{ text: options.systemInstruction }],
};
}
return this.generateContentStream(request, {
onThinking: options.onThinking,
});
}
get stream() {
return this.streamProcessor;
}
updateConfig(newConfig) {
this.httpClient.updateConfig(newConfig);
}
getConfig() {
return this.httpClient.getConfig();
}
}
exports.GeminiClient = GeminiClient;
__exportStar(require("../types"), exports);
__exportStar(require("../strategies/retry/expo"), exports);
__exportStar(require("../core/http-client"), exports);
__exportStar(require("../builder/ai.builder"), exports);
__exportStar(require("../strategies/stream/google.stream"), exports);
//# sourceMappingURL=google.adapter.js.map