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.
90 lines • 5.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GroqClient = void 0;
const groq_stream_1 = require("../strategies/stream/groq.stream");
const expo_1 = require("../strategies/retry/expo");
const google_adapter_1 = require("./google.adapter");
const comman_1 = require("./comman");
const logger_1 = require("../utils/logger");
class GroqClient {
constructor(config, baseUrl, retryHandler) {
var _a, _b, _c, _d;
this.retryHandler = new expo_1.RetryHandler({
maxRetries: (_b = (_a = config.retryConfig) === null || _a === void 0 ? void 0 : _a.maxRetries) !== null && _b !== void 0 ? _b : 1,
retryDelay: (_d = (_c = config.retryConfig) === null || _c === void 0 ? void 0 : _c.retryDelay) !== null && _d !== void 0 ? _d : 0,
});
this.httpClient = new google_adapter_1.HttpClient(config, 'https://api.groq.com/', this.retryHandler);
this.streamProcessor = new groq_stream_1.GroqStreamProcessor();
}
payloadConstructor(contents) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
return {
messages: comman_1.CommanMethods.omitModelToAssistantInContents(contents.contents),
model: this.httpClient.getConfig().model,
temperature: (_d = (_b = (_a = contents.generationConfig) === null || _a === void 0 ? void 0 : _a.temperature) !== null && _b !== void 0 ? _b : (_c = this.httpClient.getConfig().generationConfig) === null || _c === void 0 ? void 0 : _c.temperature) !== null && _d !== void 0 ? _d : 0.7,
max_completion_tokens: (_h = (_f = (_e = contents.generationConfig) === null || _e === void 0 ? void 0 : _e.maxOutputTokens) !== null && _f !== void 0 ? _f : (_g = this.httpClient.getConfig().generationConfig) === null || _g === void 0 ? void 0 : _g.maxOutputTokens) !== null && _h !== void 0 ? _h : 1024,
top_p: (_m = (_k = (_j = contents.generationConfig) === null || _j === void 0 ? void 0 : _j.topP) !== null && _k !== void 0 ? _k : (_l = this.httpClient.getConfig().generationConfig) === null || _l === void 0 ? void 0 : _l.topP) !== null && _m !== void 0 ? _m : 1.0,
stream: (_o = this.httpClient.getConfig().stream) !== null && _o !== void 0 ? _o : false,
stop: (_s = (_q = (_p = contents.generationConfig) === null || _p === void 0 ? void 0 : _p.stopSequences) !== null && _q !== void 0 ? _q : (_r = this.httpClient.getConfig().generationConfig) === null || _r === void 0 ? void 0 : _r.stopSequences) !== null && _s !== void 0 ? _s : null,
};
}
async generateContent(conent) {
comman_1.CommanMethods.validateContents(conent.contents);
logger_1.logger.info('Static Generation of Content: GROQ');
const response = await this.httpClient.request({
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.httpClient.getConfig().apiKey}`,
},
body: JSON.stringify(this.payloadConstructor(conent)),
}, false, `/openai/v1/chat/completions`);
if (!response.choices || !response.choices[0] || !response.choices[0].message) {
logger_1.logger.error('Groq API did not return a valid choices array. Full response:', response);
throw new Error(`Groq API did not return a valid choices array. Response: ${JSON.stringify(response)}`);
}
return {
resp_id: response.id,
output: response.choices[0].message.content,
created_at: response.created,
model: response.model,
usage: {
input_tokens: response.usage.prompt_tokens,
output_tokens: response.usage.completion_tokens,
total_tokens: response.usage.total_tokens,
},
status: 'success',
};
}
async *generateContentStreamAsync(request) {
logger_1.logger.info('Generating content stream asynchronously for request: %j', request);
try {
comman_1.CommanMethods.validateContents(request.contents);
logger_1.logger.info('Streaming generation (async)');
const response = await this.httpClient.streamRequest({
method: 'POST',
body: JSON.stringify(this.payloadConstructor(request)),
headers: {
Authorization: `Bearer ${this.httpClient.getConfig().apiKey}`,
'Content-Type': 'application/json',
},
}, `/openai/v1/chat/completions`);
yield* this.streamProcessor.createAsyncGenerator(comman_1.CommanMethods.convertReadableToResponse(response));
}
catch (error) {
console.error('Error in generateContentStreamAsync:', error);
throw error;
}
}
get stream() {
return this.streamProcessor;
}
updateConfig(newConfig) {
this.httpClient.updateConfig(newConfig);
}
getConfig() {
return this.httpClient.getConfig();
}
}
exports.GroqClient = GroqClient;
//# sourceMappingURL=groq.adapter.js.map