Download src/lib/ai/groq.js from NeoPy/hugging-c-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/ai/groq.js
- Command line
-
hf download hf://NeoPy/hugging-c-ai/src/lib/ai/groq.js
-
curl -L -o groq.js https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/ai/groq.js
1.13 kB
| import Groq from "groq-sdk"; | |
| export async function callGroq({ | |
| model, messages, temperature = 1.0, maxTokens = 2048, | |
| reasoning = false, stream = false, apiKey, onToken, | |
| }) { | |
| const client = new Groq({ apiKey }); | |
| const modelName = model.replace(/^groq:/, ""); | |
| const chatMessages = messages.map((m) => ({ role: m.role, content: m.content })); | |
| const params = { | |
| model: modelName, | |
| messages: chatMessages, | |
| temperature: Number(temperature), | |
| max_tokens: Number(maxTokens), | |
| stream, | |
| }; | |
| if (stream) { | |
| const completion = await client.chat.completions.create({ ...params, stream: true }); | |
| let fullText = ""; | |
| for await (const chunk of completion) { | |
| const delta = chunk.choices?.[0]?.delta?.content; | |
| if (delta) { fullText += delta; onToken?.(delta); } | |
| } | |
| return { text: fullText, usage: null }; | |
| } | |
| const completion = await client.chat.completions.create(params); | |
| return { | |
| text: completion.choices?.[0]?.message?.content || "", | |
| usage: { | |
| promptTokens: completion.usage?.prompt_tokens, | |
| completionTokens: completion.usage?.completion_tokens, | |
| }, | |
| }; | |
| } | |