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.
185 lines • 7.19 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");
/**
*
* "generationConfig": {
"temperature": 0.45,
"thinkingConfig": {
"thinkingBudget": -1,
},
"responseMimeType": "text/plain",
},
*/
class GeminiClient {
constructor(config) {
var _a;
this.retryHandler = new expo_1.RetryHandler({
maxRetries: config.maxRetries,
retryDelay: config.retryDelay,
});
this.httpClient = new http_client_1.HttpClient(config, (_a = config.baseUrl) !== null && _a !== void 0 ? _a : 'https://generativelanguage.googleapis.com', this.retryHandler);
this.streamProcessor = new google_stream_1.StreamProcessor();
}
async generateContent(request) {
ai_builder_1.MessageValidator.validateContents(request.contents);
const endpoint = `/v1beta/models/${this.httpClient.getConfig().model}:generateContent`;
return this.httpClient.request(endpoint, {
method: 'POST',
body: JSON.stringify(request),
});
}
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(endpoint, {
method: 'POST',
body: JSON.stringify(request),
});
return this.streamProcessor.processStream(this.convertReadableToResponse(response), options);
}
async *generateContentStreamAsync(request) {
ai_builder_1.MessageValidator.validateContents(request.contents);
const endpoint = `/v1beta/models/${this.httpClient.getConfig().model}:streamGenerateContent`;
const response = await this.httpClient.streamRequest(endpoint, {
method: 'POST',
body: JSON.stringify(request),
});
yield* this.streamProcessor.createAsyncGenerator(this.convertReadableToResponse(response));
}
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 analyzeImage(base64Image, mimeType, prompt, options = {}) {
const contents = ai_builder_1.ContentBuilder.create()
.addImageFromBase64(base64Image, mimeType, prompt)
.build();
return this.generateContent({
contents,
generationConfig: options.generationConfig,
});
}
async analyzeDocument(base64Document, mimeType, prompt, options = {}) {
const request = {
contents: ai_builder_1.ContentBuilder.create()
.addDocumentFromBase64(base64Document, mimeType, prompt)
.build(),
generationConfig: options.generationConfig,
};
return this.generateContent(request);
}
// Function calling
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;
}
// Configuration methods
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