2024-06-18 19:48:16 -07:00
|
|
|
import Groq from 'groq-sdk'
|
2024-06-18 17:56:49 -07:00
|
|
|
import { getKey } from '../utils/keys.js';
|
2024-06-18 17:56:04 -07:00
|
|
|
|
2024-08-25 13:16:32 -07:00
|
|
|
|
2024-08-31 15:29:34 -07:00
|
|
|
// Umbrella class for Mixtral, LLama, Gemma...
|
|
|
|
export class GroqCloudAPI {
|
2025-02-04 13:02:57 -06:00
|
|
|
constructor(model_name, url, params) {
|
2024-08-25 13:16:32 -07:00
|
|
|
this.model_name = model_name;
|
|
|
|
this.url = url;
|
2025-02-05 15:14:33 -06:00
|
|
|
this.params = params || {};
|
2024-08-31 15:29:34 -07:00
|
|
|
// ReplicateAPI theft :3
|
|
|
|
if (this.url) {
|
2025-02-04 13:02:57 -06:00
|
|
|
|
2024-08-31 15:29:34 -07:00
|
|
|
console.warn("Groq Cloud has no implementation for custom URLs. Ignoring provided URL.");
|
2024-08-25 13:16:32 -07:00
|
|
|
}
|
2024-08-31 15:29:34 -07:00
|
|
|
this.groq = new Groq({ apiKey: getKey('GROQCLOUD_API_KEY') });
|
2024-08-25 13:16:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async sendRequest(turns, systemMessage, stop_seq=null) {
|
|
|
|
let messages = [{"role": "system", "content": systemMessage}].concat(turns);
|
|
|
|
let res = null;
|
|
|
|
try {
|
|
|
|
console.log("Awaiting Groq response...");
|
2025-02-04 13:02:57 -06:00
|
|
|
if (!this.params.max_tokens) {
|
|
|
|
this.params.max_tokens = 16384;
|
|
|
|
}
|
2024-08-25 13:16:32 -07:00
|
|
|
let completion = await this.groq.chat.completions.create({
|
|
|
|
"messages": messages,
|
2024-08-31 15:29:34 -07:00
|
|
|
"model": this.model_name || "mixtral-8x7b-32768",
|
2024-08-25 13:16:32 -07:00
|
|
|
"stream": true,
|
2025-02-04 13:02:57 -06:00
|
|
|
"stop": stop_seq,
|
|
|
|
...(this.params || {})
|
2024-08-25 13:16:32 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
let temp_res = "";
|
|
|
|
for await (const chunk of completion) {
|
|
|
|
temp_res += chunk.choices[0]?.delta?.content || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
res = temp_res;
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
res = "My brain just kinda stopped working. Try again.";
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-06-18 19:10:56 -07:00
|
|
|
async embed(text) {
|
2025-02-17 16:47:45 -06:00
|
|
|
throw new Error('Embeddings are not supported by Groq.');
|
2024-06-18 19:10:56 -07:00
|
|
|
}
|
2024-06-18 20:15:17 -07:00
|
|
|
}
|