mindcraft/src/models/groq.js

51 lines
1.7 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
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 {
constructor(model_name, url, max_tokens=16384) {
2024-08-25 13:16:32 -07:00
this.model_name = model_name;
this.url = url;
2024-08-31 15:29:34 -07:00
this.max_tokens = max_tokens;
// ReplicateAPI theft :3
if (this.url) {
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...");
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
"temperature": 0.2,
2024-08-31 15:29:34 -07:00
"max_tokens": this.max_tokens, // maximum token limit, differs from model to model
2024-08-25 13:16:32 -07:00
"top_p": 1,
"stream": true,
"stop": stop_seq // "***"
});
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) {
console.log("There is no support for embeddings in Groq support. However, the following text was provided: " + text);
2024-06-18 19:10:56 -07:00
}
2024-06-18 20:15:17 -07:00
}