mirror of
https://github.com/lyz05/danmaku.git
synced 2026-02-02 17:59:53 +08:00
feat: OpenAI: 增加prompt支持
This commit is contained in:
@@ -5,6 +5,11 @@ const configuration = new Configuration({
|
|||||||
apiKey: process.env.OPENAI_API_KEY,
|
apiKey: process.env.OPENAI_API_KEY,
|
||||||
});
|
});
|
||||||
const openai = new OpenAIApi(configuration);
|
const openai = new OpenAIApi(configuration);
|
||||||
|
let prompt = { "role": "system", "content": "You are a helpful assistant." }
|
||||||
|
async function setprompt(content) {
|
||||||
|
prompt = { "role": "system", "content": content || "You are a helpful assistant." }
|
||||||
|
return prompt.content
|
||||||
|
}
|
||||||
|
|
||||||
async function completions(prompt) {
|
async function completions(prompt) {
|
||||||
const completion = await openai.createCompletion({
|
const completion = await openai.createCompletion({
|
||||||
@@ -15,7 +20,6 @@ async function completions(prompt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function chat(content, messages) {
|
async function chat(content, messages) {
|
||||||
const prompt = { "role": "system", "content": "You are a helpful assistant." }
|
|
||||||
if (!messages || messages.length == 0) {
|
if (!messages || messages.length == 0) {
|
||||||
messages = [prompt]
|
messages = [prompt]
|
||||||
}
|
}
|
||||||
@@ -25,7 +29,7 @@ async function chat(content, messages) {
|
|||||||
"messages": messages,
|
"messages": messages,
|
||||||
});
|
});
|
||||||
messages.push(completion.data.choices[0].message);
|
messages.push(completion.data.choices[0].message);
|
||||||
// console.log(messages)
|
console.log(messages)
|
||||||
return [completion.data.choices[0].message.content, messages];
|
return [completion.data.choices[0].message.content, messages];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,4 +45,5 @@ async function main() {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
completions,
|
completions,
|
||||||
chat,
|
chat,
|
||||||
|
setprompt,
|
||||||
}
|
}
|
||||||
@@ -92,6 +92,13 @@ module.exports = (TOKEN) => {
|
|||||||
bot.sendMessage(msg.chat.id, "已清空对话记录");
|
bot.sendMessage(msg.chat.id, "已清空对话记录");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
bot.onText(/\/prompt/, async (msg) => {
|
||||||
|
const prompt = msg.text.replace("/prompt ", "").replace("/prompt", "");
|
||||||
|
openai_messages[msg.chat.id] = [];
|
||||||
|
const res = await openai.setprompt(prompt);
|
||||||
|
bot.sendMessage(msg.chat.id, `已设置对话提示为:${res}`);
|
||||||
|
});
|
||||||
|
|
||||||
// 欢迎页面
|
// 欢迎页面
|
||||||
bot.onText(/\/start/, (msg) => {
|
bot.onText(/\/start/, (msg) => {
|
||||||
let name = [msg.from.first_name];
|
let name = [msg.from.first_name];
|
||||||
@@ -211,6 +218,14 @@ module.exports = (TOKEN) => {
|
|||||||
command: "help",
|
command: "help",
|
||||||
description: "帮助"
|
description: "帮助"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
command: "clear",
|
||||||
|
description: "清空OpenAI聊天记录"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
command: "prompt",
|
||||||
|
description: "设置OpenAI聊天提示句"
|
||||||
|
}
|
||||||
];
|
];
|
||||||
const helpMsgText = helpMsg.map((item) => `/${item.command} - ${item.description}`)
|
const helpMsgText = helpMsg.map((item) => `/${item.command} - ${item.description}`)
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user