/
index.js
29 lines (26 loc) · 939 Bytes
/
index.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
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
document.getElementById('btn').addEventListener('click', () => {
if (document.getElementById('question').value) {
document.querySelector('.content').innerHTML = '加载中...'
openai.createCompletion({
model: "text-davinci-003",
prompt: document.getElementById('question').value,
temperature: 0.7,
max_tokens: 3000,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
}).then((res) => {
if (res.data.choices[0].text.includes('```')) {
document.querySelector('.pre').style.display = 'block'
document.querySelector('.pre').innerText = res.data.choices[0].text;
} else {
document.querySelector('.content').innerText = res.data.choices[0].text;
}
})
}
})