-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheusworkana.js
155 lines (131 loc) · 6.82 KB
/
prometheusworkana.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// ```prometheusworkana.js
// Disable deprecation warnings
// process.noDeprecation = true;
// Import the required libraries
import express from "express";
import bodyParser from "body-parser";
import { Groq } from "groq-sdk";
import cors from "cors";
import NodeCache from "node-cache";
// Create an Express application
const app = express();
const port = 8004;
// Middleware setup
app.use(cors());
app.use(bodyParser.json());
// Initialise GROQ with API key
const groq = new Groq({
apiKey: "YOUR_GROQ_API_KEY_HERE",
});
// Cache setup (1 hour TTL)
const cache = new NodeCache({ stdTTL: 3600 });
// Utility function for sleep
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Utility function for retry with exponential backoff
async function retryWithExponentialBackoff(func, maxRetries = 5) {
for (let i = 0; i < maxRetries; i++) {
try {
return await func();
} catch (error) {
if (error.status !== 429 || i === maxRetries - 1) throw error;
const delay = Math.pow(2, i) * 1000 + Math.random() * 1000;
console.log(
`\nThe flames of wisdom are throttled. Rekindling in ${delay}ms...`,
);
await sleep(delay);
}
}
}
// Endpoint to handle answer requests
app.post("/prometheusWorkana", async (req, res) => {
const { question, options, topic } = req.body;
const cacheKey = `${topic}:${question}`;
try {
console.log("\nReceived from Workana...\n");
console.log("Topic:", topic);
console.log("Question:", question);
console.log("Options:", options);
// Check cache first
const cachedAnswer = cache.get(cacheKey);
if (cachedAnswer) {
console.log("\nCached answer:", cachedAnswer);
return res.json({ answer: cachedAnswer });
}
const prompt = `
Your Axiom: All languages, cultures, technologies, studies, concepts, practices, philosophy, design, art, culture, internet, development and programming, finance, politics, and economics are all areas of your expertise.
Your Purpose: As the mythical Prometheus, the master of ${topic}, you shall answer my question.
My Question: ${question}
Your Options:
${options.map((opt, index) => `${index + 1}. ${opt}`).join("\n")}
Your Task: Choose the option that answers my question to reveal your wisdom. Illuminate your choice with only the number, sans any additional description.`;
console.log(
"\nPrometheus stokes the flames of knowledge via GROQ API:",
);
console.log(prompt);
const response = await retryWithExponentialBackoff(async () => {
const chatCompletion = await groq.chat.completions.create({
messages: [{ role: "user", content: prompt }],
model: "llama3-70b-8192",
temperature: 0.2,
max_tokens: 1024,
top_p: 0.5,
// presence_penalty: 0.0,
// frequency_penalty: 0.5,
stream: false,
stop: null,
});
return chatCompletion.choices[0].message.content.trim();
});
console.log(
"\nPrometheus' gift of wisdom bestowed through GROQ API:",
response,
);
// Validate the response
const answerIndex = parseInt(response) - 1;
if (
isNaN(answerIndex) ||
answerIndex < 0 ||
answerIndex >= options.length
) {
throw new Error(
"\nPrometheus' flame flickers: GROQ API's oracle speaks in riddles...",
);
}
const answer = options[answerIndex];
// Cache the result
cache.set(cacheKey, answer);
res.json({ answer });
} catch (error) {
console.error(
"\nPrometheus' torch falters. Knowledge creation thwarted:",
error,
);
res.status(500).json({
error: "\nPrometheus' quest for enlightenment falls short",
});
}
});
// Start the server and print the banner
app.listen(port, () => {
const banner = `
██████╗ ██████╗ ██████╗ ███╗ ███╗███████╗████████╗██╗ ██╗███████╗██╗ ██╗███████╗
██╔══██╗██╔══██╗██╔═══██╗████╗ ████║██╔════╝╚══██╔══╝██║ ██║██╔════╝██║ ██║██╔════╝
██████╔╝██████╔╝██║ ██║██╔████╔██║█████╗ ██║ ███████║█████╗ ██║ ██║███████╗
██╔═══╝ ██╔══██╗██║ ██║██║╚██╔╝██║██╔══╝ ██║ ██╔══██║██╔══╝ ██║ ██║╚════██║
██║ ██║ ██║╚██████╔╝██║ ╚═╝ ██║███████╗ ██║ ██║ ██║███████╗╚██████╔╝███████║
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚══════╝
██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗ █████╗ ███╗ ██╗ █████╗
██║ ██║██╔═══██╗██╔══██╗██║ ██╔╝██╔══██╗████╗ ██║██╔══██╗
██║ █╗ ██║██║ ██║██████╔╝█████╔╝ ███████║██╔██╗ ██║███████║
██║███╗██║██║ ██║██╔══██╗██╔═██╗ ██╔══██║██║╚██╗██║██╔══██║
╚███╔███╔╝╚██████╔╝██║ ██║██║ ██╗██║ ██║██║ ╚████║██║ ██║
╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝
`;
console.log(banner);
console.log(
`Prometheus is delivering the knowledge of Workana at http://localhost:${port}`,
);
console.log(
"\nAwaiting the spark of enlightenment...\nPrometheus' torch of knowledge will ignite once extension activation is complete.",
);
});