-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (96 loc) · 3.69 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
import OpenAI from "openai";
import readlineSync from "readline-sync";
import dotenv from "dotenv";
dotenv.config();
const OPEN_API_KEY='apna daal le bhai'
const client = new OpenAI({
apiKey:OPEN_API_KEY,
})
//tools
function getWeatherDetails(city=''){
if(city.toLowerCase()==='patiala')return '10*C';
if(city.toLowerCase()==='delhi')return '15*C';
if(city.toLowerCase()==='mumbai')return '20*C';
if(city.toLowerCase()==='bangalore')return '25*C';
}
const tools = {
"getWeatherDetails":getWeatherDetails,
}
{/*const user="Hey, What is the weather of Patiala"
client.chat.completions.create({
model:"gpt-4",
messages:[{role:'user',content:user}],
}).then(e=>{
console.log(e.choices[0].message.content)
})*/}
const SYSTEM_PROMPT=`You are an AI Assistany with START, PLAN, ACTION, Observation and Output State.
Wait for the user prompt and first PLAN using available tools.
After planning, Take the action with appropriate tools and wait for Observation based on the action.
Once you get the observations, Return the AI response based on START prompt and observations.
Strictly follow the JSON output format as in examples
Available Tools:
- function getWeatherDetails(city:string):string
getWeatherDetails is a function that accepts city name as string and returns the weather details
Example:
START
{"type":"user", "user":"What is the sum of weather of patiala and delhi?" }
{"type":"plan", "plan":"I will call the getWeatherDetails for patiala." }
{"type":"action", "function":"getWeatherDetails,"input":"patiala" }
{"type":"observation", "observation":"10*C" }
{"type":"plan", "plan":"I will call getWeatherDetails for Delhi" }
{"type":"action", "function":"getWeatherDetails,"input":"delhi" }
{"type":"observation", "observation":"15*C" }
{"type":"output", "output":"The sum of weather of patiala and delhi is 25*C" }
`
{/*const user="Hey what is the weather of Patiala"
async function chat(){
const result= await client.chat.completions.create({
model:'gpt-4o',
messages:[
{"role":"system",content:SYSTEM_PROMPT},
{
role:'developer',
content:
'{"type":"plan", "plan":"I will call the getWeatherDetails for patiala." }'
},
{
role:'developer',
content:
'{"type":"observation", "observation":"10*C" }'
},
{role:'user', content:user}],
});
console.log(result.choices[0].message.content);
}
chat();*/}
const messages = [{role:'system',content:SYSTEM_PROMPT}];
while(true){
const query = readlineSync.question(">>: ");
const q = {
type:'user',
user:query,
};
messages.push({"role":"user",content:JSON.stringify(q)});
while(true){
const chat = await client.chat.completions.create({
model:'gpt-4o',
messages:messages,
response_format:{type:'json_object'}
});
const results = chat.choices[0].message.content;
messages.push({role:'assistant',content:results})
console.log('--------------------AI SAYS-------------------');
console.log(results);
console.log('--------------------AI ENDS-------------------');
const call = JSON.parse(results);
if(call.type=='output'){
console.log('Bot says',call.output);
break;
}else if(call.type=='action'){
const fn = tools[call.function];
const observation =fn(call.input);
const obs={"type":"observation","observation":observation};
messages.push({role:'developer',content:JSON.stringify(obs)});
}
}
}