From 849563ed4a424dc6d507dcb3387efb61ae15cd75 Mon Sep 17 00:00:00 2001 From: Yuanzhe Liu <294068487@qq.com> Date: Mon, 13 Mar 2023 13:06:48 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=AF=B9OpenAI?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ package-lock.json | 37 +++++++++++++++++++++++++++++ package.json | 1 + tgbot/{bot => api}/goindex.js | 0 tgbot/api/openai.js | 44 +++++++++++++++++++++++++++++++++++ tgbot/bot/airportsub.js | 35 +++++++++++++++++++++------- 6 files changed, 111 insertions(+), 8 deletions(-) rename tgbot/{bot => api}/goindex.js (100%) create mode 100644 tgbot/api/openai.js diff --git a/README.md b/README.md index 3d05db9..f5a2bc0 100644 --- a/README.md +++ b/README.md @@ -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常用工具 diff --git a/package-lock.json b/package-lock.json index 569b15c..cb91f5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index a7bc24f..3cad542 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tgbot/bot/goindex.js b/tgbot/api/goindex.js similarity index 100% rename from tgbot/bot/goindex.js rename to tgbot/api/goindex.js diff --git a/tgbot/api/openai.js b/tgbot/api/openai.js new file mode 100644 index 0000000..07e7a42 --- /dev/null +++ b/tgbot/api/openai.js @@ -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, +} \ No newline at end of file diff --git a/tgbot/bot/airportsub.js b/tgbot/bot/airportsub.js index 30e420d..6f696ae 100644 --- a/tgbot/bot/airportsub.js +++ b/tgbot/bot/airportsub.js @@ -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];