-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_chat.mjs
35 lines (32 loc) · 1.39 KB
/
openai_chat.mjs
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
import {OpenAI} from 'openai';
import dotenv from 'dotenv/config'
import { resolve } from 'path';
import { rejects } from 'assert';
const apiKey = process.env.OPENAI_API_KEY;
function generateChatResponse(apiKey, error, file) {
let prompt = '';
if(file){prompt = "This is the file:" + file + "this is the error:" + error;}
else{prompt = "This is the error:" + error;}
return new Promise((resolve, reject) => {
const openai = new OpenAI({
apiKey: apiKey,
});
openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'system', content: "You will recieve error outputs, your goal is to help me fix this error and understand how it happened. Your name is Hal - like the AI from 2001 space odyssey. You will respond quick and concise and in plain text - no formatting. Attached below (optionally might include the whole code) is the error output. If you were not given the full code file and feel that it might be the missing key to debugging the code, mention that."},
{ role: 'user', content: prompt }
],
})
.then(response => resolve(response))
.catch(error => reject(error));
});
}
export async function handleDebug(input, file) {
try {
const response = await generateChatResponse(apiKey, String(input), String(file));
console.log(response.choices[0].message.content);
} catch (error) {
console.error('Error:', error);
}
}