Skip to content

Commit

Permalink
修复多个bug
Browse files Browse the repository at this point in the history
1、修复feifei重复创建导致商端口冲突的bug;
2、更换pygame初始化方式;
3、修复chatgpt(彩蛋)多论对话逻辑;
4、修复js "s"未定义bug。
  • Loading branch information
xszyou committed May 22, 2023
1 parent 81bb80c commit 3373a9f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 86 deletions.
32 changes: 8 additions & 24 deletions ai_module/nlp_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,18 @@
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:
Expand Down
108 changes: 53 additions & 55 deletions core/fay_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,60 @@
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 # 情绪值
Expand Down Expand Up @@ -591,60 +642,7 @@ def __waiting_speaking(self, file_url):
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




Expand Down
6 changes: 2 additions & 4 deletions gui/flask_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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__)
Expand Down Expand Up @@ -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'])
Expand Down
3 changes: 1 addition & 2 deletions gui/static/js/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -13615,5 +13615,4 @@
Result: Pp
}
}]).default
});
s
});
2 changes: 1 addition & 1 deletion python_connector_demo/remote_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 3373a9f

Please sign in to comment.