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
|
|
|
|
2025-02-17 15:49:52 -08:00
|
|
|
// THIS API IS NOT TO BE CONFUSED WITH GROK!
|
|
|
|
// Go to grok.js for that. :)
|
2024-08-25 13:16:32 -07:00
|
|
|
|
2025-02-17 15:49:52 -08:00
|
|
|
// Umbrella class for everything under the sun... That GroqCloud provides, that is.
|
2024-08-31 15:29:34 -07:00
|
|
|
export class GroqCloudAPI {
|
2025-02-17 15:49:52 -08:00
|
|
|
|
2025-02-04 13:02:57 -06:00
|
|
|
constructor(model_name, url, params) {
|
2025-02-17 15:49:52 -08:00
|
|
|
|
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 || {};
|
2025-02-04 13:02:57 -06:00
|
|
|
|
2025-02-17 15:49:52 -08:00
|
|
|
// Remove any mention of "tools" from params:
|
|
|
|
if (this.params.tools)
|
|
|
|
delete this.params.tools;
|
|
|
|
// This is just a bit of future-proofing in case we drag Mindcraft in that direction.
|
|
|
|
|
|
|
|
// I'm going to do a sneaky ReplicateAPI theft for a lot of this, aren't I?
|
|
|
|
if (this.url)
|
2024-08-31 15:29:34 -07:00
|
|
|
console.warn("Groq Cloud has no implementation for custom URLs. Ignoring provided URL.");
|
2025-02-17 15:49:52 -08:00
|
|
|
|
2024-08-31 15:29:34 -07:00
|
|
|
this.groq = new Groq({ apiKey: getKey('GROQCLOUD_API_KEY') });
|
2025-02-17 15:49:52 -08:00
|
|
|
|
|
|
|
|
2025-02-08 22:41:07 -08:00
|
|
|
}
|
2025-02-17 15:49:52 -08:00
|
|
|
|
2025-03-15 17:25:11 -05:00
|
|
|
async sendRequest(turns, systemMessage, stop_seq = null) {
|
|
|
|
// Construct messages array
|
|
|
|
let messages = [{"role": "system", "content": systemMessage}].concat(turns);
|
2024-08-25 13:16:32 -07:00
|
|
|
|
2025-03-15 17:25:11 -05:00
|
|
|
let res = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
console.log("Awaiting Groq response...");
|
|
|
|
|
|
|
|
// Handle deprecated max_tokens parameter
|
|
|
|
if (this.params.max_tokens) {
|
|
|
|
console.warn("GROQCLOUD WARNING: A profile is using `max_tokens`. This is deprecated. Please move to `max_completion_tokens`.");
|
|
|
|
this.params.max_completion_tokens = this.params.max_tokens;
|
|
|
|
delete this.params.max_tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.params.max_completion_tokens) {
|
|
|
|
this.params.max_completion_tokens = 4000;
|
|
|
|
}
|
|
|
|
|
|
|
|
let completion = await this.groq.chat.completions.create({
|
|
|
|
"messages": messages,
|
|
|
|
"model": this.model_name || "llama-3.3-70b-versatile",
|
|
|
|
"stream": false,
|
|
|
|
"stop": stop_seq,
|
|
|
|
...(this.params || {})
|
|
|
|
});
|
|
|
|
|
2025-05-01 17:57:47 +03:00
|
|
|
res = completion.choices[0].message.content;
|
2025-02-17 15:49:52 -08:00
|
|
|
|
2025-03-15 17:25:11 -05:00
|
|
|
res = res.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
|
|
|
|
}
|
2024-08-25 13:16:32 -07:00
|
|
|
catch(err) {
|
2025-02-10 02:03:25 +09:00
|
|
|
if (err.message.includes("content must be a string")) {
|
|
|
|
res = "Vision is only supported by certain models.";
|
|
|
|
} else {
|
|
|
|
console.log(this.model_name);
|
|
|
|
res = "My brain disconnected, try again.";
|
|
|
|
}
|
2024-08-25 13:16:32 -07:00
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2025-01-27 17:29:14 +09:00
|
|
|
async sendVisionRequest(messages, systemMessage, imageBuffer) {
|
2025-02-10 02:03:25 +09:00
|
|
|
const imageMessages = messages.filter(message => message.role !== 'system');
|
2025-01-27 17:29:14 +09:00
|
|
|
imageMessages.push({
|
|
|
|
role: "user",
|
|
|
|
content: [
|
|
|
|
{ type: "text", text: systemMessage },
|
|
|
|
{
|
|
|
|
type: "image_url",
|
|
|
|
image_url: {
|
|
|
|
url: `data:image/jpeg;base64,${imageBuffer.toString('base64')}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.sendRequest(imageMessages);
|
|
|
|
}
|
|
|
|
|
2025-02-17 15:49:52 -08:00
|
|
|
async embed(_) {
|
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
|
|
|
}
|
2025-02-19 17:24:18 -08:00
|
|
|
}
|