forked from ox4f5da2/ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.cjs
40 lines (35 loc) · 1 KB
/
interface.cjs
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
const { Configuration, OpenAIApi } = require("openai");
const express = require("express");
const app = express();
// 初始化 chatgpt 接口
const configuration = new Configuration({
apiKey: "你自己的 API key", // 官网获取
});
const openai = new OpenAIApi(configuration);
app.get('/chatgpt', async (req, res) => {
const question = req.query.question;
if (!question) {
res.send({
message: "The query must contain the question parameter!",
status: 400
})
} else {
const result = await openai.createCompletion({
model: "text-davinci-003", // 接口类型
prompt: question,
max_tokens: 3000, // 结果的长度
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
});
const answer = result.data.choices[0].text.replaceAll("\n", "");
res.send({
message: "Get the result of the problem successfully!",
data: {
text: answer
},
status: 200
})
}
})
app.listen(3000, () => console.log("running..."))