-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.js
110 lines (98 loc) · 2.76 KB
/
function.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
import 'dotenv/config';
import { openai } from './openai.js';
import math from 'advanced-calculator';
const QUESTION = process.argv[2] || 'hi';
const messages = [
{
role: 'user',
content: QUESTION,
},
];
export const functions = {
calculate({ expression }) {
return math.evaluate(expression);
},
async generateImage({ prompt }) {
const result = await openai.images.generate({ prompt });
return result.data[0].url;
},
};
export const getCompletion = async (messages) => {
try {
const response = await openai.chat.completions.create({
model: 'gpt-4-0613',
messages,
functions: [
{
name: 'calculate',
description: 'Run a math expression',
parameters: {
type: 'object',
properties: {
expression: {
type: 'string',
description: 'The math expression to evaluate like "2 * 3 + (21 / 2) ^ 2"',
},
},
required: ['expression'],
},
},
{
name: 'generateImage',
description: 'Create or generate image based on a description',
parameters: {
type: 'object',
properties: {
prompt: {
type: 'string',
description: 'The description of the image that is to be generated',
},
},
required: ['prompt'],
},
},
],
temperature: 0,
});
if (!response.choices || response.choices.length === 0) {
throw new Error('No choices returned from OpenAI API');
}
return response;
} catch (error) {
console.error('Error getting completion:', error);
throw error;
}
};
// The following code is for testing purposes and should be removed or commented out in production
let response;
while (true) {
try {
response = await getCompletion(messages);
if (response.choices[0].finish_reason === 'stop') {
console.log(response.choices[0].message.content);
break;
} else if (response.choices[0].finish_reason === 'function_call') {
const fnName = response.choices[0].message.function_call.name;
const args = response.choices[0].message.function_call.arguments;
const functionToCall = functions[fnName];
const params = JSON.parse(args);
const result = await functionToCall(params);
messages.push({
role: 'assistant',
content: null,
function_call: {
name: fnName,
arguments: args,
},
});
messages.push({
role: 'function',
name: fnName,
content: JSON.stringify({ result: result }),
});
}
} catch (error) {
console.error('Error in while loop:', error);
break;
}
}