forked from Nun-Thee-Knee/ERBify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion.js
48 lines (40 loc) · 1.27 KB
/
conversion.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
const vscode = require('vscode');
const Groq = require('groq-sdk');
const groq = new Groq({apiKey: "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"});
async function convertHamlToErb(haml) {
const chatCompletion = await groq.chat.completions.create({
"messages": [
{
"role": "user",
"content": `Convert the following haml to erb\n ${haml}`
}
],
"model": "llama3-8b-8192",
"temperature": 0,
"max_tokens": 8192,
"top_p": 1,
"stream": true,
"stop": null
});
let erb = "";
for await (const chunk of chatCompletion) {
erb += chunk.choices[0]?.delta?.content || '';
}
const firstTagIndex = erb.indexOf('<');
const lastTagIndex = erb.lastIndexOf('>');
if (firstTagIndex === -1 || lastTagIndex === -1 || firstTagIndex >= lastTagIndex) {
return "";
}
const extractedContent = erb.slice(firstTagIndex, lastTagIndex + 1);
return extractedContent;
}
async function getData(data) {
try {
const erb = await convertHamlToErb(data);
return erb;
} catch (error) {
console.error('Error invoking model:', error);
vscode.window.showErrorMessage('Failed to convert HAML to ERB. Please check your API key and network connection.');
}
}
module.exports = { getData };