-
Notifications
You must be signed in to change notification settings - Fork 6
/
index[Deprecated].js
133 lines (114 loc) · 3.42 KB
/
index[Deprecated].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
const https = require("https");
// Example dummy function hard coded to return the same weather
// In production, this could be your backend API or an external API
function get_current_weather(location, unit = "celsius") {
const weather_info = {
location: location,
temperature: "23",
unit: unit,
forecast: ["sunny", "windy"],
};
return JSON.stringify(weather_info);
}
async function runConversation() {
// Step 1: send the conversation and available functions to GPT
const messages = [{ role: "user", content: "weather in melbourne" }];
const functions = [
{
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["location"],
},
},
];
const requestData = JSON.stringify({
model: "gpt-3.5-turbo",
messages: messages,
functions: functions,
function_call: "auto", // auto is default, but we'll be explicit
});
const options = {
hostname: "api.openai.com",
path: "/v1/chat/completions",
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer sk-xxxxxxxxx", // Replace with your OpenAI API key
},
};
const response = await new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(JSON.parse(data));
});
});
req.on("error", (error) => {
reject(error);
});
req.write(requestData);
req.end();
});
const responseMessage = response.choices[0].message;
// Step 2: check if GPT wanted to call a function
if (responseMessage.function_call) {
// Step 3: call the function
const availableFunctions = {
get_current_weather: get_current_weather,
};
const functionName = responseMessage.function_call.name;
const functionToCall = availableFunctions[functionName];
const functionArgs = JSON.parse(responseMessage.function_call.arguments);
const functionResponse = functionToCall(
functionArgs.location,
functionArgs.unit
);
// Step 4: send the info on the function call and function response to GPT
messages.push(responseMessage); // extend conversation with assistant's reply
messages.push({
role: "function",
name: functionName,
content: functionResponse,
}); // extend conversation with function response
const secondRequestData = JSON.stringify({
model: "gpt-3.5-turbo",
messages: messages,
});
const secondResponse = await new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(JSON.parse(data));
});
});
req.on("error", (error) => {
reject(error);
});
req.write(secondRequestData);
req.end();
});
return secondResponse;
}
}
runConversation()
.then((response) => {
console.log(response.choices[0].message.content);
})
.catch((error) => {
console.error(error);
});