From 61fb54bcf20a450a01ac4e434df523ba8b6d6b15 Mon Sep 17 00:00:00 2001 From: tom-snow Date: Tue, 16 May 2023 13:02:04 +0000 Subject: [PATCH] =?UTF-8?q?v1.2=20=E6=B7=BB=E5=8A=A0=E7=83=AD=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=BE=9D=E8=B5=96=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PyroEdgeGptBot.py | 54 +++++++++++++++++++++++++++++++++++++++++++---- README.md | 2 ++ README_zh.md | 2 ++ requirements.txt | 2 +- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/PyroEdgeGptBot.py b/PyroEdgeGptBot.py index 429cc25..42abef9 100644 --- a/PyroEdgeGptBot.py +++ b/PyroEdgeGptBot.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import re +import sys, importlib import json import time import pytz @@ -8,6 +9,8 @@ import asyncio import contextlib +import EdgeGPT + # import py3langid as langid from logging.handlers import TimedRotatingFileHandler from datetime import datetime @@ -19,7 +22,6 @@ from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardMarkup, \ ReplyKeyboardRemove, InlineQueryResultPhoto, InlineQueryResultArticle, InputTextMessageContent, \ InputMediaPhoto -from EdgeGPT import Chatbot, ConversationStyle from config import API_ID, API_KEY, BOT_TOKEN, ALLOWED_USER_IDS, COOKIE_FILE, NOT_ALLOW_INFO, \ BOT_NAME, SUGGEST_MODE, DEFAULT_CONVERSATION_STYLE_TYPE, RESPONSE_TYPE, STREAM_INTERVAL, \ @@ -78,7 +80,7 @@ def formatTime(self, record, datefmt=None): def check_conversation_style(style): - if style in ConversationStyle.__members__: + if style in EdgeGPT.ConversationStyle.__members__: return True return False @@ -103,8 +105,8 @@ class BAD_CONFIG_ERROR(Exception): tmpLoop = asyncio.get_event_loop() for user_id in ALLOWED_USER_IDS: EDGES[user_id] = { - "bot": tmpLoop.run_until_complete(Chatbot.create(cookie_path=COOKIE_FILE)), # 共用一个 cookie.json 文件 - "style": ConversationStyle[DEFAULT_CONVERSATION_STYLE_TYPE], + "bot": tmpLoop.run_until_complete(EdgeGPT.Chatbot.create(cookie_path=COOKIE_FILE)), # 共用一个 cookie.json 文件 + "style": EdgeGPT.ConversationStyle[DEFAULT_CONVERSATION_STYLE_TYPE], "response": RESPONSE_TYPE, "interval": STREAM_INTERVAL, "suggest": SUGGEST_MODE, @@ -183,6 +185,50 @@ async def set_response_handle(bot, update): reply_text = f"{BOT_NAME}: set RESPONSE_TYPE to '{EDGES[update.chat.id]['response']}'." await bot.send_message(chat_id=update.chat.id, text=reply_text) +# 更新依赖 +@pyro.on_message(filters.command("update") & filters.private & filters.chat(ALLOWED_USER_IDS)) +async def set_update_handle(bot, update): + logger.info(f"Receive commands [{update.command}] from [{update.chat.id}]") + msg = await bot.send_message(chat_id=update.chat.id + , text=f"{BOT_NAME}: Updateing [EdgeGPT](https://github.com/acheong08/EdgeGPT)." + , disable_web_page_preview=True) + # 关闭连接 + for user_id in EDGES: + await EDGES[user_id]["bot"].close() + # 更新&重载依赖 + python_path = sys.executable + executor = await asyncio.create_subprocess_shell(f"{python_path} -m pip install -U EdgeGPT" + , stdout=asyncio.subprocess.PIPE + , stderr=asyncio.subprocess.PIPE + , stdin=asyncio.subprocess.PIPE) + stdout, stderr = await executor.communicate() + logger.info(f"[set_update_handle] stdout: {stdout.decode()}") + result = "" + old_version = "" + new_version = "" + for line in stdout.decode().split("\n"): # 解析日志 + if "Successfully uninstalled EdgeGPT-" in line: + old_version = line.replace("Successfully uninstalled EdgeGPT-", "").strip() + if "Successfully installed EdgeGPT-" in line: + new_version = line.replace("Successfully installed EdgeGPT-", "").strip() + if old_version and new_version: + result = f"[EdgeGPT](https://github.com/acheong08/EdgeGPT): {old_version} -> {new_version}" + err = False + if "WARNING" not in stderr.decode(): + err = True + if err: + logger.error(f"[set_update_handle] stderr: {stderr.decode()}") + result += stderr.decode() + else: + logger.warning(f"[set_update_handle] stderr: {stderr.decode()}") + + importlib.reload(EdgeGPT) + # 重新连接 + for user_id in EDGES: + EDGES[user_id]["bot"] = await EdgeGPT.Chatbot.create(cookie_path=COOKIE_FILE) + EDGES[user_id]["style"] = EdgeGPT.ConversationStyle[DEFAULT_CONVERSATION_STYLE_TYPE] + await msg.edit_text(f"{BOT_NAME}: Updated!\n\n{result}", disable_web_page_preview=True) + # 设置 stream 模式消息更新间隔 @pyro.on_message(filters.command("interval") & filters.private & filters.chat(ALLOWED_USER_IDS)) async def set_interval_handle(bot, update): diff --git a/README.md b/README.md index c0f30e4..3a09e45 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ _A telegram bot with Bing AI ( using [EdgeGPT](https://github.com/acheong08/Edge - [ ] Prompt Support - [x] Image generation (inline query and command mode) - [ ] Export conversation to Notion +- [x] Hot update the EdgeGPT dependence # Setup ## Requirements @@ -95,6 +96,7 @@ switch - Switch the conversation style interval - Set edit interval suggest_mode - Set the suggest mode image_gen - Generate images +update - Update the EdgeGPT dependence ``` diff --git a/README_zh.md b/README_zh.md index 996f1e4..d72a770 100644 --- a/README_zh.md +++ b/README_zh.md @@ -15,6 +15,7 @@ _在 Telegram 上使用 Bing AI ( 使用 [EdgeGPT](https://github.com/acheong08/ - [ ] 支持 prompt - [x] 图片生成 (inline query 与命令模式) - [ ] 导出会话到 notion +- [x] 热更新 EdgeGPT 依赖 # 安装 ## 依赖 @@ -99,6 +100,7 @@ switch - 切换聊天模式 interval - 设置修改消息间隔 suggest_mode - 建议消息模式 image_gen - 图片生成 +update - 热更新 EdgeGPT 依赖 ``` diff --git a/requirements.txt b/requirements.txt index 5641c51..7aedeef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ python-dotenv>=1.0.0 Pyrogram>=2.0 -EdgeGPT>=0.3.8 +EdgeGPT>=0.4.2 tgcrypto aiohttp pytz