mirror of
https://github.com/xszyou/Fay.git
synced 2026-03-12 17:51:28 +08:00
年番更新
1、qa回复新增在范围内随机命中; 2、gpt问答新增当前时间获取; 3、命中qa的回复,标示为采纳; 4,新增执行python main.py start命令可自启动。
This commit is contained in:
@@ -63,7 +63,6 @@ modules = {
|
||||
"nlp_ollama_api": nlp_ollama_api,
|
||||
"nlp_coze": nlp_coze,
|
||||
"nlp_agent": fay_agent
|
||||
|
||||
}
|
||||
|
||||
#大语言模型回复
|
||||
@@ -121,9 +120,11 @@ class FeiFei:
|
||||
def __get_answer(self, interleaver, text):
|
||||
answer = None
|
||||
# 全局问答
|
||||
answer = qa_service.QAService().question('qa',text)
|
||||
answer, type = qa_service.QAService().question('qa',text)
|
||||
if answer is not None:
|
||||
return answer
|
||||
return answer, type
|
||||
else:
|
||||
return None, None
|
||||
|
||||
|
||||
#语音消息处理
|
||||
@@ -152,8 +153,8 @@ class FeiFei:
|
||||
wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"member","content":interact.data["msg"], "username":username, "uid":uid, "id":content_id}, "Username" : username})
|
||||
|
||||
#确定是否命中q&a
|
||||
answer = self.__get_answer(interact.interleaver, interact.data["msg"])
|
||||
|
||||
answer, type = self.__get_answer(interact.interleaver, interact.data["msg"])
|
||||
|
||||
#大语言模型回复
|
||||
text = ''
|
||||
textlist = []
|
||||
@@ -167,7 +168,7 @@ class FeiFei:
|
||||
|
||||
else:
|
||||
text = answer
|
||||
|
||||
|
||||
#记录回复
|
||||
self.write_to_file("./logs", "answer_result.txt", text)
|
||||
content_id = content_db.new_instance().add_content('fay','speak',text, username, uid)
|
||||
@@ -175,7 +176,10 @@ class FeiFei:
|
||||
#文字输出:面板、聊天窗、log、数字人
|
||||
if wsa_server.get_web_instance().is_connected(username):
|
||||
wsa_server.get_web_instance().add_cmd({"panelMsg": text, "Username" : username, 'robot': f'http://{cfg.fay_url}:5000/robot/Speaking.jpg'})
|
||||
wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"fay","content":text, "username":username, "uid":uid, "id":content_id}, "Username" : username})
|
||||
if type == 'qa':
|
||||
wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"fay","content":text, "username":username, "uid":uid, "id":content_id, "is_adopted":True}, "Username" : username})
|
||||
else:
|
||||
wsa_server.get_web_instance().add_cmd({"panelReply": {"type":"fay","content":text, "username":username, "uid":uid, "id":content_id, "is_adopted":False}, "Username" : username})
|
||||
if len(textlist) > 1:
|
||||
i = 1
|
||||
while i < len(textlist):
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import csv
|
||||
import difflib
|
||||
import random
|
||||
from utils import config_util as cfg
|
||||
from scheduler.thread_manager import MyThread
|
||||
import shlex
|
||||
@@ -37,14 +38,16 @@ class QAService:
|
||||
answer, action = self.__get_keyword(answer_dict, text, query_type)
|
||||
if action:
|
||||
MyThread(target=self.__run, args=[action]).start()
|
||||
return answer
|
||||
return answer, 'qa'
|
||||
|
||||
elif query_type == 'Persona':
|
||||
answer_dict = self.attribute_keyword
|
||||
answer, action = self.__get_keyword(answer_dict, text, query_type)
|
||||
return answer, 'Persona'
|
||||
elif query_type == 'command':
|
||||
answer, action = self.__get_keyword(self.command_keyword, text, query_type)
|
||||
return answer
|
||||
return answer, 'command'
|
||||
return None, None
|
||||
|
||||
def __run(self, action):
|
||||
time.sleep(0.1)
|
||||
@@ -78,22 +81,30 @@ class QAService:
|
||||
writer.writerow([question, answer])
|
||||
|
||||
def __get_keyword(self, keyword_dict, text, query_type):
|
||||
last_similar = 0
|
||||
last_answer = ''
|
||||
last_action = ''
|
||||
threshold = 0.6
|
||||
candidates = []
|
||||
|
||||
for qa in keyword_dict:
|
||||
if len(qa) < 2:
|
||||
continue
|
||||
for quest in qa[0]:
|
||||
similar = self.__string_similar(text, quest)
|
||||
if quest in text:
|
||||
similar += 0.3
|
||||
if similar > last_similar:
|
||||
last_similar = similar
|
||||
last_answer = qa[1]
|
||||
if query_type == "qa":
|
||||
last_action = qa[2]
|
||||
if last_similar >= 0.6:
|
||||
return last_answer, last_action
|
||||
return None, None
|
||||
if similar >= threshold:
|
||||
action = qa[2] if (query_type == "qa" and len(qa) > 2) else None
|
||||
candidates.append((similar, qa[1], action))
|
||||
|
||||
if not candidates:
|
||||
return None, None
|
||||
|
||||
candidates.sort(key=lambda x: x[0], reverse=True)
|
||||
|
||||
max_hits = max(1, int(len(keyword_dict) * 0.1))
|
||||
candidates = candidates[:max_hits]
|
||||
|
||||
chosen = random.choice(candidates)
|
||||
return chosen[1], chosen[2]
|
||||
|
||||
def __string_similar(self, s1, s2):
|
||||
return difflib.SequenceMatcher(None, s1, s2).quick_ratio()
|
||||
|
||||
@@ -189,14 +189,26 @@ class FayInterface {
|
||||
vueInstance.userList.push([data.panelReply.uid, data.panelReply.username]);
|
||||
}
|
||||
if (vueInstance.selectedUser && data.panelReply.username === vueInstance.selectedUser[1]) {
|
||||
if ('is_adopted' in data.panelReply && data.panelReply.is_adopted === true) {
|
||||
vueInstance.messages.push({
|
||||
id: data.panelReply.id,
|
||||
username: data.panelReply.username,
|
||||
content: data.panelReply.content,
|
||||
type: data.panelReply.type,
|
||||
timetext: this.getTime(),
|
||||
is_adopted: 1
|
||||
});
|
||||
} else {
|
||||
vueInstance.messages.push({
|
||||
id: data.panelReply.id,
|
||||
username: data.panelReply.username,
|
||||
content: data.panelReply.content,
|
||||
type: data.panelReply.type,
|
||||
timetext: this.getTime(),
|
||||
is_adopted:0
|
||||
});
|
||||
is_adopted: 0
|
||||
});
|
||||
}
|
||||
|
||||
vueInstance.$nextTick(() => {
|
||||
const chatContainer = vueInstance.$el.querySelector('.chatmessage');
|
||||
if (chatContainer) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import time
|
||||
import json
|
||||
import requests
|
||||
from urllib3.exceptions import InsecureRequestWarning
|
||||
from datetime import datetime
|
||||
import pytz
|
||||
|
||||
# 禁用不安全请求警告
|
||||
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
|
||||
@@ -42,6 +44,9 @@ def build_prompt(observation=""):
|
||||
return prompt
|
||||
|
||||
def get_communication_history(uid=0):
|
||||
tz = pytz.timezone('Asia/Shanghai')
|
||||
thistime = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
contentdb = content_db.new_instance()
|
||||
if uid == 0:
|
||||
communication_history = contentdb.get_list('all', 'desc', 11)
|
||||
@@ -57,6 +62,9 @@ def get_communication_history(uid=0):
|
||||
messages.append({"role": "user", "content": message_content})
|
||||
elif role == "fay":
|
||||
messages.append({"role": "assistant", "content": message_content})
|
||||
|
||||
if messages:
|
||||
messages[-1]["content"] += f" 当前时间:{thistime}。"
|
||||
return messages
|
||||
|
||||
def send_request(session, data):
|
||||
|
||||
9
main.py
9
main.py
@@ -5,6 +5,7 @@ import sys
|
||||
import time
|
||||
import psutil
|
||||
import re
|
||||
import argparse
|
||||
from utils import config_util, util
|
||||
from asr import ali_nls
|
||||
from core import wsa_server
|
||||
@@ -143,6 +144,14 @@ if __name__ == '__main__':
|
||||
util.log(1, '注册命令...')
|
||||
MyThread(target=console_listener).start()
|
||||
|
||||
parser = argparse.ArgumentParser(description="start自启动")
|
||||
parser.add_argument('command', nargs='?', default='', help="start")
|
||||
|
||||
parsed_args = parser.parse_args()
|
||||
if parsed_args.command.lower() == 'start':
|
||||
MyThread(target=fay_booter.start).start()
|
||||
|
||||
|
||||
#普通模式下启动窗口
|
||||
if config_util.start_mode == 'common':
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
Reference in New Issue
Block a user