修复agent 中英文混乱问题,修复背景信息未使用问题
This commit is contained in:
@@ -102,24 +102,23 @@ async def chat_stream(request: DeepAgentChatRequest):
|
||||
响应流包含三种类型的事件:会话开始、节点消息、会话结束
|
||||
|
||||
"""
|
||||
if request.thread_id:
|
||||
need_title = False
|
||||
else:
|
||||
need_title = True
|
||||
# ===================== 简洁优化版 =====================
|
||||
# 1. 线程与标题标记
|
||||
need_title = not request.thread_id
|
||||
source_thread_id = request.thread_id
|
||||
checkpoint_id = request.checkpoint_id
|
||||
|
||||
# 1. 確定目標 thread_id
|
||||
is_branching = source_thread_id and checkpoint_id
|
||||
# 2. 目标线程 ID
|
||||
is_branching = all([source_thread_id, checkpoint_id])
|
||||
target_thread_id = str(uuid.uuid4())[:8] if is_branching else (source_thread_id or str(uuid.uuid4())[:8])
|
||||
# 构建主agent
|
||||
workspace_dir = os.path.join(PROJECT_ROOT, f"agent_workspace/{target_thread_id}")
|
||||
logger.info(f"chat request data: {request} | target_thread_id : workspace_dir: {workspace_dir}")
|
||||
|
||||
# 3. Agent 初始化
|
||||
workspace_dir = os.path.join(PROJECT_ROOT, "agent_workspace", target_thread_id)
|
||||
logger.info(f"chat request data: {request} | target_thread_id: {target_thread_id}, workspace_dir: {workspace_dir}")
|
||||
main_agent = build_main_agent(workspace_dir, request.enable_thinking)
|
||||
|
||||
# 2. 配置參數
|
||||
# 4. 配置
|
||||
temp = request.config_params.temperature if request.config_params else 0.7
|
||||
|
||||
current_config = {
|
||||
"recursion_limit": 120,
|
||||
"configurable": {
|
||||
@@ -129,37 +128,38 @@ async def chat_stream(request: DeepAgentChatRequest):
|
||||
}
|
||||
}
|
||||
|
||||
# 3. 初始化消息 + 系統提示 TODO 写入数据库
|
||||
# 5. 初始化系统消息
|
||||
initial_messages = []
|
||||
if not source_thread_id or is_branching:
|
||||
if request.config_params:
|
||||
cp = request.config_params
|
||||
system_prompt = (
|
||||
f"Current furniture design background settings:\n"
|
||||
f"- type: {cp.type}\n"
|
||||
f"- space/region: {cp.region}\n"
|
||||
f"- style tendency: {cp.style}\n"
|
||||
f"Please strictly follow the above settings in subsequent conversations。"
|
||||
)
|
||||
initial_messages.append(SystemMessage(content=system_prompt))
|
||||
cp = request.config_params
|
||||
if cp:
|
||||
config_items = [
|
||||
("type", cp.type),
|
||||
("space/region", cp.region),
|
||||
("style tendency", cp.style)
|
||||
]
|
||||
valid_lines = [f"- {k}: {v}" for k, v in config_items if v]
|
||||
if valid_lines:
|
||||
system_prompt = (
|
||||
"Current furniture design background settings:\n"
|
||||
+ "\n".join(valid_lines) + "\n"
|
||||
"Please strictly follow the above settings in subsequent conversations。"
|
||||
)
|
||||
initial_messages.append(SystemMessage(content=system_prompt))
|
||||
|
||||
# 4. 處理分支(從歷史 checkpoint 複製狀態)
|
||||
# 6. 分支处理
|
||||
if is_branching:
|
||||
source_config = {
|
||||
"configurable": {
|
||||
"thread_id": source_thread_id,
|
||||
"checkpoint_id": checkpoint_id
|
||||
}
|
||||
}
|
||||
source_config = {"configurable": {"thread_id": source_thread_id, "checkpoint_id": checkpoint_id}}
|
||||
last_checkpoint_id = await get_branch_checkpoint_id(main_agent, source_config)
|
||||
older_state = await main_agent.aget_state(source_config)
|
||||
combined_values = older_state.values.copy()
|
||||
if initial_messages:
|
||||
combined_values["messages"] = list(combined_values.get("messages", [])) + initial_messages
|
||||
combined_values["messages"] = combined_values.get("messages", []) + initial_messages
|
||||
await main_agent.aupdate_state(current_config, combined_values)
|
||||
else:
|
||||
last_checkpoint_id = await get_checkpoint_id(main_agent, current_config)
|
||||
|
||||
# 7. 事件流生成
|
||||
async def event_generator() -> AsyncGenerator[str, None]:
|
||||
is_first = True
|
||||
content = [{"type": "text", "text": request.message}]
|
||||
@@ -168,50 +168,52 @@ async def chat_stream(request: DeepAgentChatRequest):
|
||||
"quote_image": "",
|
||||
"current_image": ""
|
||||
}
|
||||
# 用户上传图片
|
||||
input_image_content = ''
|
||||
input_image_content = ""
|
||||
|
||||
# 处理上传图片
|
||||
if request.input_image_paths:
|
||||
input_image_content += "\n【附件上传图片路径】\n"
|
||||
for i, path in enumerate(request.input_image_paths):
|
||||
input_image_content += f"- 上传图片{i}: {path}\n"
|
||||
|
||||
bucket, object_name = path.split('/', 1)
|
||||
# image_url = get_presigned_url(oss_client=minio_client, bucket=bucket, object_name=object_name)
|
||||
copy_result = minio_client.copy_object("fida-public-bucket", path, CopySource(bucket, object_name))
|
||||
image_url = f"https://www.minio-api.aida.com.hk/{copy_result.bucket_name}/{copy_result.object_name}"
|
||||
bucket, obj = path.split("/", 1)
|
||||
minio_client.copy_object("fida-public-bucket", path, CopySource(bucket, obj))
|
||||
image_url = f"https://www.minio-api.aida.com.hk/fida-public-bucket/{path}"
|
||||
content.append({"type": "image_url", "image_url": {"url": image_url}})
|
||||
files["input_image"].append(path)
|
||||
|
||||
# 用户引用图片
|
||||
# 处理引用图片
|
||||
if request.quote_image_path:
|
||||
input_image_content += "\n【附件引用图片路径】\n"
|
||||
input_image_content += f"- 引用图片: {request.quote_image_path}\n"
|
||||
|
||||
bucket, object_name = request.quote_image_path.split('/', 1)
|
||||
# image_url = get_presigned_url(oss_client=minio_client, bucket=bucket, object_name=object_name)
|
||||
copy_result = minio_client.copy_object("fida-public-bucket", request.quote_image_path, CopySource(bucket, object_name))
|
||||
image_url = f"https://www.minio-api.aida.com.hk/{copy_result.bucket_name}/{copy_result.object_name}"
|
||||
bucket, obj = request.quote_image_path.split("/", 1)
|
||||
minio_client.copy_object("fida-public-bucket", request.quote_image_path, CopySource(bucket, obj))
|
||||
image_url = f"https://www.minio-api.aida.com.hk/fida-public-bucket/{request.quote_image_path}"
|
||||
content.append({"type": "image_url", "image_url": {"url": image_url}})
|
||||
files["quote_image"] = request.quote_image_path
|
||||
|
||||
# 追加文本内容
|
||||
if input_image_content:
|
||||
content[0]['text'] += input_image_content
|
||||
final_messages = {
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": content
|
||||
},
|
||||
],
|
||||
"files": files
|
||||
}
|
||||
content[0]["text"] += input_image_content
|
||||
|
||||
message_list = [{"role": "user", "content": content}]
|
||||
final_messages = {"messages": message_list, "files": files}
|
||||
logger.info(final_messages)
|
||||
|
||||
config_content_type = f"- type: {request.config_params.type}\n" if request.config_params.type else ""
|
||||
config_content_region = f"- region: {request.config_params.region}\n" if request.config_params.region else ""
|
||||
config_content_style = f"- style: {request.config_params.style}\n" if request.config_params.style else ""
|
||||
|
||||
async for stream in main_agent.astream(
|
||||
final_messages,
|
||||
config=current_config,
|
||||
stream_mode=["updates", "messages", "custom"],
|
||||
subgraphs=True,
|
||||
context=Context(use_report=request.use_report, language=request.language),
|
||||
context=Context(use_report=request.use_report,
|
||||
language=request.language,
|
||||
type=config_content_type,
|
||||
region=config_content_region,
|
||||
style=config_content_style,
|
||||
),
|
||||
):
|
||||
_, mode, chunks = stream
|
||||
if is_first:
|
||||
|
||||
Reference in New Issue
Block a user