From 81bb80c63850ddeee7b4e1014e9fac262168416e Mon Sep 17 00:00:00 2001 From: xszyou Date: Mon, 22 May 2023 13:58:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8DChatGPT=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9B=B8=E5=90=8C=E4=BC=9A=E8=AF=9D=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复ChatGPT无法使用相同会话的bug --- ai_module/nlp_gpt.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ai_module/nlp_gpt.py b/ai_module/nlp_gpt.py index bce81ba..b5b79e5 100644 --- a/ai_module/nlp_gpt.py +++ b/ai_module/nlp_gpt.py @@ -20,8 +20,11 @@ def question(cont): try: chatbot = Chatbot(config={ "access_token": cfg.key_gpt_access_token, - "conversation_id": cfg.key_gpt_conversation_id - }) + "paid": False, + "collect_analytics": True, + "conversation_id":cfg.key_gpt_conversation_id + },conversation_id=cfg.key_gpt_conversation_id, + parent_id=None) prompt = text + ' 现在想咨询的问题是:'+cont response = "" From 3373a9f56f18b3b6b453644b7ccfa45321eadf15 Mon Sep 17 00:00:00 2001 From: xszyou Date: Tue, 23 May 2023 00:49:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、修复feifei重复创建导致商端口冲突的bug; 2、更换pygame初始化方式; 3、修复chatgpt(彩蛋)多论对话逻辑; 4、修复js "s"未定义bug。 --- ai_module/nlp_gpt.py | 32 ++------ core/fay_core.py | 108 +++++++++++++------------- gui/flask_server.py | 6 +- gui/static/js/element.js | 3 +- python_connector_demo/remote_audio.py | 2 +- 5 files changed, 65 insertions(+), 86 deletions(-) diff --git a/ai_module/nlp_gpt.py b/ai_module/nlp_gpt.py index b5b79e5..7c5fbb3 100644 --- a/ai_module/nlp_gpt.py +++ b/ai_module/nlp_gpt.py @@ -3,34 +3,18 @@ from core.content_db import Content_Db from utils import config_util as cfg def question(cont): - contentdb = Content_Db() - list = contentdb.get_list('all','desc',5) - text = '' - if len(list) > 0: - relist = [] - i = len(list)-1 - text = '以下是我和机器人的历史对话:' - while i >= 0: - if list[i][0] == 'fay': - text = text + ' 机器人:' + list[i][2] - else: - text = text + ' 我:' + list[i][2] - i -= 1 - try: chatbot = Chatbot(config={ - "access_token": cfg.key_gpt_access_token, - "paid": False, - "collect_analytics": True, - "conversation_id":cfg.key_gpt_conversation_id - },conversation_id=cfg.key_gpt_conversation_id, - parent_id=None) + "access_token": cfg.key_gpt_access_token, + "paid": False, + "collect_analytics": True, + "conversation_id":cfg.key_gpt_conversation_id + },conversation_id=cfg.key_gpt_conversation_id, + parent_id=None) - prompt = text + ' 现在想咨询的问题是:'+cont + prompt = cont response = "" - for data in chatbot.ask( - prompt - ): + for data in chatbot.ask(prompt): response = data["message"] return response except: diff --git a/core/fay_core.py b/core/fay_core.py index 2f55ee3..27935c9 100644 --- a/core/fay_core.py +++ b/core/fay_core.py @@ -29,9 +29,60 @@ from core.content_db import Content_Db from datetime import datetime from ai_module import nlp_rasa from ai_module import nlp_gpt + +#文本消息处理 +def send_for_answer(msg,sendto): + contentdb = Content_Db() + contentdb.add_content('member','send',msg) + text = '' + textlist = [] + try: + #wsa_server.get_web_instance().add_cmd({"panelMsg": "思考中..."}) + util.log(1, '自然语言处理...') + tm = time.time() + cfg.load_config() + if sendto == 2: + text = nlp_gpt.question(msg) + else: + if cfg.key_chat_module == 'xfaiui': + text = xf_aiui.question(msg) + elif cfg.key_chat_module == 'yuan': + text = yuan_1_0.question(msg) + elif cfg.key_chat_module == 'chatgpt': + text = chatgpt.question(msg) + elif cfg.key_chat_module == 'rasa': + textlist = nlp_rasa.question(msg) + text = textlist[0]['text'] + + + else: + raise RuntimeError('讯飞key、yuan key、chatgpt key都没有配置!') + util.log(1, '自然语言处理完成. 耗时: {} ms'.format(math.floor((time.time() - tm) * 1000))) + if text == '哎呀,你这么说我也不懂,详细点呗' or text == '': + util.log(1, '[!] 自然语言无语了!') + text = '哎呀,你这么说我也不懂,详细点呗' + # wsa_server.get_web_instance().add_cmd({"panelMsg": ""}) + + except BaseException as e: + print(e) + util.log(1, '自然语言处理错误!') + text = '哎呀,你这么说我也不懂,详细点呗' + # wsa_server.get_web_instance().add_cmd({"panelMsg": ""}) + + now = datetime.now() + timetext = str(now.strftime("%Y-%m-%d %H:%M:%S")) + contentdb.add_content('fay','send',text) + wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"fay","content":text}}) + if len(textlist) > 1: + i = 1 + while i < len(textlist): + contentdb.add_content('fay','send',textlist[i]['text']) + wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"fay","content":textlist[i]['text']}}) + i+= 1 + return text class FeiFei: def __init__(self): - pygame.init() + pygame.mixer.init() self.q_msg = '你叫什么名字?' self.a_msg = 'hi,我叫菲菲,英文名是fay' self.mood = 0.0 # 情绪值 @@ -591,60 +642,7 @@ class FeiFei: self.last_interact_time = time.time() self.speaking = False - def send_for_answer(self,msg,sendto): - contentdb = Content_Db() - contentdb.add_content('member','send',msg) - answer = self.__get_answer('send', msg) - - text = '' - textlist = [] - if answer is None: - try: - #wsa_server.get_web_instance().add_cmd({"panelMsg": "思考中..."}) - util.log(1, '自然语言处理...') - tm = time.time() - cfg.load_config() - if sendto == 2: - text = nlp_gpt.question(msg) - else: - if cfg.key_chat_module == 'xfaiui': - text = xf_aiui.question(msg) - elif cfg.key_chat_module == 'yuan': - text = yuan_1_0.question(msg) - elif cfg.key_chat_module == 'chatgpt': - text = chatgpt.question(msg) - elif cfg.key_chat_module == 'rasa': - textlist = nlp_rasa.question(msg) - text = textlist[0]['text'] - - - else: - raise RuntimeError('讯飞key、yuan key、chatgpt key都没有配置!') - util.log(1, '自然语言处理完成. 耗时: {} ms'.format(math.floor((time.time() - tm) * 1000))) - if text == '哎呀,你这么说我也不懂,详细点呗' or text == '': - util.log(1, '[!] 自然语言无语了!') - text = '哎呀,你这么说我也不懂,详细点呗' - # wsa_server.get_web_instance().add_cmd({"panelMsg": ""}) - - except BaseException as e: - print(e) - util.log(1, '自然语言处理错误!') - text = '哎呀,你这么说我也不懂,详细点呗' - # wsa_server.get_web_instance().add_cmd({"panelMsg": ""}) - - elif answer != 'NO_ANSWER': - text = answer - now = datetime.now() - timetext = str(now.strftime("%Y-%m-%d %H:%M:%S")) - contentdb.add_content('fay','send',text) - wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"fay","content":text}}) - if len(textlist) > 1: - i = 1 - while i < len(textlist): - contentdb.add_content('fay','send',textlist[i]['text']) - wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"fay","content":textlist[i]['text']}}) - i+= 1 - return text + diff --git a/gui/flask_server.py b/gui/flask_server.py index 2323bf4..3768d0c 100644 --- a/gui/flask_server.py +++ b/gui/flask_server.py @@ -12,7 +12,7 @@ from gevent import pywsgi from scheduler.thread_manager import MyThread from utils import config_util from core import wsa_server -from core.fay_core import FeiFei +from core import fay_core from core.content_db import Content_Db __app = Flask(__name__) @@ -75,10 +75,8 @@ def api_stop_live(): @__app.route('/api/send', methods=['post']) def api_send(): data = request.values.get('data') - print(data) info = json.loads(data) - feiFei = FeiFei() - text = feiFei.send_for_answer(info['msg'],info['sendto']) + text = fay_core.send_for_answer(info['msg'],info['sendto']) return '{"result":"successful","msg":"'+text+'"}' @__app.route('/api/get-msg', methods=['post']) diff --git a/gui/static/js/element.js b/gui/static/js/element.js index e6e85ea..11be32a 100644 --- a/gui/static/js/element.js +++ b/gui/static/js/element.js @@ -13615,5 +13615,4 @@ Result: Pp } }]).default -}); -s \ No newline at end of file +}); \ No newline at end of file diff --git a/python_connector_demo/remote_audio.py b/python_connector_demo/remote_audio.py index af62a79..77ae694 100644 --- a/python_connector_demo/remote_audio.py +++ b/python_connector_demo/remote_audio.py @@ -48,7 +48,7 @@ def receive_audio(client): if __name__ == "__main__": client = socket.socket() client.connect(("192.168.1.101", 10001)) - pygame.init() + pygame.mixer.init() thread_manager.MyThread(target=send_audio, args=(client,)).start() thread_manager.MyThread(target=receive_audio, args=(client,)).start()