-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenAIService.js
38 lines (33 loc) · 1.15 KB
/
openAIService.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
import dotenv from 'dotenv';
dotenv.config();
const model = process.env.USE_GPT_4 ? 'gpt-4' : 'gpt-3.5-turbo';
export async function getEmbeddingVector(textInput) {
const embeddingResponse = await (await fetch('https://api.openai.com/v1/embeddings', {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
},
body: JSON.stringify({
model: 'text-embedding-ada-002',
input: textInput
}),
method: 'POST'
})).json()
const embedding = embeddingResponse.data[0].embedding;
return embedding;
}
export async function getChatResponse(prompt) {
const queryResponse = await (await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
},
body: JSON.stringify({
model,
messages: [{ "role": "user", "content": prompt }],
}),
method: 'POST'
})).json()
const answer = queryResponse.choices[0].message.content;
return answer;
}