mirror of
https://github.com/lyz05/danmaku.git
synced 2026-07-01 17:56:06 +08:00
feat: 增加对OpenAI的支持
This commit is contained in:
@@ -32,6 +32,8 @@ Express框架的性能也比Python的Django要好很多。
|
||||
flyctl scale count 0
|
||||
flyctl regions add sea
|
||||
flyctl regions remove nrt
|
||||
flyctl config env
|
||||
flyctl secrets set DEBUG=true
|
||||
```
|
||||
|
||||
# Node常用工具
|
||||
|
||||
37
package-lock.json
generated
37
package-lock.json
generated
@@ -31,6 +31,7 @@
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"node-cron": "^3.0.2",
|
||||
"node-telegram-bot-api": "^0.60.0",
|
||||
"openai": "^3.2.1",
|
||||
"pako": "^1.0.11",
|
||||
"querystring": "^0.2.1",
|
||||
"whacko": "^0.19.1",
|
||||
@@ -5897,6 +5898,23 @@
|
||||
"wrappy": "1"
|
||||
}
|
||||
},
|
||||
"node_modules/openai": {
|
||||
"version": "3.2.1",
|
||||
"resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz",
|
||||
"integrity": "sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==",
|
||||
"dependencies": {
|
||||
"axios": "^0.26.0",
|
||||
"form-data": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/openai/node_modules/axios": {
|
||||
"version": "0.26.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
|
||||
"integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.14.8"
|
||||
}
|
||||
},
|
||||
"node_modules/optionator": {
|
||||
"version": "0.9.1",
|
||||
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.1.tgz",
|
||||
@@ -12653,6 +12671,25 @@
|
||||
"wrappy": "1"
|
||||
}
|
||||
},
|
||||
"openai": {
|
||||
"version": "3.2.1",
|
||||
"resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz",
|
||||
"integrity": "sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==",
|
||||
"requires": {
|
||||
"axios": "^0.26.0",
|
||||
"form-data": "^4.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": {
|
||||
"version": "0.26.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
|
||||
"integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
|
||||
"requires": {
|
||||
"follow-redirects": "^1.14.8"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"optionator": {
|
||||
"version": "0.9.1",
|
||||
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.1.tgz",
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"node-cron": "^3.0.2",
|
||||
"node-telegram-bot-api": "^0.60.0",
|
||||
"openai": "^3.2.1",
|
||||
"pako": "^1.0.11",
|
||||
"querystring": "^0.2.1",
|
||||
"whacko": "^0.19.1",
|
||||
|
||||
44
tgbot/api/openai.js
Normal file
44
tgbot/api/openai.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const { Configuration, OpenAIApi } = require("openai");
|
||||
require("dotenv").config({ path: "../.env" });
|
||||
|
||||
const configuration = new Configuration({
|
||||
apiKey: process.env.OPENAI_API_KEY,
|
||||
});
|
||||
const openai = new OpenAIApi(configuration);
|
||||
|
||||
async function completions(prompt) {
|
||||
const completion = await openai.createCompletion({
|
||||
model: "text-davinci-003",
|
||||
prompt: prompt,
|
||||
});
|
||||
console.log(completion.data.choices[0].text);
|
||||
}
|
||||
|
||||
async function chat(content, messages) {
|
||||
const prompt = { "role": "system", "content": "You are a helpful assistant." }
|
||||
if (!messages || messages.length == 0) {
|
||||
messages = [prompt]
|
||||
}
|
||||
messages.push({ "role": "user", "content": content });
|
||||
const completion = await openai.createChatCompletion({
|
||||
"model": "gpt-3.5-turbo",
|
||||
"messages": messages,
|
||||
});
|
||||
messages.push(completion.data.choices[0].message);
|
||||
// console.log(messages)
|
||||
return [completion.data.choices[0].message.content, messages];
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const messages = [];
|
||||
// completions("护眼")
|
||||
await chat("我们来玩个猜数游戏", messages);
|
||||
await chat("规则是你心中默想一个数,然后我猜你想的数,数的范围是一到一百", messages);
|
||||
await chat("我猜50", messages);
|
||||
console.log(messages)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
completions,
|
||||
chat,
|
||||
}
|
||||
@@ -4,7 +4,8 @@ const yaml = require("js-yaml");
|
||||
const TelegramBot = require("node-telegram-bot-api");
|
||||
const axios = require("axios");
|
||||
const oss = require("../../utils/oss");
|
||||
const goindex = require("./goindex");
|
||||
const goindex = require("../api/goindex");
|
||||
const openai = require("../api/openai");
|
||||
|
||||
async function finduserbychatid(chatid) {
|
||||
const database = await oss.get("SUB/database.yaml");
|
||||
@@ -32,6 +33,7 @@ async function setchatidbyuser(user, chatid) {
|
||||
|
||||
module.exports = (TOKEN) => {
|
||||
const game = {};
|
||||
let openai_messages = {};
|
||||
let setu = {};
|
||||
const bot = new TelegramBot(TOKEN, { polling: true });
|
||||
|
||||
@@ -61,18 +63,35 @@ module.exports = (TOKEN) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 智能聊天机器人
|
||||
bot.on("text", (msg) => {
|
||||
// // 智能聊天机器人
|
||||
// bot.on("text", (msg) => {
|
||||
// if (msg.text.indexOf("/") === -1) {
|
||||
// bot.sendMessage(msg.chat.id, `you said: ${msg.text}`);
|
||||
// axios.get(`https://api.qingyunke.com/api.php?key=free&appid=0&msg=${encodeURI(msg.text)}`)
|
||||
// .then((res) => {
|
||||
// console.log(res.data);
|
||||
// bot.sendMessage(msg.chat.id, res.data.content);
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
|
||||
// ChatGPT版智能聊天机器人
|
||||
bot.on("text", async (msg) => {
|
||||
if (msg.text.indexOf("/") === -1) {
|
||||
bot.sendMessage(msg.chat.id, `you said: ${msg.text}`);
|
||||
axios.get(`https://api.qingyunke.com/api.php?key=free&appid=0&msg=${encodeURI(msg.text)}`)
|
||||
.then((res) => {
|
||||
console.log(res.data);
|
||||
bot.sendMessage(msg.chat.id, res.data.content);
|
||||
});
|
||||
let messages = openai_messages[msg.chat.id] || [], res;
|
||||
[res, messages] = await openai.chat(msg.text, messages);
|
||||
const length = (messages.length - 1) / 2;
|
||||
bot.sendMessage(msg.chat.id, `${res}\n\nPowered by OpenAI 连续对话了${length}次`);
|
||||
openai_messages[msg.chat.id] = messages;
|
||||
}
|
||||
});
|
||||
|
||||
bot.onText(/\/clear/, (msg) => {
|
||||
openai_messages[msg.chat.id] = [];
|
||||
bot.sendMessage(msg.chat.id, "已清空对话记录");
|
||||
});
|
||||
|
||||
// 欢迎页面
|
||||
bot.onText(/\/start/, (msg) => {
|
||||
let name = [msg.from.first_name];
|
||||
|
||||
Reference in New Issue
Block a user