diff --git a/ai_module/nlp_gpt.py b/ai_module/nlp_gpt.py new file mode 100644 index 0000000..bce81ba --- /dev/null +++ b/ai_module/nlp_gpt.py @@ -0,0 +1,34 @@ +from revChatGPT.V1 import Chatbot +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, + "conversation_id": cfg.key_gpt_conversation_id + }) + + prompt = text + ' 现在想咨询的问题是:'+cont + response = "" + for data in chatbot.ask( + prompt + ): + response = data["message"] + return response + except: + return 'gpt当前繁忙,请稍后重试' diff --git a/core/fay_core.py b/core/fay_core.py index 2bcffec..2f55ee3 100644 --- a/core/fay_core.py +++ b/core/fay_core.py @@ -28,9 +28,10 @@ from utils import config_util as cfg from core.content_db import Content_Db from datetime import datetime from ai_module import nlp_rasa +from ai_module import nlp_gpt class FeiFei: def __init__(self): - pygame.mixer.init() + pygame.init() self.q_msg = '你叫什么名字?' self.a_msg = 'hi,我叫菲菲,英文名是fay' self.mood = 0.0 # 情绪值 @@ -516,27 +517,27 @@ class FeiFei: audio_length = eyed3.load(file_url).info.time_secs #mp3音频长度 # with wave.open(file_url, 'rb') as wav_file: #wav音频长度 # audio_length = wav_file.getnframes() / float(wav_file.getframerate()) - if audio_length <= config_util.config["interact"]["maxInteractTime"] or say_type == "script": - if config_util.config["interact"]["playSound"]: # 展板播放 - self.__play_sound(file_url) - else:#发送音频给ue和socket - content = {'Topic': 'Unreal', 'Data': {'Key': 'audio', 'Value': os.path.abspath(file_url), 'Time': audio_length, 'Type': say_type}} - wsa_server.get_instance().add_cmd(content) - if self.deviceConnect is not None: - try: - self.deviceConnect.send(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08') # 发送音频开始标志,同时也检查设备是否在线 - wavfile = open(os.path.abspath(file_url),'rb') + # if audio_length <= config_util.config["interact"]["maxInteractTime"] or say_type == "script": + if config_util.config["interact"]["playSound"]: # 展板播放 + self.__play_sound(file_url) + else:#发送音频给ue和socket + content = {'Topic': 'Unreal', 'Data': {'Key': 'audio', 'Value': os.path.abspath(file_url), 'Time': audio_length, 'Type': say_type}} + wsa_server.get_instance().add_cmd(content) + if self.deviceConnect is not None: + try: + self.deviceConnect.send(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08') # 发送音频开始标志,同时也检查设备是否在线 + wavfile = open(os.path.abspath(file_url),'rb') + data = wavfile.read(1024) + total = 0 + while data: + total += len(data) + self.deviceConnect.send(data) data = wavfile.read(1024) - total = 0 - while data: - total += len(data) - self.deviceConnect.send(data) - data = wavfile.read(1024) - time.sleep(0.001) - self.deviceConnect.send(b'\x08\x07\x06\x05\x04\x03\x02\x01\x00')# 发送音频结束标志 - util.log(1, "远程音频发送完成:{}".format(total)) - except socket.error as serr: - util.log(1,"远程音频输入输出设备已经断开:{}".format(serr)) + time.sleep(0.001) + self.deviceConnect.send(b'\x08\x07\x06\x05\x04\x03\x02\x01\x00')# 发送音频结束标志 + util.log(1, "远程音频发送完成:{}".format(total)) + except socket.error as serr: + util.log(1,"远程音频输入输出设备已经断开:{}".format(serr)) @@ -590,7 +591,7 @@ class FeiFei: self.last_interact_time = time.time() self.speaking = False - def send_for_answer(self,msg): + def send_for_answer(self,msg,sendto): contentdb = Content_Db() contentdb.add_content('member','send',msg) answer = self.__get_answer('send', msg) @@ -603,24 +604,27 @@ class FeiFei: util.log(1, '自然语言处理...') tm = time.time() cfg.load_config() - 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'] + 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": ""}) + 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) diff --git a/gui/flask_server.py b/gui/flask_server.py index 5d74113..2323bf4 100644 --- a/gui/flask_server.py +++ b/gui/flask_server.py @@ -78,7 +78,7 @@ def api_send(): print(data) info = json.loads(data) feiFei = FeiFei() - text = feiFei.send_for_answer(info['msg']) + text = feiFei.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/index.js b/gui/static/js/index.js index d19a645..140c6ad 100644 --- a/gui/static/js/index.js +++ b/gui/static/js/index.js @@ -82,8 +82,8 @@ new Vue({ methods: { // 回车和空格键提交右侧信息 handkeyCode(e) { - if(e.keyCode === 13){ - this.send() + if(e.keyCode === 13 && e.keyCode === 18){ + this.send(1) } }, handleTabsEdit(targetName, action) { @@ -452,7 +452,7 @@ new Vue({ type: 'success' }); }, - send() { + send(sendto) { let _this = this; let text = _this.send_msg; if (!text) { @@ -474,7 +474,8 @@ new Vue({ _this.send_msg = '' let url = "http://127.0.0.1:5000/api/send"; let send_data = { - "msg": text + "msg": text, + "sendto" : sendto }; let xhr = new XMLHttpRequest() diff --git a/gui/templates/index.html b/gui/templates/index.html index e2e4e29..369ca7b 100644 --- a/gui/templates/index.html +++ b/gui/templates/index.html @@ -333,7 +333,10 @@
- + +
diff --git a/requirements.txt b/requirements.txt index 335a53b..cdf5177 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,4 +18,5 @@ simhash pytz gevent~=22.10.1 edge_tts~=6.1.3 -eyed3 \ No newline at end of file +eyed3 +#revChatGPT 删除注释打开彩蛋 \ No newline at end of file diff --git a/system.conf b/system.conf index f5c067b..c1b7194 100644 --- a/system.conf +++ b/system.conf @@ -18,8 +18,8 @@ ms_tts_key= ms_tts_region= # 讯飞 情绪分析 服务密钥 https://www.xfyun.cn/service/emotion-analysis/ -xf_ltp_app_id= -xf_ltp_api_key= +xf_ltp_app_id=604404c8 +xf_ltp_api_key=78d2db9a83cda0b355c76ea791bc74da #NLP四选一:xfaiui、yuan、chatgpt、rasa(需启动chatglm及rasa,https://m.bilibili.com/video/BV1D14y1f7pr) chat_module=xfaiui @@ -37,3 +37,7 @@ chatgpt_api_key= #ngrok内网穿透id,远程设备可以通过互联网连接Fay(非必须)http://ngrok.cc ngrok_cc_id= + +#revChatGPT对接(非必须,https://chat.openai.com登录后访问https://chat.openai.com/api/auth/session获取) +gpt_access_token= +gpt_conversation_id= \ No newline at end of file diff --git a/utils/config_util.py b/utils/config_util.py index b52d37b..797d1f8 100644 --- a/utils/config_util.py +++ b/utils/config_util.py @@ -21,6 +21,8 @@ key_yuan_1_0_account = None key_yuan_1_0_phone = None key_chatgpt_api_key = None key_chat_module = None +key_gpt_access_token = None +key_gpt_conversation_id = None ASR_mode = None local_asr_ip = None @@ -43,6 +45,8 @@ def load_config(): global key_yuan_1_0_phone global key_chatgpt_api_key global key_chat_module + global key_gpt_access_token + global key_gpt_conversation_id global ASR_mode global local_asr_ip @@ -64,6 +68,8 @@ def load_config(): key_yuan_1_0_phone = system_config.get('key', 'yuan_1_0_phone') key_chatgpt_api_key = system_config.get('key', 'chatgpt_api_key') key_chat_module = system_config.get('key', 'chat_module') + key_gpt_access_token = system_config.get('key', 'gpt_access_token') + key_gpt_conversation_id = system_config.get('key', 'gpt_conversation_id') ASR_mode = system_config.get('key', 'ASR_mode') local_asr_ip = system_config.get('key', 'local_asr_ip')