-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.js
62 lines (55 loc) · 1.64 KB
/
openai.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 axios from 'axios'
import { config } from "dotenv"; config();
const OPENAI_API_KEY = process.env.OA_API_KEY
const OPENAI_ORG = process.env.ORG_ID
const ask = (tradeQuery) => {
return new Promise((resolve, reject) => {
const data = JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: 'This assistant is trained on a variety of data, including specific databases and general world knowledge, to assist users in buying and selling goods in Africa.'
},
{
role: 'user',
content: `${tradeQuery}`
},
{
role: 'user',
content: 'Using 160 characters, what is the answer?'
},
],
temperature: 1,
top_p: 1,
n: 1,
stream: false,
max_tokens: 250,
presence_penalty: 0,
frequency_penalty: 0
})
const config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api.openai.com/v1/chat/completions',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`,
'OpenAI-Organization': OPENAI_ORG,
},
data
}
axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data))
console.log(response.data.choices[0].message.content)
resolve({ status: 'success', message: response.data.choices[0].message.content })
})
.catch((error) => {
console.log("you got an error")
console.error(error)
resolve({ status: 'error', message: error })
})
})
}
export default ask