-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetchData.js
45 lines (41 loc) · 1.1 KB
/
fetchData.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
import fetch, { Headers } from "node-fetch";
export default function fetchData(req, res, token) {
var content =
req.body.content || res.status(500).json({ error: "Content is required" });
// var myHeaders = new Headers();
// myHeaders.append("Authorization", `Bearer ${token}`);
// myHeaders.append("Content-Type", "application/json");
// var raw = JSON.stringify({
// model: "gpt-3.5-turbo",
// messages: [
// {
// role: "user",
// content: content,
// },
// ],
// });
fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: new Headers({
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
}),
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: content,
},
],
}),
redirect: "follow",
})
.then((response) => response.text())
.then((result) => {
res.status(200).json({ result: result });
})
.catch((error) => {
res.status(500).json({ error: error.message });
});
}