上线THINK功能
This commit is contained in:
parent
51b6d085fd
commit
0c8266797e
@ -26,11 +26,31 @@ agent_manager = init_global_sharded_agent_manager(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def generate_stream_response(agent, messages, tool_response: bool, model: str):
|
async def generate_stream_response(agent, messages, thought_list, tool_response: bool, model: str):
|
||||||
"""生成流式响应"""
|
"""生成流式响应"""
|
||||||
accumulated_content = ""
|
accumulated_content = ""
|
||||||
|
|
||||||
|
|
||||||
chunk_id = 0
|
chunk_id = 0
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
if len(thought_list)>0:
|
||||||
|
accumulated_content = get_content_from_messages(thought_list, tool_response=tool_response)
|
||||||
|
chunk_data = {
|
||||||
|
"id": f"chatcmpl-thought",
|
||||||
|
"object": "chat.completion.chunk",
|
||||||
|
"created": int(__import__('time').time()),
|
||||||
|
"model": model,
|
||||||
|
"choices": [{
|
||||||
|
"index": 0,
|
||||||
|
"delta": {
|
||||||
|
"content": accumulated_content
|
||||||
|
},
|
||||||
|
"finish_reason": None
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
yield f"data: {json.dumps(chunk_data, ensure_ascii=False)}\n\n"
|
||||||
|
|
||||||
for response in agent.run(messages=messages):
|
for response in agent.run(messages=messages):
|
||||||
previous_content = accumulated_content
|
previous_content = accumulated_content
|
||||||
accumulated_content = get_content_from_messages(response, tool_response=tool_response)
|
accumulated_content = get_content_from_messages(response, tool_response=tool_response)
|
||||||
@ -202,7 +222,8 @@ async def create_agent_and_generate_response(
|
|||||||
|
|
||||||
if all_checks:
|
if all_checks:
|
||||||
# 将checks数组格式化为JSON字符串
|
# 将checks数组格式化为JSON字符串
|
||||||
guideline_analysis = json.dumps({"checks": all_checks}, ensure_ascii=False)
|
guideline_analysis = "\n".join([item["rationale"] for item in all_checks])
|
||||||
|
# guideline_analysis = json.dumps({"checks": all_checks}, ensure_ascii=False)
|
||||||
print(f"Merged guideline analysis result: {guideline_analysis}")
|
print(f"Merged guideline analysis result: {guideline_analysis}")
|
||||||
|
|
||||||
# 将分析结果添加到最后一个消息的内容中
|
# 将分析结果添加到最后一个消息的内容中
|
||||||
@ -226,17 +247,20 @@ async def create_agent_and_generate_response(
|
|||||||
user_identifier=user_identifier
|
user_identifier=user_identifier
|
||||||
)
|
)
|
||||||
|
|
||||||
|
thought_list = []
|
||||||
|
if guideline_analysis != '':
|
||||||
|
thought_list = [{"role": "assistant","content": guideline_analysis}]
|
||||||
# 根据stream参数决定返回流式还是非流式响应
|
# 根据stream参数决定返回流式还是非流式响应
|
||||||
if stream:
|
if stream:
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
generate_stream_response(agent, messages, tool_response, model_name),
|
generate_stream_response(agent, messages, thought_list, tool_response, model_name),
|
||||||
media_type="text/event-stream",
|
media_type="text/event-stream",
|
||||||
headers={"Cache-Control": "no-cache", "Connection": "keep-alive"}
|
headers={"Cache-Control": "no-cache", "Connection": "keep-alive"}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# 非流式响应
|
# 非流式响应
|
||||||
final_responses = agent.run_nonstream(messages)
|
agent_responses = agent.run_nonstream(messages)
|
||||||
|
final_responses = thought_list+agent_responses
|
||||||
if final_responses and len(final_responses) > 0:
|
if final_responses and len(final_responses) > 0:
|
||||||
# 使用 get_content_from_messages 处理响应,支持 tool_response 参数
|
# 使用 get_content_from_messages 处理响应,支持 tool_response 参数
|
||||||
content = get_content_from_messages(final_responses, tool_response=tool_response)
|
content = get_content_from_messages(final_responses, tool_response=tool_response)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user