自然进化

1、修复文字沟通接口打断功能;
2、修复qa命中采纳按钮点亮。
This commit is contained in:
guo zebin
2025-09-14 01:08:57 +08:00
parent 4d7102c8cf
commit 8540fa5dac
5 changed files with 22 additions and 16 deletions

View File

@@ -267,7 +267,11 @@ class FeiFei:
username = interact.data.get("user", "User")
if member_db.new_instance().is_username_exist(username) == "notexists":
member_db.new_instance().add_user(username)
MyThread(target=self.__process_interact, args=[interact]).start()
# 判断调用来源如果是非stream调用则同步处理
if interact.data.get("stream", False):
MyThread(target=self.__process_interact, args=[interact]).start()
else:
return self.__process_interact(interact)
else:
return self.__process_interact(interact)
@@ -739,7 +743,7 @@ class FeiFei:
self.__send_digital_human_message(text, username)
# 打印日志
util.printInfo(1, username, '({}) {}'.format(self.__get_mood_voice(), text))
util.printInfo(1, username, '({}) {}'.format("llm", text))
import importlib
fay_booter = importlib.import_module('fay_booter')

View File

@@ -314,15 +314,16 @@ class StreamManager:
# 处理句子标记(无锁,避免长时间持有锁)
is_first = "_<isfirst>" in sentence
is_end = "_<isend>" in sentence
sentence = sentence.replace("_<isfirst>", "").replace("_<isend>", "")
is_qa = "_<isqa>" in sentence
sentence = sentence.replace("_<isfirst>", "").replace("_<isend>", "").replace("_<isqa>", "")
# 执行实际处理(无锁,避免死锁)
if sentence or is_first or is_end:
if sentence or is_first or is_end or is_qa:
fay_core = fay_booter.feiFei
# 附带当前会话ID方便下游按会话控制输出
effective_cid = producer_cid if producer_cid is not None else getattr(self, 'conversation_ids', {}).get(username, "")
interact = Interact("stream", 1, {"user": username, "msg": sentence, "isfirst": is_first, "isend": is_end, "conversation_id": effective_cid})
fay_core.say(interact, sentence) # 调用核心处理模块进行响应
fay_core.say(interact, sentence, type="qa" if is_qa else "") # 调用核心处理模块进行响应
time.sleep(0.01) # 短暂休眠以控制处理频率

View File

@@ -343,17 +343,21 @@ def api_send_v1_chat_completions():
model = data.get('model', 'fay')
observation = data.get('observation', '')
interact = Interact("text", 1, {'user': username, 'msg': last_content, 'observation': str(observation)})
util.printInfo(1, username, '[文字沟通接口]{}'.format(interact.data["msg"]), time.time())
fay_booter.feiFei.on_interact(interact)
# 检查请求中是否指定了流式传输
stream_requested = data.get('stream', False)
# 优先使用请求中的stream参数如果没有指定则使用配置中的设置
if stream_requested or model == 'fay-streaming':
interact = Interact("text", 1, {'user': username, 'msg': last_content, 'observation': str(observation), 'stream':True})
util.printInfo(1, username, '[文字沟通接口(流式)]{}'.format(interact.data["msg"]), time.time())
fay_booter.feiFei.on_interact(interact)
return gpt_stream_response(last_content, username)
else:
interact = Interact("text", 1, {'user': username, 'msg': last_content, 'observation': str(observation), 'stream':False})
util.printInfo(1, username, '[文字沟通接口(非流式)]{}'.format(interact.data["msg"]), time.time())
fay_booter.feiFei.on_interact(interact)
return non_streaming_response(last_content, username)
except Exception as e:
return jsonify({'error': f'处理请求时出错: {e}'}), 500
@@ -415,10 +419,6 @@ def gpt_stream_response(last_content, username):
conversation_id = sm.get_conversation_id(username)
def generate():
while True:
# If interrupted or session switched, end the SSE stream promptly
if sm.should_stop_generation(username, conversation_id=conversation_id):
yield 'data: [DONE]\n\n'
break
sentence = nlp_Stream.read()
if sentence is None:
gsleep(0.01)
@@ -469,9 +469,6 @@ def non_streaming_response(last_content, username):
conversation_id = sm.get_conversation_id(username)
text = ""
while True:
# If interrupted or session switched, stop waiting and return what we have
if sm.should_stop_generation(username, conversation_id=conversation_id):
break
sentence = nlp_Stream.read()
if sentence is None:
gsleep(0.01)

View File

@@ -56,7 +56,7 @@ class StreamStateManager:
}
return conversation_id
def prepare_sentence(self, username, text, force_first=False, force_end=False, conversation_id=None):
def prepare_sentence(self, username, text, force_first=False, force_end=False, is_qa=False, conversation_id=None):
"""
准备要发送的句子:根据需要追加首尾标记并安全更新状态。
@@ -107,6 +107,8 @@ class StreamStateManager:
marked_text += "_<isfirst>"
if is_end and not marked_text.endswith("_<isend>"):
marked_text += "_<isend>"
if is_qa and not marked_text.endswith("_<isqa>"):
marked_text += "_<isqa>"
return marked_text, is_first, is_end
def end_session(self, username, conversation_id=None):

View File

@@ -111,6 +111,7 @@ class StreamTextProcessor:
sentence_text,
force_first=(not first_sentence_sent), # 第一段 True其它 False
force_end=False,
is_qa=is_qa,
conversation_id=conversation_id,
)
@@ -136,6 +137,7 @@ class StreamTextProcessor:
accumulated_text,
force_first=(not first_sentence_sent), # 如果还没发送过句子,这是第一段
force_end=True,
is_qa=is_qa,
conversation_id=conversation_id,
)
stream_manager.new_instance().write_sentence(