This commit is contained in:
parent
1493f9d1fe
commit
11a4505db1
|
|
@ -17,6 +17,7 @@ class ConversationState(rx.State):
|
|||
"""
|
||||
会话状态
|
||||
"""
|
||||
|
||||
# 当前会话唯一标识
|
||||
current_conversation_id: str = str(uuid7())
|
||||
# 会话字典
|
||||
|
|
@ -133,7 +134,10 @@ class ConversationState(rx.State):
|
|||
current_conversation = self.conversations[self.current_conversation_id]
|
||||
|
||||
# 若当前运行唯一标识为空或不存在则返回 False
|
||||
if not self.current_run_id or self.current_run_id not in current_conversation.runs:
|
||||
if (
|
||||
not self.current_run_id
|
||||
or self.current_run_id not in current_conversation.runs
|
||||
):
|
||||
return False
|
||||
|
||||
# 当前运行
|
||||
|
|
@ -153,7 +157,7 @@ class ConversationState(rx.State):
|
|||
|
||||
# 当前会话
|
||||
current_conversation = self.conversations[self.current_conversation_id]
|
||||
|
||||
|
||||
# 获取用户提示词
|
||||
user_prompt = form_data["user_prompt"].strip()
|
||||
if not user_prompt:
|
||||
|
|
@ -171,32 +175,38 @@ class ConversationState(rx.State):
|
|||
user_prompt=user_prompt,
|
||||
message_history=message_history,
|
||||
):
|
||||
# 若为运行开始事件则将事件运行唯一标识设置为当前运行唯一标识
|
||||
if event.event_kind == EventKind.RUN_START:
|
||||
self.current_run_id = event.run_id
|
||||
continue
|
||||
|
||||
# 若当前运行唯一标识为空或与事件运行唯一标识不相同则跳过
|
||||
if not self.current_run_id or self.current_run_id != event.run_id:
|
||||
|
||||
# 若事件为空或当前运行唯一标识为空或当前运行唯一标识与事件运行唯一标识不相同则跳过
|
||||
if (
|
||||
not event
|
||||
or not self.current_run_id
|
||||
or self.current_run_id != event.run_id
|
||||
):
|
||||
continue
|
||||
|
||||
# 若当前运行唯一标识不存在则新增运行
|
||||
if self.current_run_id not in current_conversation.runs:
|
||||
# 新增运行
|
||||
current_conversation.runs[self.current_run_id] = Run(user_prompt=user_prompt, is_streaming=True)
|
||||
# ========== 运行开始 ==========
|
||||
if event.event_kind == EventKind.RUN_START:
|
||||
# 将事件运行唯一标识设置为当前运行唯一标识
|
||||
self.current_run_id = event.run_id
|
||||
# 创建运行
|
||||
current_conversation.runs[self.current_run_id] = Run(
|
||||
user_prompt=user_prompt, is_streaming=True
|
||||
)
|
||||
yield # 通知前端渲染
|
||||
continue
|
||||
|
||||
# 当前运行
|
||||
current_run = current_conversation.runs[self.current_run_id]
|
||||
|
||||
|
||||
# ========== 推理 ==========
|
||||
if event.event_kind == EventKind.PART_START and event.part_kind == PartKind.THINKING:
|
||||
if (
|
||||
event.event_kind == EventKind.PART_START
|
||||
and event.part_kind == PartKind.THINKING
|
||||
):
|
||||
current_run.thinking += event.event_content
|
||||
yield # 通知前端渲染
|
||||
continue
|
||||
|
||||
|
||||
if part_index := event.part_index:
|
||||
if part_index not in current_run.reasonings:
|
||||
current_run.reasonings[part_index] += event.event_content
|
||||
|
|
@ -204,7 +214,10 @@ class ConversationState(rx.State):
|
|||
continue
|
||||
|
||||
# ========== 回答 ==========
|
||||
if event.part_kind == PartKind.TEXT:
|
||||
if (
|
||||
event.part_kind == PartKind.TEXT
|
||||
and event.event_content != EventKind.PART_END
|
||||
):
|
||||
current_run.answer += event.event_content
|
||||
yield # 通知前端渲染
|
||||
continue
|
||||
|
|
@ -213,24 +226,11 @@ class ConversationState(rx.State):
|
|||
if event.event_kind == EventKind.RUN_END:
|
||||
# 保存运行新增消息
|
||||
await database_state.save_new_message(
|
||||
conversation_id=self.current_conversation_id, run_id=self.current_run_id, run_new_message=event.run_new_messages
|
||||
conversation_id=self.current_conversation_id,
|
||||
run_id=self.current_run_id,
|
||||
run_new_message=event.run_new_messages,
|
||||
)
|
||||
# 设置当前运行流式输出状态非正在流式输出
|
||||
current_run.is_streaming = False
|
||||
yield # 通知前端渲染
|
||||
continue
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -305,6 +305,18 @@ class AIAgent:
|
|||
tool_name=tool_name,
|
||||
run_id=current_run_id,
|
||||
)
|
||||
# 文本分片结束事件
|
||||
case TextPart(
|
||||
part_kind=part_kind, content=content
|
||||
):
|
||||
yield Event(
|
||||
event_kind=EventKind(event_kind),
|
||||
event_content=content,
|
||||
part_index=index,
|
||||
part_kind=PartKind(part_kind),
|
||||
next_part_kind=PartKind(next_part_kind),
|
||||
run_id=current_run_id,
|
||||
)
|
||||
|
||||
# ========== 函数工具调用事件 ==========
|
||||
case FunctionToolCallEvent(
|
||||
|
|
|
|||
Loading…
Reference in New Issue