-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathsolution.js
148 lines (134 loc) · 4.21 KB
/
solution.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
148
import { OpenAI } from 'openai';
// 1: Define the functions
function calculateDistance(lat1, long1, lat2, long2) {
// Perform the task of calculating the distance between two points
// Return the distance between the points
return Math.sqrt((lat2 - lat1) ** 2 + (long2 - long1) ** 2);
}
function getGpsPosition(lat, long) {
// Perform the task of getting the GPS position of the current location
// Return the GPS position
return { lat: 7.5, long: 134.5 };
}
function getWeatherForecast(lat, long) {
// Perform the task of getting the weather forecast for a given location
// Return the weather forecast
return "Sunny";
}
// 2: create metadata for the functions
const calculateDistanceJson = {
name: "calculate-distance",
description: "Calculates th distance between two points",
parameters: {
type: "object",
properties: {
lat1: {
type: "number",
description: "The latitude of the first point",
},
long1: {
type: "number",
description: "The longitude of the first point",
},
lat2: {
type: "number",
description: "The latitude of the second point",
},
long2: {
type: "number",
description: "The longitude of the second point",
},
},
required: ["lat1", "long1", "lat2", "long2"],
},
output: { type: "number" }
};
const getGpsPositionJson = {
name: "get-gps-position",
description: "Gets the GPS position of the current location",
parameters: {
type: "object",
properties: {
lat: {
type: "number",
description: "The latitude of the first point",
},
long: {
type: "number",
description: "The longitude of the first point",
},
},
required: ["lat", "long"],
},
output: { type: "object", properties: { lat: "number", long: "number" } }
}
const getWeatherForecastJson = {
name: "get-weather-forecast",
description: "Gets the weather forecast for a given location",
parameters: {
type: "object",
properties: {
lat: {
type: "number",
description: "The latitude of the location",
},
long: {
type: "number",
description: "The longitude of the location",
},
},
required: ["lat", "long"],
},
output: { type: "string" }
}
// 3: create a tools object with the functions
const tools = {
[calculateDistanceJson.name]: calculateDistance,
[getGpsPositionJson.name]: getGpsPosition,
[getWeatherForecastJson.name]: getWeatherForecast
};
// 4: create an OpenAI instance with the tools
const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com",
apiKey: process.env.GITHUB_TOKEN,
});
// 5: create messages to test the functions
// enable one of these messages at a time to test the functions
const messages = [
{
role: "user",
content: `We need to know where to land, here's the coordinates: 7.5, 134.5. `,
},
// {
// role: "user",
// content: `What is the distance between the points 7.5, 134.5 and 8.5, 135.5?`,
// },
// {
// role: "user",
// content: `What is the weather forecast for the location 7.5, 134.5?`,
// },
];
// 6: make a chat completion
async function main(){
const result = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: messages,
functions: [calculateDistanceJson, getGpsPositionJson, getWeatherForecastJson]
});
// 7: interpret the result
for (const choice of result.choices) {
// console.log("Result", choice.message);
let functionCall = choice.message?.function_call;
let functionName = functionCall?.name;
let args = JSON.parse(functionCall?.arguments);
// console.log("Wants to call: ", choice.message?.function_call);
// console.log("With args: ", args);
if (functionName && functionName in tools) {
console.log(`Calling [${functionName}]`);
const toolFunction = tools[functionName];
const toolResponse = toolFunction(...Object.values(args)); // Extract values from args and spread them
console.log("Result from [tool] calling: ", toolResponse);
}
}
}
main();