feat: model name log, args help hint and the proxy of key does not participate in the judgment.

This commit is contained in:
weiensong
2023-11-17 11:13:25 +08:00
parent aaacbf29ea
commit e247e7832d
7 changed files with 50 additions and 23 deletions

View File

@@ -8,3 +8,15 @@ class ChatType(IntEnum):
CHATGPT = 2 # ChatGPT
XINGHUO_WEB = 3 # 讯飞星火
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():
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()
class ChatGLM():
class ChatGLM:
def __init__(self, config={}, wcf: Optional[Wcf] = None, max_retry=5) -> None:
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:"}],
"code": [{"role": "system", "content": "你是一位智能AI助手你叫ChatGLM你连接着一台电脑但请注意不能联网。在使用Python解决任务时你可以运行代码并得到结果如果运行结果有错误你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件文件默认存储路径是{}".format(self.filePath)}]}
def __repr__(self):
return 'ChatGLM'
def get_answer(self, question: str, wxid: str) -> str:
# wxid或者roomid,个人时为微信id群消息时为群id
if '#帮助' == question:

View File

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

View File

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

View File

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

View File

@@ -9,6 +9,7 @@ from wcferry import Wcf
from configuration import Config
from func_report_reminder import ReportReminder
from robot import Robot
from constants import ChatType
def weather_report(robot: Robot) -> None:
@@ -61,6 +62,6 @@ def main(chat_type: int):
if __name__ == "__main__":
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
main(args)

View File

@@ -31,37 +31,39 @@ class Robot(Job):
self.wxid = self.wcf.get_self_wxid()
self.allContacts = self.getAllContacts()
if chat_type == ChatType.UnKnown.value:
if all(value is not None for value in self.config.TIGERBOT.values()):
if ChatType.is_in_chat_types(chat_type):
if chat_type == ChatType.TIGER_BOT.value and self.value_check(self.config.TIGERBOT.values()):
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
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)
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)
else:
self.LOG.warning('未配置模型')
self.chat = None
else:
if chat_type == ChatType.TIGER_BOT.value and all(
value is not None for value in self.config.TIGERBOT.values()):
if self.value_check(self.config.TIGERBOT.values()):
self.chat = TigerBot(self.config.TIGERBOT)
elif chat_type == ChatType.CHATGPT.value and all(
value is not None for value in self.config.CHATGPT.values()):
elif self.value_check(self.config.CHATGPT.values()):
cgpt = self.config.CHATGPT
self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt"))
elif chat_type == ChatType.XINGHUO_WEB.value and all(
value is not None for value in self.config.XINGHUO_WEB.values()):
elif self.value_check(self.config.XINGHUO_WEB.values()):
self.chat = XinghuoWeb(self.config.XINGHUO_WEB)
elif chat_type == ChatType.CHATGLM.value and all(
value is not None for value in self.config.CHATGLM.values()):
elif self.value_check(self.config.CHATGLM.values()):
self.chat = ChatGLM(self.config.CHATGLM)
else:
self.LOG.warning('未配置模型')
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:
"""处理被 @ 消息
:param msg: 微信消息结构
@@ -131,22 +133,22 @@ class Robot(Job):
if msg.roomid not in self.config.GROUPS: # 不在配置的响应的群列表里,忽略
return
if msg.is_at(self.wxid): # 被@
if msg.is_at(self.wxid): # 被@
self.toAt(msg)
else: # 其他消息
else: # 其他消息
self.toChengyu(msg)
return # 处理完群聊信息,后面就不需要处理了
# 非群聊信息,按消息类型进行处理
if msg.type == 37: # 好友请求
if msg.type == 37: # 好友请求
self.autoAcceptFriendRequest(msg)
elif msg.type == 10000: # 系统信息
self.sayHiToNewFriend(msg)
elif msg.type == 0x01: # 文本消息
elif msg.type == 0x01: # 文本消息
# 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
if msg.from_self():
if msg.content == "^更新$":
@@ -213,7 +215,7 @@ class Robot(Job):
格式: {"wxid": "NickName"}
"""
contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
return {contact["UserName"]: contact["NickName"]for contact in contacts}
return {contact["UserName"]: contact["NickName"] for contact in contacts}
def keepRunningAndBlockProcess(self) -> None:
"""