-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
53 lines (44 loc) · 1.79 KB
/
helper.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
import { Configuration, OpenAIApi } from 'openai';
const configuration = new Configuration({
//organization: 'org-8ION5v6gukEFsPmZBBBWhSwx',
apiKey: process.env.OPENAI_API_KEY
});
const openai = new OpenAIApi(configuration);
async function getCompletion( prompt, model = 'gpt-3.5-turbo', temperature = 0 ) {
const messages = [{
role: 'user',
content: prompt
}];
const completion = await openai.createChatCompletion({
model: model,
messages: messages,
temperature: temperature // The degree of randomness in the output
});
return completion.data.choices[0].message.content;
};
// This function expects an array of messages, e.g.:
//
// let messages = [
// {'role':'system', 'content':'You are an assistant...'},
// {'role':'user', 'content':'tell me a joke'},
// {'role':'assistant', 'content':'Why did the chicken cross the road'},
// {'role':'user', 'content':'I don\'t know'}
// ]
//
// Note the different role specifications:
//
// system: Sets the behaviour of the system for the ensuing conversation.
// We could have said: 'You are an assistant that speaks like Shakespeare.'
// The user never sees this but it set's the tone the system
// uses in the conversation
// assistant: This is the chat models compleations.
// user: This is the enties the user makes
async function getCompletionFromMessages( messages, model = 'gpt-3.5-turbo', temperature = 0 ) {
const completion = await openai.createChatCompletion({
model: model,
messages: messages,
temperature: temperature // The degree of randomness in the output
});
return completion.data.choices[0].message.content;
};
export { getCompletion, getCompletionFromMessages };