-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprometheushackerrank.js
More file actions
237 lines (206 loc) · 9.92 KB
/
prometheushackerrank.js
File metadata and controls
237 lines (206 loc) · 9.92 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import express from "express";
import bodyParser from "body-parser";
import { Groq } from "groq-sdk";
import cors from "cors";
import NodeCache from "node-cache";
import prettier from "prettier";
import dotenv from "dotenv";
// Load environment variables from .env file
dotenv.config();
// Create an Express application
const app = express();
const port = process.env.PORT || 8005;
const groqApiKey = "YOUR_GROQ_API_KEY" || process.env.GROQ_API_KEY;
// Middleware setup
app.use(cors());
app.use(bodyParser.json());
// Initialise GROQ with API key, fallback to default key if not set in environment
const groq = new Groq({
apiKey: groqApiKey,
});
// 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);
}
}
}
// Function to format content
async function formatContent(content) {
let formattedContent = content;
// Improve formatting of titles
formattedContent = formattedContent.replace(
/^(#+)\s*(.+)$/gm,
(match, hashes, title) => {
return `${hashes} **${title.trim()}**`;
},
);
// Ensure bold numbers remain bold
formattedContent = formattedContent.replace(/\*\*(\d+)\*\*/g, "**$1**");
// Convert fractions and equalities to the desired LaTeX format
formattedContent = formattedContent.replace(
/(\d+)\/(\d+)\s*=\s*(0\.\d+)/g,
"$\\frac{$1}{$2} = $3$",
);
// Format code blocks while preserving their content
formattedContent = formattedContent.replace(
/```([\s\S]*?)```/g,
(match, codeContent) => {
// Trim whitespace from the start and end of the code block
return `\`\`\`\n${codeContent.trim()}\n\`\`\``;
},
);
// Ensure LaTeX formulas are properly formatted while preserving content
formattedContent = formattedContent.replace(
/\$(.*?)\$/g,
(match, formula) => {
// Trim whitespace but keep the original content
return `$${formula.trim()}$`;
},
);
// Preserve "n = 5" format in LaTeX
formattedContent = formattedContent.replace(/\$(\d+)\$/g, (match, number) => {
return `$n = ${number}$`;
});
// Format using prettier
try {
formattedContent = await prettier.format(formattedContent, {
parser: "markdown",
});
} catch (error) {
console.error("Error formatting with Prettier:", error);
}
return formattedContent;
}
// Endpoint to handle content formatting requests
app.post("/prometheusHackerRank", async (req, res) => {
const { content } = req.body;
const cacheKey = `formatted:${content}`;
try {
console.log("\nReceived content for formatting:", content);
// Check cache first
const cachedContent = cache.get(cacheKey);
if (cachedContent) {
console.log("\nReturning cached formatted content");
return res.json({ formattedContent: cachedContent });
}
const prompt = `
Your Axiom: You are an expert in formatting content into Markdown. You have the knowledge to convert various content structures into clean, readable Markdown.
Your Task: Format the following content into Markdown. Preserve all mathematical notations and ensure they are properly formatted for LaTeX rendering. Use the format $\\frac{a}{b} = c$ for fractions and their decimal equivalents. Ensure code blocks are properly formatted and maintain their syntax highlighting. Pay special attention to the formatting of arrays, constraints, and input/output examples. Preserve all original information, including STDIN and Function details in code blocks.
Content:
${content}
Your formatted Markdown content:
`;
console.log("\nSending prompt to GROQ API");
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: 2048,
top_p: 0.7,
stream: false,
stop: null,
});
return chatCompletion.choices[0].message.content.trim();
});
console.log("\nReceived response from GROQ API:", response);
const formattedContent = await formatContent(response);
console.log("Formatted content in Markdown:\n", formattedContent);
// Cache the result
cache.set(cacheKey, formattedContent);
res.json({ formattedContent });
} catch (error) {
console.error("\nError during content formatting:", error);
if (error.status === 401) {
res.status(401).json({
error: "Invalid API Key. Please check your GROQ API Key and try again.",
});
} else {
res.status(500).json({
error: "An error occurred during content formatting",
details: error.message,
});
}
}
});
// Route to solve the problem
app.post("/solveProblem", async (req, res) => {
const { problemStatement, userSolution } = req.body;
try {
console.log("\nReceived request to solve problem");
const prompt = `
You are an expert programmer tasked with solving a HackerRank problem.
Given the problem statement and a partial solution, complete the solution to pass all test cases.
Problem Statement:
${problemStatement}
Partial Solution:
${userSolution}
Please provide a complete, working solution that will pass all test cases.
Include all necessary imports, function definitions, and ensure the main function
or solution logic is complete. Do not omit any part of the code.
Do not explain the code, just provide the code.
Do not add any text before or after the code.
Do not use triple backticks (\`\`\`) or any other formatting around the code.
Your response should contain only the complete code solution, nothing else.
Your complete solution:
`;
console.log("\nSending prompt to GROQ API");
const response = await retryWithExponentialBackoff(async () => {
const chatCompletion = await groq.chat.completions.create({
messages: [{ role: "user", content: prompt }],
model: "llama3-70b-8192",
temperature: 0.1,
max_tokens: 4096,
top_p: 0.3,
stream: false,
stop: null,
});
return chatCompletion.choices[0].message.content.trim();
});
console.log("\nReceived response from GROQ API");
res.json({ solution: response });
} catch (error) {
console.error("\nError during problem solving:", error);
res.status(500).json({
error: "An error occurred during problem solving",
details: error.message,
});
}
});
// Start the server and print the banner
app.listen(port, () => {
const banner = `
██████╗ ██████╗ ██████╗ ███╗ ███╗███████╗████████╗██╗ ██╗███████╗██╗ ██╗███████╗
██╔══██╗██╔══██╗██║ ██║████╗ ████║██╔════╝╚══██╔══╝██║ ██║██╔════╝██║ ██║██╔════╝
██████╔╝██████╔╝██║ ██║██╔████╔██║█████╗ ██║ ███████║█████╗ ██║ ██║███████╗
██╔═══╝ ██╔══██╗██║ ██║██║╚██╔╝██║██╔══╝ ██║ ██╔══██║██╔══╝ ██║ ██║╚════██║
██║ ██║ ██║╚██████╔╝██║ ╚═╝ ██║███████╗ ██║ ██║ ██║███████╗╚██████╔╝███████║
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚══════╝
██╗ ██╗ █████╗ ██████╗██╗ ██╗███████╗██████╗ ██████╗ █████╗ ███╗ ██╗██╗ ██╗
██║ ██║██╔══██╗██╔════╝██║ ██╔╝██╔════╝██╔══██╗██╔══██╗██╔══██╗████╗ ██║██║ ██╔╝
███████║███████║██║ █████╔╝ █████╗ ██████╔╝██████╔╝███████║██╔██╗ ██║█████╔╝
██╔══██║██╔══██║██║ ██╔═██╗ ██╔══╝ ██╔══██╗██╔══██╗██╔══██║██║╚██╗██║██╔═██╗
██║ ██║██║ ██║╚██████╗██║ ██╗███████╗██║ ██║██║ ██║██║ ██║██║ ╚████║██║ ██╗
╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝
`;
console.log(banner);
console.log(
`Prometheus HackerRank is delivering the knowledge of HackerRank at http://localhost:${port}`,
);
console.log(
"\nAwaiting the spark of enlightenment...\nPrometheus' torch of knowledge will ignite once extension activation is complete.",
);
});