-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
65 lines (57 loc) · 1.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(() => {
const localRequire = require("module").createRequire(__filename);
const fetch = localRequire("node-fetch-commonjs");
const Groq = require('groq-sdk');
const groqApiKey = process.env['GROQ_API_KEY'];
globalThis.llm = async function (message, options = {}) {
const model = options.model || 'groq'; // Default to Groq if no model specified
try {
if (model.toLowerCase() !== 'groq') {
// Ollama version (for any non-Groq model)
const response = await fetch("http://127.0.0.1:11434/api/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
model: model.toLowerCase(), // Use the specified model
messages: [
{
role: "user",
content: message,
},
],
stream: false,
}),
});
const data = await response.json();
if (data.message) {
return data.message.content;
} else {
return "No message found in the response.";
}
} else {
// Groq (llama) version
if (!groqApiKey) {
console.warn('Groq API key is not set. Please set the GROQ_API_KEY environment variable.');
return 'Unable to fetch message due to missing Groq API key.';
}
const groq = new Groq({
apiKey: groqApiKey,
});
const chatCompletion = await groq.chat.completions.create({
messages: [{ role: 'user', content: message }],
model: 'llama-3.1-70b-versatile',
});
if (chatCompletion.choices && chatCompletion.choices.length > 0) {
return chatCompletion.choices[0].message.content;
} else {
return "No message found in the response.";
}
}
} catch (error) {
console.error("Error fetching message:", error);
return "An error occurred while fetching the message.";
}
};
})();