-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_groq.js
139 lines (119 loc) · 4.48 KB
/
server_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
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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const { spawn } = require('child_process');
const Groq = require('groq-sdk');
require('dotenv').config();
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
const promptPath = 'files/prompt.txt';
const descriptionPath = 'files/descPrompt.txt';
const textData = (filePath) => {
try {
return fs.readFileSync(filePath, 'utf8');
} catch (err) {
console.error(`Error reading the file: ${filePath}`, err);
return 'Error reading data. Please return a 404 error output.';
}
};
const promptData = textData(promptPath);
const descriptionData = textData(descriptionPath);
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
const generateDescription = async (htmlData) => {
console.log('Generating website description... This may take up to 2 minutes.');
const chatCompletion = await groq.chat.completions.create({
messages: [
{ role: "system", content: descriptionData },
{ role: "user", content: htmlData }
],
"model": "mixtral-8x7b-32768",
"temperature": 0,
"max_tokens": 32768,
"top_p": 1,
"stream": false,
"stop": null
});
const websiteDescription = chatCompletion.choices[0].message.content;
console.log('Website Description Generated:', websiteDescription);
return websiteDescription;
};
const generatePlaywrightScript = async (websiteDescription, htmlData, instructions, websiteLink) => {
console.log('Generating Playwright script...');
const chatCompletion = await groq.chat.completions.create({
messages: [
{ role: "system", content: promptData },
{
role: "user", content: `
Instructions: ${instructions}
Description: ${websiteDescription}
Website Link: ${websiteLink}
HTML:
${htmlData}
Javascript: Not provided
` }
],
model: "llama3-70b-8192",
temperature: 0.1,
max_tokens: 8192,
top_p: 1,
stream: false,
stop: null
});
const script = chatCompletion.choices[0].message.content.replace(/```javascript|```/g, '');
console.log('Playwright Script Generated:', script);
return script;
};
const executeScript = (script) => {
const lamScript = `tempScript.js`;
fs.writeFileSync(lamScript, script);
console.log('Executing script in 5 seconds... Press CTRL + C to stop execution.');
setTimeout(() => {
console.log('Executing script...');
const child = spawn('node', [lamScript]);
child.stdout.on('data', (data) => console.log(`Script output: \n${data.toString()}`));
child.stderr.on('data', (data) => console.error(`Script error: ${data.toString()}`));
child.on('exit', () => fs.unlinkSync(lamScript));
child.on('error', (err) => console.error(`Failed to run script: ${err.message}`));
console.log('Playwright script finished!');
}, 5000);
};
const getWebsiteHTML = async (url) => {
try {
const response = await axios.get(url);
const htmlData = response.data;
await fs.promises.writeFile('files/html.txt', htmlData);
return htmlData;
} catch (error) {
console.error('Error fetching website HTML');
return `Error fetching website HTML`;
}
};
const runLam = async (websiteLink, instructions) => {
try {
const htmlData = await getWebsiteHTML(websiteLink);
const websiteDescription = await generateDescription(htmlData);
const playwrightScript = await generatePlaywrightScript(websiteDescription, htmlData, instructions, websiteLink);
executeScript(playwrightScript);
} catch (error) {
console.error('Error running LAM:', error);
}
};
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.post('/run-script', async (req, res) => {
const { websiteUrl, instructions } = req.body;
try {
await runLam(websiteUrl, instructions);
res.json({ output: 'Done! Executing script...' });
} catch (error) {
res.status(500).json({ output: `Error executing script: ${error.message}` });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});