Merge branch 'TheRamU:main' into main
This commit is contained in:
		
						commit
						ba9a1f65e6
					
				| @ -3,31 +3,18 @@ from core.content_db import Content_Db | |||||||
| from utils import config_util as cfg | from utils import config_util as cfg | ||||||
| 
 | 
 | ||||||
| def question(cont): | 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: |     try: | ||||||
|         chatbot = Chatbot(config={ |         chatbot = Chatbot(config={ | ||||||
|             "access_token": cfg.key_gpt_access_token, |             "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 | ||||||
|         }) |             },conversation_id=cfg.key_gpt_conversation_id, | ||||||
|  |             parent_id=None) | ||||||
| 
 | 
 | ||||||
|         prompt = text + ' 现在想咨询的问题是:'+cont |         prompt = cont | ||||||
|         response = "" |         response = "" | ||||||
|         for data in chatbot.ask( |         for data in chatbot.ask(prompt): | ||||||
|         prompt |  | ||||||
|         ): |  | ||||||
|             response = data["message"] |             response = data["message"] | ||||||
|         return response |         return response | ||||||
|     except: |     except: | ||||||
|  | |||||||
							
								
								
									
										106
									
								
								core/fay_core.py
									
									
									
									
									
								
							
							
						
						
									
										106
									
								
								core/fay_core.py
									
									
									
									
									
								
							| @ -29,9 +29,60 @@ from core.content_db import Content_Db | |||||||
| from datetime import datetime | from datetime import datetime | ||||||
| from ai_module import nlp_rasa | from ai_module import nlp_rasa | ||||||
| from ai_module import nlp_gpt | 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: | class FeiFei: | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         pygame.init() |         pygame.mixer.init() | ||||||
|         self.q_msg = '你叫什么名字?' |         self.q_msg = '你叫什么名字?' | ||||||
|         self.a_msg = 'hi,我叫菲菲,英文名是fay' |         self.a_msg = 'hi,我叫菲菲,英文名是fay' | ||||||
|         self.mood = 0.0  # 情绪值 |         self.mood = 0.0  # 情绪值 | ||||||
| @ -592,60 +643,7 @@ class FeiFei: | |||||||
|             self.last_interact_time = time.time() |             self.last_interact_time = time.time() | ||||||
|             self.speaking = False |             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 |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ from gevent import pywsgi | |||||||
| from scheduler.thread_manager import MyThread | from scheduler.thread_manager import MyThread | ||||||
| from utils import config_util | from utils import config_util | ||||||
| from core import wsa_server | from core import wsa_server | ||||||
| from core.fay_core import FeiFei | from core import fay_core | ||||||
| from core.content_db import Content_Db | from core.content_db import Content_Db | ||||||
| 
 | 
 | ||||||
| __app = Flask(__name__) | __app = Flask(__name__) | ||||||
| @ -75,10 +75,8 @@ def api_stop_live(): | |||||||
| @__app.route('/api/send', methods=['post']) | @__app.route('/api/send', methods=['post']) | ||||||
| def api_send(): | def api_send(): | ||||||
|     data = request.values.get('data') |     data = request.values.get('data') | ||||||
|     print(data) |  | ||||||
|     info = json.loads(data) |     info = json.loads(data) | ||||||
|     feiFei = FeiFei() |     text = fay_core.send_for_answer(info['msg'],info['sendto']) | ||||||
|     text = feiFei.send_for_answer(info['msg'],info['sendto']) |  | ||||||
|     return '{"result":"successful","msg":"'+text+'"}' |     return '{"result":"successful","msg":"'+text+'"}' | ||||||
| 
 | 
 | ||||||
| @__app.route('/api/get-msg', methods=['post']) | @__app.route('/api/get-msg', methods=['post']) | ||||||
|  | |||||||
| @ -13616,4 +13616,3 @@ | |||||||
|         } |         } | ||||||
|     }]).default |     }]).default | ||||||
| }); | }); | ||||||
| s |  | ||||||
| @ -48,7 +48,7 @@ def receive_audio(client): | |||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     client = socket.socket() |     client = socket.socket() | ||||||
|     client.connect(("192.168.1.101", 10001)) |     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=send_audio, args=(client,)).start() | ||||||
|     thread_manager.MyThread(target=receive_audio, args=(client,)).start() |     thread_manager.MyThread(target=receive_audio, args=(client,)).start() | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 andrew lee
						andrew lee