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.
86 lines • 4.94 kB
JavaScript
import { GroqStreamProcessor } from '../strategies/stream/groq.stream';
import { RetryHandler } from '../strategies/retry/expo';
import { HttpClient } from './google.adapter';
import { CommanMethods } from './comman';
import { logger } from '../utils/logger';
export class GroqClient {
constructor(config, baseUrl, retryHandler) {
var _a, _b, _c, _d;
this.retryHandler = new 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 HttpClient(config, 'https://api.groq.com/', this.retryHandler);
this.streamProcessor = new GroqStreamProcessor();
}
payloadConstructor(contents) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
return {
messages: 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) {
CommanMethods.validateContents(conent.contents);
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.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.info('Generating content stream asynchronously for request: %j', request);
try {
CommanMethods.validateContents(request.contents);
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(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();
}
}
//# sourceMappingURL=groq.adapter.js.map