This commit is contained in:
parent
c499b16c3b
commit
cc8efb7143
|
|
@ -98,12 +98,12 @@ class Run(BaseModel):
|
||||||
)
|
)
|
||||||
is_reasoning: bool = Field(
|
is_reasoning: bool = Field(
|
||||||
default=False,
|
default=False,
|
||||||
description="正在推理状态,True 表示正在推理,False 表示非正在推理",
|
description="推理状态,True 表示正在推理,False 表示非正在推理",
|
||||||
)
|
)
|
||||||
assistant_content: str = Field(default="", description="回复正文")
|
assistant_content: str = Field(default="", description="回复正文")
|
||||||
is_streaming: bool = Field(
|
is_running: bool = Field(
|
||||||
default=False,
|
default=False,
|
||||||
description="流式输出状态,True 表示正在流式输出,False 表示非正在流式输出",
|
description="运行状态,True 表示正在运行,False 表示非正在运行",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -155,10 +155,10 @@ class ConversationState(rx.State):
|
||||||
return conversation.runs if conversation else {}
|
return conversation.runs if conversation else {}
|
||||||
|
|
||||||
@rx.var
|
@rx.var
|
||||||
def get_run_streaming_status(self) -> bool:
|
def get_run_running_status(self) -> bool:
|
||||||
"""
|
"""
|
||||||
获取当前运行流式输出状态
|
获取当前运行运行状态
|
||||||
:return: 当前运行流式输出状态(True 表示正在流式输出,False 表示非正在流式输出)
|
:return: 当前运行状态(True 表示正在运行,False 表示非正在运行)
|
||||||
"""
|
"""
|
||||||
# 当前对话
|
# 当前对话
|
||||||
conversation = self.conversations.get(self.conversation_id)
|
conversation = self.conversations.get(self.conversation_id)
|
||||||
|
|
@ -167,7 +167,7 @@ class ConversationState(rx.State):
|
||||||
|
|
||||||
# 末位运行
|
# 末位运行
|
||||||
run = next(reversed(conversation.runs.values()))
|
run = next(reversed(conversation.runs.values()))
|
||||||
return run.is_streaming
|
return run.is_running
|
||||||
|
|
||||||
@rx.event
|
@rx.event
|
||||||
async def run(self, form_data: dict[str, Any]) -> AsyncGenerator[None]:
|
async def run(self, form_data: dict[str, Any]) -> AsyncGenerator[None]:
|
||||||
|
|
@ -200,15 +200,16 @@ class ConversationState(rx.State):
|
||||||
conversation.runs[str(uuid7())] = Run(user_prompt=user_prompt)
|
conversation.runs[str(uuid7())] = Run(user_prompt=user_prompt)
|
||||||
# 将末位运行唯一标识、运行设置为当前运行唯一标识、运行
|
# 将末位运行唯一标识、运行设置为当前运行唯一标识、运行
|
||||||
run_id, run = next(reversed(conversation.runs.items()))
|
run_id, run = next(reversed(conversation.runs.items()))
|
||||||
# 将流式输出状态设置为正在流式输出
|
# 将运行状态设置为正在运行
|
||||||
run.is_streaming = True
|
run.is_running = True
|
||||||
yield # 通知前端渲染流式输出状态
|
yield # 通知前端渲染运行状态
|
||||||
|
|
||||||
async for event in self.agent.run_stream_events(
|
async with self.agent.run_stream_events(
|
||||||
conversation_id=self.conversation_id,
|
conversation_id=self.conversation_id,
|
||||||
user_prompt=user_prompt,
|
user_prompt=user_prompt,
|
||||||
message_history=message_history,
|
message_history=message_history,
|
||||||
):
|
) as events:
|
||||||
|
async for event in events:
|
||||||
match event:
|
match event:
|
||||||
# ========== 分片开始事件 ==========
|
# ========== 分片开始事件 ==========
|
||||||
case PartStartEvent(
|
case PartStartEvent(
|
||||||
|
|
@ -219,9 +220,9 @@ class ConversationState(rx.State):
|
||||||
match part:
|
match part:
|
||||||
# 思考分片开始事件
|
# 思考分片开始事件
|
||||||
case ThinkingPart(
|
case ThinkingPart(
|
||||||
part_kind=part_kind, content=content
|
content=content
|
||||||
):
|
):
|
||||||
# 若上一分片种类为空则将正在推理状态设置为正在推理
|
# 若上一分片种类为空则将推理状态设置为正在推理
|
||||||
if not previous_part_kind:
|
if not previous_part_kind:
|
||||||
run.is_reasoning = True
|
run.is_reasoning = True
|
||||||
|
|
||||||
|
|
@ -241,7 +242,7 @@ class ConversationState(rx.State):
|
||||||
|
|
||||||
# 文本分片开始事件
|
# 文本分片开始事件
|
||||||
case TextPart(
|
case TextPart(
|
||||||
part_kind=part_kind, content=content
|
content=content
|
||||||
):
|
):
|
||||||
# 初始化回复正文
|
# 初始化回复正文
|
||||||
run.assistant_content = content
|
run.assistant_content = content
|
||||||
|
|
@ -284,7 +285,7 @@ class ConversationState(rx.State):
|
||||||
case ThinkingPart(
|
case ThinkingPart(
|
||||||
part_kind=part_kind, content=content
|
part_kind=part_kind, content=content
|
||||||
):
|
):
|
||||||
# 若下一分片种类为文本则将正在推理状态设置为非正在推理
|
# 若下一分片种类为文本则将推理状态设置为非正在推理
|
||||||
if next_part_kind == Kind.TEXT:
|
if next_part_kind == Kind.TEXT:
|
||||||
run.is_reasoning = False
|
run.is_reasoning = False
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue