-
Notifications
You must be signed in to change notification settings - Fork 1
/
groq.js
64 lines (53 loc) · 1.84 KB
/
groq.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
const LLM_API_URL = "https://api.groq.com/openai/v1/chat/completions";
const SYSTEM_MESSAGE = `You run in a process of Question, Thought, Action, Observation.
Use Thought to describe your thoughts about the question you have been asked.
Observation will be the result of running those actions.
Finally at the end, state the Answer.
Here are some sample sessions.
Question: What is capital of france?
Thought: This is about geography, I can recall the answer from my memory.
Action: lookup: capital of France.
Observation: Paris is the capital of France.
Answer: The capital of France is Paris.
Question: Who painted Mona Lisa?
Thought: This is about general knowledge, I can recall the answer from my memory.
Action: lookup: painter of Mona Lisa.
Observation: Mona Lisa was painted by Leonardo da Vinci .
Answer: Leonardo da Vinci painted Mona Lisa.
Let's go!`;
export async function answer(text) {
const MARKER = "Answer:";
const pos = text.lastIndexOf(MARKER);
if (pos < 0) return "?";
const answer = text.substr(pos + MARKER.length).trim();
return answer;
}
export async function think(question) {
const response = await generate(question);
console.log("Response:", response);
return answer(response);
}
export async function generate(prompt) {
if (!prompt) throw new Error("Prompt is required");
const method = "POST";
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.API_KEY}`,
};
const body = JSON.stringify({
model: "llama3-8b-8192",
temperature: 0,
max_tokens: 1024,
top_p: 1,
stream: false,
messages: [
{ role: "system", content: SYSTEM_MESSAGE },
{ role: "user", content: prompt },
],
stop: "",
});
const request = { method, headers, body };
const res = await fetch(LLM_API_URL, request);
const { choices } = await res.json();
return choices[0].message?.content?.trim();
}