forked from Azure-Samples/genai-gateway-apim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiService.js
83 lines (74 loc) · 2.79 KB
/
apiService.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
let dotenv = require('dotenv');
// NOTE: remove this when deploying as it will read from App Service configuration
dotenv.config();
const URL = `${process.env.APIM_ENDPOINT}/${process.env.API_SUFFIX}/deployments/${process.env.DEPLOYMENT_ID}/completions?api-version=${process.env.API_VERSION}`;
const URL_CHAT = `${process.env.APIM_ENDPOINT}/${process.env.API_SUFFIX}/deployments/${process.env.DEPLOYMENT_ID}/chat/completions?api-version=${process.env.API_VERSION}`
console.log("[API] URL: ",URL);
module.exports = {
getChatCompletion(prompt) {
let headers = null;
let body = {
"model":"gpt-35-turbo","messages":[
{
"role":"system","content":"You're a helpful assistant"
},
{
"role":"user","content":prompt
}
]};
return fetch(URL_CHAT, {
method: "POST",
headers: {
"Ocp-Apim-Subscription-Key": process.env.SUBSCRIPTION_KEY,
"Content-Type": "application/json"
},
body: JSON.stringify(body)
})
.then(response => {
headers = response.headers;
return response.json();
}).then(data => {
// throw if choices is empty
console.log("[API] data: ",data);
console.log("[API] response: ",data.choices[0].message.content);
if (data.choices.length === 0) {
throw new Error('No response');
} else {
console.log("[API] responses: ",data.choices[0].message.content);
return {
text : data.choices[0].message.content,
headers : headers
};
}
});
},
getCompletion(prompt) {
let headers = null;
let body = {
"prompt":prompt,
"max_tokens":400
};
return fetch(URL, {
method: "POST",
headers: {
"Ocp-Apim-Subscription-Key": process.env.SUBSCRIPTION_KEY,
"Content-Type": "application/json"
},
body: JSON.stringify(body)
}).then(response => {
headers = response.headers;
return response.json();
}).then(data => {
// throw if choices is empty
if (data.choices.length === 0) {
throw new Error('No completion choices');
} else {
console.log("[API] responses: ",data.choices);
return {
text : data.choices[0].text,
headers : headers
};
}
});
}
}