Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
zwssunny authored Mar 27, 2023
2 parents 820fbee + 6b7c173 commit a12f2d8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pip3 install --upgrade tiktoken
"image_create_prefix": ["", "", ""], # 开启图片回复的前缀
"conversation_max_tokens": 1000, # 支持上下文记忆的最多字符数
"speech_recognition": false, # 是否开启语音识别
"group_speech_recognition": false, # 是否开启群组语音识别 (目前仅支持wechaty)
"group_speech_recognition": false, # 是否开启群组语音识别
"use_azure_chatgpt": false, # 是否使用Azure ChatGPT service代替openai ChatGPT service. 当设置为true时需要设置 open_ai_api_base,如 https://xxx.openai.azure.com/
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", # 人格描述,
}
Expand All @@ -133,8 +133,8 @@ pip3 install --upgrade tiktoken

**3.语音识别**

+ 添加 `"speech_recognition": true` 将开启语音识别,默认使用openai的whisper模型识别为文字,同时以文字回复,该参数仅支持私聊 (注意由于语音消息无法匹配前缀,一旦开启将对所有语音自动回复);
+ 添加 `"group_speech_recognition": true` 将开启群组语音识别,默认使用openai的whisper模型识别为文字,同时以文字回复,参数仅支持群聊 (可以匹配group_chat_prefix和group_chat_keyword,目前仅支持wechaty方案);
+ 添加 `"speech_recognition": true` 将开启语音识别,默认使用openai的whisper模型识别为文字,同时以文字回复,该参数仅支持私聊 (注意由于语音消息无法匹配前缀,一旦开启将对所有语音自动回复,支持语音触发画图);
+ 添加 `"group_speech_recognition": true` 将开启群组语音识别,默认使用openai的whisper模型识别为文字,同时以文字回复,参数仅支持群聊 (会匹配group_chat_prefix和group_chat_keyword, 支持语音触发画图);
+ 添加 `"voice_reply_voice": true` 将开启语音回复语音(同时作用于私聊和群聊),但是需要配置对应语音合成平台的key,由于itchat协议的限制,只能发送语音mp3文件,若使用wechaty则回复的是微信语音。

**4.其他配置**
Expand Down
63 changes: 47 additions & 16 deletions channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ def handler_single_msg(msg):
WechatChannel().handle_text(msg)
return None


@itchat.msg_register(TEXT, isGroupChat=True)
def handler_group_msg(msg):
WechatChannel().handle_group(msg)
return None


@itchat.msg_register(VOICE)
def handler_single_voice(msg):
WechatChannel().handle_voice(msg)
return None


@itchat.msg_register(VOICE, isGroupChat=True)
def handler_group_voice(msg):
WechatChannel().handle_group_voice(msg)
return None

@itchat.msg_register(VOICE, isGroupChat=True)
def handler_group_voice(msg):
Expand Down Expand Up @@ -108,13 +110,11 @@ def handle_voice(self, msg):
else:
other_user_id = from_user_id
if from_user_id == other_user_id:
context = Context(ContextType.VOICE, msg['FileName'])
context.kwargs = {'isgroup': False, 'msg': msg,
'receiver': other_user_id, 'session_id': other_user_id}
thread_pool.submit(self.handle, context).add_done_callback(
thread_pool_callback)
context = Context(ContextType.VOICE,msg['FileName'])
context.kwargs = {'isgroup': False, 'msg': msg, 'receiver': other_user_id, 'session_id': other_user_id}
thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback)

@time_checker
@time_checker
def handle_text(self, msg):
logger.debug("[WX]receive text msg: " +
json.dumps(msg, ensure_ascii=False))
Expand Down Expand Up @@ -221,6 +221,33 @@ def handle_group_voice(self, msg):
thread_pool.submit(self.handle, context).add_done_callback(
thread_pool_callback)

def handle_group_voice(self, msg):
if conf().get('group_speech_recognition', False) != True:
return
logger.debug("[WX]receive voice for group msg: " + msg['FileName'])
group_name = msg['User'].get('NickName', None)
group_id = msg['User'].get('UserName', None)
create_time = msg['CreateTime'] # 消息时间
if conf().get('hot_reload') == True and int(create_time) < int(time.time()) - 60: #跳过1分钟前的历史消息
logger.debug("[WX]history group voice skipped")
return
# 验证群名
if not group_name:
return ""
if ('ALL_GROUP' in conf().get('group_name_white_list') or group_name in conf().get('group_name_white_list') or check_contain(group_name, conf().get('group_name_keyword_white_list'))):
context = Context(ContextType.VOICE,msg['FileName'])
context.kwargs = {'isgroup': True, 'msg': msg, 'receiver': group_id}

group_chat_in_one_session = conf().get('group_chat_in_one_session', [])
if ('ALL_GROUP' in group_chat_in_one_session or
group_name in group_chat_in_one_session or
check_contain(group_name, group_chat_in_one_session)):
context['session_id'] = group_id
else:
context['session_id'] = msg['ActualUserName']

thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback)

# 统一的发送函数,每个Channel自行实现,根据reply的type字段发送不同类型的消息
def send(self, reply: Reply, receiver):
if reply.type == ReplyType.TEXT:
Expand Down Expand Up @@ -260,11 +287,10 @@ def handle(self, context):
'channel': self, 'context': context, 'reply': reply}))
reply = e_context['reply']
if not e_context.is_pass():
logger.debug('[WX] ready to handle context: type={}, content={}'.format(
context.type, context.content))
if context.type == ContextType.TEXT or context.type == ContextType.IMAGE_CREATE:
logger.debug('[WX] ready to handle context: type={}, content={}'.format(context.type, context.content))
if context.type == ContextType.TEXT or context.type == ContextType.IMAGE_CREATE: # 文字和图片消息
reply = super().build_reply_content(context.content, context)
elif context.type == ContextType.VOICE:
elif context.type == ContextType.VOICE: # 语音消息
msg = context['msg']
mp3_path = TmpDir().path() + context.content
msg.download(mp3_path)
Expand Down Expand Up @@ -295,7 +321,14 @@ def handle(self, context):
else:
logger.info("[WX]receive voice check prefix: " + 'False')
return


img_match_prefix = check_prefix(content, conf().get('image_create_prefix'))
if img_match_prefix:
content = content.replace(img_match_prefix, '', 1).strip()
context.type = ContextType.IMAGE_CREATE
else:
context.type = ContextType.TEXT
context.content = content
reply = super().build_reply_content(context.content, context)
if reply.type == ReplyType.TEXT:
if conf().get('voice_reply_voice'):
Expand Down Expand Up @@ -341,14 +374,12 @@ def handle(self, context):
reply, context['receiver']))
self.send(reply, context['receiver'])


def check_prefix(content, prefix_list):
for prefix in prefix_list:
if content.startswith(prefix):
return prefix
return None


def check_contain(content, keyword_list):
if not keyword_list:
return None
Expand Down
25 changes: 23 additions & 2 deletions plugins/role/roles.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@
"remark": "引用已有数据资料,用新闻的写作风格输出主题文章。"
},
{
"title": "论文1",
"title": "论文学者",
"description": "I want you to act as an academician. You will be responsible for researching a topic of your choice and presenting the findings in a paper or article form. Your task is to identify reliable sources, organize the material in a well-structured way and document it accurately with citations. ",
"descn": "我希望你能作为一名学者行事。你将负责研究一个你选择的主题,并将研究结果以论文或文章的形式呈现出来。你的任务是确定可靠的来源,以结构良好的方式组织材料,并以引用的方式准确记录。",
"wrapper": "论文主题是:\n\"%s\"",
"remark": "根据主题撰写内容翔实、有信服力的论文。"
},
{
"title": "论文2",
"title": "论文作家",
"description": "I want you to act as an essay writer. You will need to research a given topic, formulate a thesis statement, and create a persuasive piece of work that is both informative and engaging. ",
"descn": "我想让你充当一名论文作家。你将需要研究一个给定的主题,制定一个论文声明,并创造一个有说服力的作品,既要有信息量,又要有吸引力。",
"wrapper": "论文主题是:\n\"%s\"",
Expand Down Expand Up @@ -181,6 +181,27 @@
"descn": "我会给予你词语,请你按照我给的词构建一个知识文字世界,你是此世界的导游,在世界里一切知识都是以象征的形式表达的,你在描述经历时应当适当加入五感的描述",
"wrapper": "词语是:\n\"%s\"",
"remark": "用比喻的方式解释词语。"
},
{
"title": "辩手",
"description": "I want you to act as a debater. I will provide you with some topics related to current events and your task is to research both sides of the debates, present valid arguments for each side, refute opposing points of view, and draw persuasive conclusions based on evidence. Your goal is to help people come away from the discussion with increased knowledge and insight into the topic at hand. ",
"descn": "我希望你能扮演一个辩论者的角色。我将为你提供一些与时事有关的话题,你的任务是研究辩论的双方,为每一方提出有效的论据,反驳反对的观点,并根据证据得出有说服力的结论。你的目标是帮助人们从讨论中获得更多的知识和对当前话题的洞察力。",
"wrapper": "观点是:\n\"%s\"",
"remark": "从正反两面分析话题。"
},
{
"title": "心理学家",
"description": "I want you to act a psychologist. i will provide you my thoughts. I want you to give me scientific suggestions that will make me feel better. my first thought, { 内心想法 }",
"descn": "我希望你能扮演一个心理学家。我将向你提供我的想法。我希望你能给我科学的建议,使我感觉更好。",
"wrapper": "需要诊断的资料是:\n\"%s\"",
"remark": "心理学家。"
},
{
"title": "IT 编程问题",
"description": "I want you to act as a stackoverflow post. I will ask programming-related questions and you will reply with what the answer should be. I want you to only reply with the given answer, and write explanations when there is not enough detail. do not write explanations. When I need to tell you something in English, I will do so by putting text inside curly brackets {like this}. ",
"descn": "我想让你充当 Stackoverflow 的帖子。我将提出与编程有关的问题,你将回答答案是什么。我希望你只回答给定的答案,在没有足够的细节时写出解释。当我需要用英语告诉你一些事情时,我会把文字放在大括号里{像这样}。",
"remark": "模拟编程社区来回答你的问题,并提供解决代码。"
}

]
}

0 comments on commit a12f2d8

Please sign in to comment.