-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
120 lines (86 loc) · 3.58 KB
/
agent.js
File metadata and controls
120 lines (86 loc) · 3.58 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
const { OpenAI } = require('openai');
const {exec} = require('child_process');
const API_KEY=""
const SYSTEM_PROMPT = `You are a helpful aai agent who is designed to resolve user query. You work on START, THINK, ACTION, OBSERVE and OUTPUT mode.
START mode: You are in START mode. You will receive a query from the user.
Then you THINK how to resolve the query atleast 3-4 times and make sure that if there is a need to call a tool, you can call an ACTION event with tool and input parameters.
After that you OBSERVE the result of the action and OUTPUT the result to the user.
Based on the OBSERVE from previous step, you either output or repeat the THINK step.
Rules:
-Always wait for next step
-Always output a single step and wait for the next step.
-Output must be strctly JSON
-Only call tool action from Avaliable tools
-Strictly follow the format of output
-No other tools can be called
Available tools:
- executeCommand(command): string Executes a given linux command on user's device and return STDOUT and STDERR.
Example:
START: What is the weather in London?
THINK: The user is asking for the weather in London.
THINK: I will use the getWeather tool to get the weather in London.
ACTION: call tool getWeather(London)
OBSERVE: 29 degrees C
OUTPUT: The weather in London is 29 degrees C, which is quite hot.
Output example:
{role: "user", content: "What is the weather in London?"}
{"step": "think": "content": "The user is asking for the weather in London."}
{"step": "think": "content": "I will use the getWeather tool to get the weather in London."}
{"step": "action": "tool": "getWeather", "input": "London"}
{"step": "observe": "content": "29 degrees C"}
{"step": "output": "content": "The weather in London is 29 degrees C, which is quite hot."}
Output format:
{"step": "string", "tool": "string", "input": "string", "content": "string"}
`;
const client = new OpenAI({ apiKey: API_KEY,
baseURL: 'https://api.perplexity.ai'
});
const TOOLS_MAP = {
executeCommand: executeCommand
}
function executeCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(error);
} else {
resolve(`stdout: ${stdout}, stderr: ${stderr}`);
}
});
});
}
async function init(){
const messages = [
{role: "system", content: SYSTEM_PROMPT}
];
const userQuery = 'Create a folder todo app and create a todo app with html css and JS'
messages.push({role: "user", content: userQuery});
while(true){
const response = await client.chat.completions.create({
model: "sonar",
messages: messages,
})
messages.push({role: "assistant", content: response.choices[0].message.content});
const content = response.choices[0].message.content;
const parsedResponse = JSON.parse(content);
if (parsedResponse.step && parsedResponse.step === 'think'){
console.log(`thinking: ${parsedResponse.content}`);
messages.push({role:"user", content:"continue"});
}
if (parsedResponse.step && parsedResponse.step === 'output'){
console.log(`output: ${parsedResponse.content}`);
messages.push({role:"user", content:"continue"});
break; // Exit the loop if output is provided
}
if (parsedResponse.step && parsedResponse.step === 'action'){
const tool = parsedResponse.tool;
const input = parsedResponse.input;
const value = await TOOLS_MAP[tool](input);
console.log(`action: ${tool}(${input})`);
messages.push({role: "tool", content: JSON.stringify({step: 'observe', content: value})});
messages.push({role:"user", content:"continue"});
continue
}
}
}
init();