Merge pull request #22 from weiensong/master

feat: model name log, args help hint and the proxy of key does not pa…
This commit is contained in:
Changhua
2023-11-16 21:53:24 -06:00
committed by GitHub
7 changed files with 52 additions and 25 deletions

View File

@@ -3,8 +3,20 @@ from enum import IntEnum, unique
@unique @unique
class ChatType(IntEnum): class ChatType(IntEnum):
UnKnown = 0 # 未知, 即未设置 # UnKnown = 0 # 未知, 即未设置
TIGER_BOT = 1 # TigerBot TIGER_BOT = 1 # TigerBot
CHATGPT = 2 # ChatGPT CHATGPT = 2 # ChatGPT
XINGHUO_WEB = 3 # 讯飞星火 XINGHUO_WEB = 3 # 讯飞星火
ChatGLM = 4 # ChatGLM CHATGLM = 4 # ChatGLM
@staticmethod
def is_in_chat_types(chat_type: int) -> bool:
if chat_type in [ChatType.TIGER_BOT.value, ChatType.CHATGPT.value,
ChatType.XINGHUO_WEB.value, ChatType.CHATGLM.value]:
return True
return False
@staticmethod
def help_hint() -> str:
return str({member.value: member.name for member in ChatType}).replace('{', '').replace('}', '')

View File

@@ -16,7 +16,7 @@ from chatglm.tool_registry import dispatch_tool, extract_code, get_tools
functions = get_tools() functions = get_tools()
class ChatGLM(): class ChatGLM:
def __init__(self, config={}, wcf: Optional[Wcf] = None, max_retry=5) -> None: def __init__(self, config={}, wcf: Optional[Wcf] = None, max_retry=5) -> None:
openai.api_key = config.get('key', 'XXX') openai.api_key = config.get('key', 'XXX')
@@ -35,6 +35,9 @@ class ChatGLM():
"tool": [{"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:"}], "tool": [{"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:"}],
"code": [{"role": "system", "content": "你是一位智能AI助手你叫ChatGLM你连接着一台电脑但请注意不能联网。在使用Python解决任务时你可以运行代码并得到结果如果运行结果有错误你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件文件默认存储路径是{}".format(self.filePath)}]} "code": [{"role": "system", "content": "你是一位智能AI助手你叫ChatGLM你连接着一台电脑但请注意不能联网。在使用Python解决任务时你可以运行代码并得到结果如果运行结果有错误你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件文件默认存储路径是{}".format(self.filePath)}]}
def __repr__(self):
return 'ChatGLM'
def get_answer(self, question: str, wxid: str) -> str: def get_answer(self, question: str, wxid: str) -> str:
# wxid或者roomid,个人时为微信id群消息时为群id # wxid或者roomid,个人时为微信id群消息时为群id
if '#帮助' == question: if '#帮助' == question:

View File

@@ -6,7 +6,7 @@ from datetime import datetime
import openai import openai
class ChatGPT(): class ChatGPT:
def __init__(self, key: str, api: str, proxy: str, prompt: str) -> None: def __init__(self, key: str, api: str, proxy: str, prompt: str) -> None:
openai.api_key = key openai.api_key = key
@@ -17,6 +17,9 @@ class ChatGPT():
self.conversation_list = {} self.conversation_list = {}
self.system_content_msg = {"role": "system", "content": prompt} self.system_content_msg = {"role": "system", "content": prompt}
def __repr__(self):
return 'ChatGPT'
def get_answer(self, question: str, wxid: str) -> str: def get_answer(self, question: str, wxid: str) -> str:
# wxid或者roomid,个人时为微信id群消息时为群id # wxid或者roomid,个人时为微信id群消息时为群id
self.updateMessage(wxid, question, "user") self.updateMessage(wxid, question, "user")

View File

@@ -7,7 +7,7 @@ import requests
from random import randint from random import randint
class TigerBot(): class TigerBot:
def __init__(self, tbconf=None) -> None: def __init__(self, tbconf=None) -> None:
self.LOG = logging.getLogger(__file__) self.LOG = logging.getLogger(__file__)
self.tburl = "https://api.tigerbot.com/bot-service/ai_service/gpt" self.tburl = "https://api.tigerbot.com/bot-service/ai_service/gpt"
@@ -15,6 +15,9 @@ class TigerBot():
self.tbmodel = tbconf["model"] self.tbmodel = tbconf["model"]
self.fallback = ["", "快滚", "赶紧滚"] self.fallback = ["", "快滚", "赶紧滚"]
def __repr__(self):
return 'TigerBot'
def get_answer(self, msg: str, sender: str = None) -> str: def get_answer(self, msg: str, sender: str = None) -> str:
payload = { payload = {
"text": msg, "text": msg,

View File

@@ -3,7 +3,7 @@
from sparkdesk_web.core import SparkWeb from sparkdesk_web.core import SparkWeb
class XinghuoWeb(): class XinghuoWeb:
def __init__(self, xhconf=None) -> None: def __init__(self, xhconf=None) -> None:
self._sparkWeb = SparkWeb( self._sparkWeb = SparkWeb(
@@ -16,6 +16,9 @@ class XinghuoWeb():
if xhconf["prompt"]: if xhconf["prompt"]:
self._chat.chat(xhconf["prompt"]) self._chat.chat(xhconf["prompt"])
def __repr__(self):
return 'XinghuoWeb'
def get_answer(self, msg: str, sender: str = None) -> str: def get_answer(self, msg: str, sender: str = None) -> str:
answer = self._chat.chat(msg) answer = self._chat.chat(msg)
return answer return answer

View File

@@ -9,6 +9,7 @@ from wcferry import Wcf
from configuration import Config from configuration import Config
from func_report_reminder import ReportReminder from func_report_reminder import ReportReminder
from robot import Robot from robot import Robot
from constants import ChatType
def weather_report(robot: Robot) -> None: def weather_report(robot: Robot) -> None:
@@ -61,6 +62,6 @@ def main(chat_type: int):
if __name__ == "__main__": if __name__ == "__main__":
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument('-c', type=int, default=0, help='选择模型参数: 1, 2, 3, 4') parser.add_argument('-c', type=int, default=0, help=f'选择模型参数序号: {ChatType.help_hint()}')
args = parser.parse_args().c args = parser.parse_args().c
main(args) main(args)

View File

@@ -31,37 +31,39 @@ class Robot(Job):
self.wxid = self.wcf.get_self_wxid() self.wxid = self.wcf.get_self_wxid()
self.allContacts = self.getAllContacts() self.allContacts = self.getAllContacts()
if chat_type == ChatType.UnKnown.value: if ChatType.is_in_chat_types(chat_type):
if all(value is not None for value in self.config.TIGERBOT.values()): if chat_type == ChatType.TIGER_BOT.value and self.value_check(self.config.TIGERBOT.values()):
self.chat = TigerBot(self.config.TIGERBOT) self.chat = TigerBot(self.config.TIGERBOT)
elif all(value is not None for value in self.config.CHATGPT.values()): elif chat_type == ChatType.CHATGPT.value and self.value_check(self.config.CHATGPT.values()):
cgpt = self.config.CHATGPT cgpt = self.config.CHATGPT
self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt")) self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt"))
elif all(value is not None for value in self.config.XINGHUO_WEB.values()): elif chat_type == ChatType.XINGHUO_WEB.value and self.value_check(self.config.XINGHUO_WEB.values()):
self.chat = XinghuoWeb(self.config.XINGHUO_WEB) self.chat = XinghuoWeb(self.config.XINGHUO_WEB)
elif all(value is not None for value in self.config.CHATGLM.values()): elif chat_type == ChatType.CHATGLM.value and self.value_check(self.config.CHATGLM.values()):
self.chat = ChatGLM(self.config.CHATGLM) self.chat = ChatGLM(self.config.CHATGLM)
else: else:
self.LOG.warning('未配置模型') self.LOG.warning('未配置模型')
self.chat = None self.chat = None
else: else:
if chat_type == ChatType.TIGER_BOT.value and all( if self.value_check(self.config.TIGERBOT.values()):
value is not None for value in self.config.TIGERBOT.values()):
self.chat = TigerBot(self.config.TIGERBOT) self.chat = TigerBot(self.config.TIGERBOT)
elif chat_type == ChatType.CHATGPT.value and all( elif self.value_check(self.config.CHATGPT.values()):
value is not None for value in self.config.CHATGPT.values()):
cgpt = self.config.CHATGPT cgpt = self.config.CHATGPT
self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt")) self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt"))
elif chat_type == ChatType.XINGHUO_WEB.value and all( elif self.value_check(self.config.XINGHUO_WEB.values()):
value is not None for value in self.config.XINGHUO_WEB.values()):
self.chat = XinghuoWeb(self.config.XINGHUO_WEB) self.chat = XinghuoWeb(self.config.XINGHUO_WEB)
elif chat_type == ChatType.CHATGLM.value and all( elif self.value_check(self.config.CHATGLM.values()):
value is not None for value in self.config.CHATGLM.values()):
self.chat = ChatGLM(self.config.CHATGLM) self.chat = ChatGLM(self.config.CHATGLM)
else: else:
self.LOG.warning('未配置模型') self.LOG.warning('未配置模型')
self.chat = None self.chat = None
self.LOG.info(f'已选择: {self.chat}')
@staticmethod
def value_check(args: dict) -> bool:
return all(value is not None for key, value in args.items() if key != 'proxy')
def toAt(self, msg: WxMsg) -> bool: def toAt(self, msg: WxMsg) -> bool:
"""处理被 @ 消息 """处理被 @ 消息
:param msg: 微信消息结构 :param msg: 微信消息结构