-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
106 lines (89 loc) · 2.44 KB
/
test.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
101
102
103
104
105
106
import OpenAI from 'openai'
require('dotenv').config()
const openAI = new OpenAI({
apiKey: process.env.OPENAI_KEY,
})
const functions = [
{
name: 'get_current_weather',
description: 'Get the current weather in a given location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description:
'The location to get the weather for, city and country, e.g. "London, UK"',
},
},
required: ['location'],
},
function: async ({ location }: { location: string }) => {
const response = await fetch(
`https://nominatim.openstreetmap.org/search?format=json&q=${location}`
).then((res) => res.json())
const { lat, lon } = response[0]
const response2 = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}&hourly=temperature_2m,weathercode`
)
const data = await response2.json()
//get current weather
const currentWeather = data.hourly[0]
return {
temperature: '20',
}
console.log(data)
},
},
]
const handleFunctionCall = async (message: string) => {
const res1 = await openAI.chat.completions.create({
model: 'gpt-3.5-turbo-0613',
functions,
messages: [
{
content: message,
role: 'user',
},
],
})
if (res1.choices[0].finish_reason === 'function_call') {
const selectedFn = functions.find(
(f) => f.name === res1.choices[0].message.function_call.name
)
if (!selectedFn) {
throw new Error('Function not found')
}
const result = await selectedFn.function(
JSON.parse(res1.choices[0].message.function_call.arguments)
)
const res2 = await openAI.chat.completions.create({
model: 'gpt-3.5-turbo-0613',
messages: [
{
content: message,
role: 'user',
},
{
function_call: res1.choices[0].message.function_call,
content: '',
role: 'assistant',
},
{
content: JSON.stringify(result),
name: res1.choices[0].message.function_call.name,
role: 'function',
},
],
})
return res2
}
return res1
}
const main = async (message: string) => {
const completion = await handleFunctionCall(message)
console.log(completion)
console.log(completion.choices[0].message)
}
const message = process.argv[2]
main(message)