-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (118 loc) · 3.61 KB
/
index.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
import { Configuration, OpenAIApi } from "openai";
import { exec } from 'child_process';
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY // <-- YOUR KEY
}));
async function runCommand(cmd) {
console.log("Running command:", cmd);
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
resolve(error.toString());
}
resolve(stdout);
});
});
}
async function runPythonCode(code) {
console.log("Running Python code:", code);
return new Promise((resolve, reject) => {
exec(`python -c "${code}"`, (error, stdout, stderr) => {
if (error) {
resolve(error.toString());
}
resolve(stdout || "Executed Successfully");
});
});
}
async function chatWithAI() {
let gptResponse = null;
let history = [
{
role: "user",
content: "run some python code that finds the current date and time. if it doesnt work stop."
}
];
while (true) {
if (gptResponse && gptResponse.data.choices && gptResponse.data.choices.length > 0) {
const functionCall = gptResponse.data.choices[0].message.function_call;
if (functionCall && functionCall.name === "run_cli_command") {
const command = JSON.parse(functionCall.arguments).command;
const output = await runCommand(command);
history.push({
role: "function",
name: "run_cli_command",
content: output ?? "Executed command successfully"
});
} else if (functionCall && functionCall.name === "run_python_code") {
const code = JSON.parse(functionCall.arguments).code;
const output = await runPythonCode(code);
history.push({
role: "function",
name: "run_python_code",
content: output
});
} else if (functionCall && functionCall.name === "end") {
console.log("Task completed.");
break;
}
}
try {
gptResponse = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages: history,
functions: [
{
name: "run_cli_command",
description: "Run a cli command",
parameters: {
type: "object",
properties: {
command: {
type: "string",
description: "The command to run"
},
},
required: ["command"]
}
},
{
name: "run_python_code",
description: "Run Python code",
parameters: {
type: "object",
properties: {
code: {
type: "string",
description: "The Python code to run. Must be just code, no additional formatting."
},
},
required: ["code"]
}
},
{
name: "end",
description: "Mark the task as complete and end the program.",
parameters: {
type: "object",
properties: {}
},
required: []
}
],
function_call: history[history.length - 1].role == "function" ? "none" : "auto",
});
if (gptResponse.data.choices[0].message.content) {
history.push({
role: gptResponse.data.choices[0].message.role,
content: gptResponse.data.choices[0].message.content
});
console.log(gptResponse.data.choices[0].message.content);
}
} catch (error) {
console.log(error.message);
throw new Error("error");
}
}
}
chatWithAI();