-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt.ts
100 lines (95 loc) · 3.18 KB
/
chatgpt.ts
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
import OpenAI from 'openai';
const openai = new OpenAI();
// Example dummy function hard coded to return the same weather
// In production, this could be your backend API or an external API
function getCurrentWeather(location, unit = 'fahrenheit') {
if (location.toLowerCase().includes('tokyo')) {
return JSON.stringify({
location: 'Tokyo',
temperature: '10',
unit: 'celsius',
});
} else if (location.toLowerCase().includes('san francisco')) {
return JSON.stringify({
location: 'San Francisco',
temperature: '72',
unit: 'fahrenheit',
});
} else if (location.toLowerCase().includes('paris')) {
return JSON.stringify({
location: 'Paris',
temperature: '22',
unit: 'fahrenheit',
});
} else {
return JSON.stringify({ location, temperature: 'unknown' });
}
}
async function runConversation() {
console.log('hello');
// Step 1: send the conversation and available functions to the model
const messages = [
{
role: 'user',
content: "What's the weather like in San Francisco, Tokyo, and Paris?",
},
];
const tools = [
{
type: 'function',
function: {
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 response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-0125',
messages: messages,
tools: tools,
tool_choice: 'auto', // auto is default, but we'll be explicit
});
const responseMessage = response.choices[0].message;
// Step 2: check if the model wanted to call a function
const toolCalls = responseMessage.tool_calls;
if (responseMessage.tool_calls) {
// Step 3: call the function
// Note: the JSON response may not always be valid; be sure to handle errors
const availableFunctions = {
get_current_weather: getCurrentWeather,
}; // only one function in this example, but you can have multiple
messages.push(responseMessage); // extend conversation with assistant's reply
for (const toolCall of toolCalls) {
const functionName = toolCall.function.name;
const functionToCall = availableFunctions[functionName];
const functionArgs = JSON.parse(toolCall.function.arguments);
const functionResponse = functionToCall(
functionArgs.location,
functionArgs.unit,
);
messages.push({
tool_call_id: toolCall.id,
role: 'tool',
name: functionName,
content: functionResponse,
}); // extend conversation with function response
}
const secondResponse = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-0125',
messages: messages,
}); // get a new response from the model where it can see the function response
return secondResponse.choices;
}
}
runConversation().then(console.log).catch(console.error);