-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (134 loc) · 5.39 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const readline = require("readline");
const { getCompletion } = require("./gpt");
function isValidDate(dateObject) {
const compare =
new Date(dateObject).toString() !== "Invalid Date" &&
new Date(dateObject) >= new date();
return compare;
}
const messages = [
{
role: "system",
content: `Ask the User following step-by-step instructions and respond to user inputs.
Step 1 - Ask User for the pickup location first when user ask to book a office ride.When ask for pickup send this map location https://www.google.co.in/maps/@28.6020871,77.3690171,14.58z?entry=ttu link to user to choose location on map
Step 2 - Once user gives the pickup location then ask for office location.
Step 3 - Once Step 1 to 2 inputs are given ask for car type? when ask for car type share list of car type ["Standard Car", "SUV", "Luxury Car"]
Step 4 - Once step 3 inputs are given call rides_available function.
Step 5 - Once the step 4 completed ask for selected ride ID to user.
Step 6 - When pickup_location, office_location, car_type, selected_ride_id are provided by user or found in conversation history and the user has clearly stated they confirm call the function book_travel
don't make assumption or set any default value.
Call book_ride only when you have [pickup_location, office_location, car_type, selected_ride_id] in conversation history and the user has clearly stated they confirm,
otherwise you must continue asking about the missing values.
`,
},
];
// [
// {
// role: "system",
// content: `It's mandatory to only call
// book_travel function when user input all parameters which are required.
// Dont make assumption or set any default value.
// Ask for [destination, departure, number_people, travel_mode,when].
// Call book_travel only when you have [destination, departure, number_people, travel_mode,when] in conversation history and the user has clearly stated they confirm,
// otherwise you must continue interviewing.`,
// },
// ];
//{role: "system", content: "Your name is Abhijeet's Genie"}
var dyn_functions = [];
dyn_functions["send_email"] = function (args) {
return "send email";
};
dyn_functions["book_travel"] = function (args) {
console.log("args ->", args);
console.log(">>>>> ", isValidDate(args.when));
// if(args.number_people == '1')
// return { "error" : "invalid number_people" }
if (!isValidDate(args.when) || !args.when) {
return { error: "Invalid travel date, please provide again" };
}
return { data: "Ticket Booked. Details are :" + JSON.stringify(args) };
};
dyn_functions["book_ride"] = function (args) {
console.log("args ->", args);
return { data: "Office Ride Booked. Details are :" + JSON.stringify(args) };
};
dyn_functions["rides_available"] = function (args) {
const rides = [
{
ID: 1,
name: "xyz",
distance: "2 Km",
rating: "4.1",
},
{
ID: 2,
name: "abc",
distance: "4 Km",
rating: "4.2",
},
];
return {
data:
"You may accept one of the offers quoted below or you may counter an offer as well. :" +
JSON.stringify(rides),
};
};
const userInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
userInterface.prompt();
userInterface.on("line", async (input) => {
try {
messages.push({ role: "user", content: input });
let res = await getCompletion(messages);
if (res.data.choices[0].message.function_call) {
const fnName = res.data.choices[0].message.function_call.name;
const args = JSON.parse(
res.data.choices[0].message.function_call.arguments
);
//console.log(`Function call: ${fnName}, Arguments: ${args}`);
const fn = dyn_functions[fnName];
const result = await fn(args);
console.log(
`Calling Function ${fnName} Result: ` + JSON.stringify(result)
);
messages.push(
{
role: "assistant",
content: null,
function_call: {
name: fnName,
arguments: JSON.stringify(args),
},
},
{
role: "function",
name: fnName,
content: JSON.stringify(result),
}
);
res = await getCompletion(messages);
console.log(res.data.choices[0].message);
} else {
console.log(res.data.choices[0].message);
// messages.push({
// role: "system",
// content: `It's mandatory to only call
// book_travel function when user input all parameters which are required.
// Dont make assumption or set any default value.
// Ask for [destination, departure, number_people, travel_mode,when].
// Call book_travel only when you have [destination, departure, number_people, travel_mode,when] in conversation history and the user has clearly stated they confirm,
// check if date is not the previous date
// otherwise you must continue interviewing.`,
// });
messages.push({
role: "assistant",
content: res.data.choices[0].message.content,
});
}
userInterface.prompt();
} catch (e) {
console.log(e.response);
}
});