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 ;
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
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
}