-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
130 lines (120 loc) · 3.27 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
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
import { Configuration, OpenAIApi } from 'openai';
import scraper from './scraper.js';
const configuration = new Configuration({
organization: "org-yourorg",
apiKey: "sk-yourapikey",
});
const openai = new OpenAIApi(configuration);
// Define your function
function helloWorld(appendString){
let hello = "Hello World! " + appendString
return hello
}
// Get the current time of day
function getTimeOfDay(){
let date = new Date()
let hours = date.getHours()
let minutes = date.getMinutes()
let seconds = date.getSeconds()
let timeOfDay = "AM"
if(hours > 12){
hours = hours - 12
timeOfDay = "PM"
}
return hours + ":" + minutes + ":" + seconds + " " + timeOfDay
}
// Define your ChatGPT Function
async function callChatGPTWithFunctions(appendString){
let messages = [{
role: "system",
content: "Perform function requests for the user",
},{
role: "user",
content: "Hello, I am a user, I would like to get 5 book recommendations with the keyword 'ChatGPT'",
}];
// Step 1: Call ChatGPT with the function name
let chat = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages,
functions: [{
name: "helloWorld",
description: "Prints hello world with the string passed to it",
parameters: {
type: "object",
properties: {
appendString: {
type: "string",
description: "The string to append to the hello world message",
},
},
require: ["appendString"],
}
},{
name: "scraper",
description: "Scraps the book website goodreads for books with the keyword passed to it",
parameters: {
type: "object",
properties: {
keyword: {
type: "string",
description: "The keyword to search for",
},
},
require: ["keyword"],
}
},
{
name: "getTimeOfDay",
description: "Get the time of day.",
parameters: {
type: "object",
properties: {
},
require: [],
}
}],
function_call: "auto",
})
let wantsToUseFunction = chat.data.choices[0].finish_reason == "function_call"
let content = ""
// Step 2: Check if ChatGPT wants to use a function
if(wantsToUseFunction){
// Step 3: Use ChatGPT arguments to call your function
if(chat.data.choices[0].message.function_call.name == "helloWorld"){
let argumentObj = JSON.parse(chat.data.choices[0].message.function_call.arguments)
content = helloWorld(argumentObj.appendString)
messages.push(chat.data.choices[0].message)
messages.push({
role: "function",
name: "helloWorld",
content,
})
}
if(chat.data.choices[0].message.function_call.name == "scraper"){
let argumentObj = JSON.parse(chat.data.choices[0].message.function_call.arguments)
content = await scraper(argumentObj.keyword)
messages.push(chat.data.choices[0].message)
messages.push({
role: "function",
name: "scraper",
content,
})
}
if(chat.data.choices[0].message.function_call.name == "getTimeOfDay"){
content = getTimeOfDay()
messages.push(chat.data.choices[0].message)
messages.push({
role: "function",
name: "getTimeOfDay",
content,
})
}
}
// Step 4: Call ChatGPT again with the function response
let step4response = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages,
});
console.log(step4response.data.choices[0])
}
callChatGPTWithFunctions()