forked from wangrongding/wechat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 1015 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
29
30
31
32
33
34
35
import { remark } from 'remark'
import stripMarkdown from 'strip-markdown'
import { Configuration, OpenAIApi } from 'openai'
import dotenv from 'dotenv'
const env = dotenv.config().parsed // 环境参数
const configuration = new Configuration({
apiKey: env.OPENAI_API_KEY,
})
const openai = new OpenAIApi(configuration)
export async function getOpenAiReply(prompt) {
console.log('🚀🚀🚀 / prompt', prompt)
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt: prompt,
temperature: 0.9, // 每次返回的答案的相似度0-1(0:每次都一样,1:每次都不一样)
max_tokens: 4000,
top_p: 1,
frequency_penalty: 0.0,
presence_penalty: 0.6,
stop: [' Human:', ' AI:'],
})
const reply = markdownToText(response.data.choices[0].text)
console.log('🚀🚀🚀 / reply', reply)
return reply
}
function markdownToText(markdown) {
return remark()
.use(stripMarkdown)
.processSync(markdown ?? '')
.toString()
}