feat: model name log, args help hint and the proxy of key does not participate in the judgment.
This commit is contained in:
12
constants.py
12
constants.py
@@ -8,3 +8,15 @@ class ChatType(IntEnum):
|
|||||||
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():
|
||||||
|
return str({member.value: member.name for member in ChatType}).replace('{', '').replace('}', '')
|
||||||
|
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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")
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
3
main.py
3
main.py
@@ -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)
|
||||||
|
|||||||
38
robot.py
38
robot.py
@@ -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: 微信消息结构
|
||||||
@@ -131,22 +133,22 @@ class Robot(Job):
|
|||||||
if msg.roomid not in self.config.GROUPS: # 不在配置的响应的群列表里,忽略
|
if msg.roomid not in self.config.GROUPS: # 不在配置的响应的群列表里,忽略
|
||||||
return
|
return
|
||||||
|
|
||||||
if msg.is_at(self.wxid): # 被@
|
if msg.is_at(self.wxid): # 被@
|
||||||
self.toAt(msg)
|
self.toAt(msg)
|
||||||
|
|
||||||
else: # 其他消息
|
else: # 其他消息
|
||||||
self.toChengyu(msg)
|
self.toChengyu(msg)
|
||||||
|
|
||||||
return # 处理完群聊信息,后面就不需要处理了
|
return # 处理完群聊信息,后面就不需要处理了
|
||||||
|
|
||||||
# 非群聊信息,按消息类型进行处理
|
# 非群聊信息,按消息类型进行处理
|
||||||
if msg.type == 37: # 好友请求
|
if msg.type == 37: # 好友请求
|
||||||
self.autoAcceptFriendRequest(msg)
|
self.autoAcceptFriendRequest(msg)
|
||||||
|
|
||||||
elif msg.type == 10000: # 系统信息
|
elif msg.type == 10000: # 系统信息
|
||||||
self.sayHiToNewFriend(msg)
|
self.sayHiToNewFriend(msg)
|
||||||
|
|
||||||
elif msg.type == 0x01: # 文本消息
|
elif msg.type == 0x01: # 文本消息
|
||||||
# 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
|
# 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
|
||||||
if msg.from_self():
|
if msg.from_self():
|
||||||
if msg.content == "^更新$":
|
if msg.content == "^更新$":
|
||||||
@@ -213,7 +215,7 @@ class Robot(Job):
|
|||||||
格式: {"wxid": "NickName"}
|
格式: {"wxid": "NickName"}
|
||||||
"""
|
"""
|
||||||
contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
|
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:
|
def keepRunningAndBlockProcess(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user