-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (54 loc) · 1.82 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
import OpenAI from "openai";
import dotenv from "dotenv";
import axios from "axios";
import moment from 'moment-timezone'
dotenv.config();
const openai = new OpenAI({
apiKey: process.env.OPENAI_KEY,
});
async function lookupTime(location) {
const res = await axios.get(
`http://worldtimeapi.org/api/timezone/${location}`
);
const { datetime } = res.data;
let time = moment.tz(datetime, location).format('h:mmA')
console.log(`El tiempo actual en ${location} es ${time}`);
}
async function main() {
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Que hora es actualmente en España?" },
],
functions: [
{
name: "lookupTime",
description: "Look up the current time in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description:
'La ubicacion de la cual se quiere saber la hora actual. Ejemplo: "Bogota". Pero debe estar escrigo con el nombre del timezone por ejemplo: Asia/Shangai, America/Bogota, Europe/Madrid, etc.',
},
},
required: ["location"],
},
},
],
function_call: "auto",
});
const completionResponse = completion.choices[0].message;
if (!completionResponse.content) {
const functionCallName = completionResponse.function_call.name;
console.log(`Funcion llamada: ${functionCallName}`);
if (functionCallName === "lookupTime") {
const args = JSON.parse(completionResponse.function_call.arguments);
console.log(arguments);
lookupTime(args.location);
}
}
}
main();