mindcraft/src/models/groq.js

94 lines
3.2 KiB
JavaScript
Raw Normal View History

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
// NOTICE: I've moved to an actual custom coding style. Sorry if this seems quite a bit more spaced out than the rest of the classes, as this style places HEAVY emphasis on spacing and
// readability.
// I'm just maintaining my old legacy code.
// Cheers, Kalinite/Copper/FateUnix29/...
// 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;
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
2024-08-25 13:16:32 -07:00
}
async sendRequest(turns, systemMessage, stop_seq=null) {
2025-02-17 15:49:52 -08:00
let messages = [{"role": "system", "content": systemMessage}].concat(turns); // The standard for GroqCloud is just appending to a messages array starting with the system prompt, but
// this is perfectly acceptable too, and I recommend it.
// I still feel as though I should note it for any future revisions of MindCraft, though.
// These variables look odd, but they're for the future. Please keep them intact.
let raw_res = null;
2024-08-25 13:16:32 -07:00
let res = null;
2025-02-17 15:49:52 -08:00
let tool_calls = null;
2024-08-25 13:16:32 -07:00
try {
2025-02-17 15:49:52 -08:00
2024-08-25 13:16:32 -07:00
console.log("Awaiting Groq response...");
2025-02-17 15:49:52 -08:00
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 = 8000; // Set it lower. This is a common theme.
2025-02-04 13:02:57 -06:00
}
2025-02-17 15:49:52 -08:00
2024-08-25 13:16:32 -07:00
let completion = await this.groq.chat.completions.create({
"messages": messages,
2025-02-17 15:49:52 -08:00
"model": this.model_name || "llama-3.3-70b-versatile",
"stream": false,
"tools": null,
"tool_choice": "auto",
2025-02-04 13:02:57 -06:00
"stop": stop_seq,
...(this.params || {})
2024-08-25 13:16:32 -07:00
});
2025-02-17 15:49:52 -08:00
raw_res = completion.choices[0].message;
res = raw_res.content;
2024-08-25 13:16:32 -07:00
}
2025-02-17 15:49:52 -08:00
2024-08-25 13:16:32 -07:00
catch(err) {
2025-02-17 15:49:52 -08:00
2024-08-25 13:16:32 -07:00
console.log(err);
res = "My brain just kinda stopped working. Try again.";
2025-02-17 15:49:52 -08:00
2024-08-25 13:16:32 -07:00
}
2025-02-17 15:49:52 -08:00
2024-08-25 13:16:32 -07:00
return res;
}
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
}
2024-06-18 20:15:17 -07:00
}