-
Notifications
You must be signed in to change notification settings - Fork 0
/
groq_filter_text_strings.cjs
56 lines (51 loc) · 1.98 KB
/
groq_filter_text_strings.cjs
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
require('dotenv').config();
const Groq = require('groq-sdk');
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
async function getChatCompletion(message) {
let attempts = 0;
const maxAttempts = 3; // Set a maximum number of attempts to avoid infinite loops
while (attempts < maxAttempts) {
try {
const chatCompletion = await groq.chat.completions.create({
"messages": [
{
"role": "system",
"content": `You are a helpful assistant. You are being given a pair of text and code snippets.
Based on the given input,
Your job is to determine a starting point in the text string that is talking about the code snippet.
Respond with a starting position of the relevant text inside the given string.
Important: Only return a single number in response`
},
{
"role": "user",
"content": message
}
],
"model": "llama3-8b-8192",
"temperature": 1,
"max_tokens": 1024,
"top_p": 1,
"stream": false,
"stop": null
});
const result = chatCompletion.choices[0].message.content.toLowerCase();
if (result === "text" || result === "code") {
process.stdout.write("OK "); // Print 'OK' followed by a space, without a newline
return result;
} else {
attempts++;
}
} catch (error) {
if (error.response && error.response.status === 429) {
const retryAfter = error.response.headers['retry-after'];
process.stdout.write("ER "); // Print 'ER' followed by a space, without a newline
await new Promise(resolve => setTimeout(resolve, retryAfter * 60000)); // Wait before retrying
attempts++;
} else {
process.stdout.write("ER "); // Print 'ER' for other errors as well, without a newline
}
}
}
return "Error: Failed to get valid response after several attempts.";
}
module.exports = { getChatCompletion };