189 lines
7.8 KiB
Python
189 lines
7.8 KiB
Python
"""多轮agent循环版本的理解层:不再是一次性吐出整份JSON,而是像一个真正在动手
|
|
建模的人一样,一步一步调用能力、时不时看一眼当前状态/渲染图、自己发现问题自己
|
|
改,最后调用finish提交、执行器自动核验,核验不通过可以在同一个会话里继续修正
|
|
——不需要像原来的三级兜底那样整个从头重来。
|
|
|
|
这是参考同类项目(ai-mechanical-3dcad-design-agent)"编码agent驱动CAD、边做边看、
|
|
自动验证再修正"的思路,把它内建到我们自己的后端服务里,不需要用户额外装一个
|
|
编码agent——这正是我们跟那个项目最大的架构差异,也是补上的那一课。
|
|
"""
|
|
|
|
import base64
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from openai import OpenAI
|
|
|
|
from .agent_session import AgentSession
|
|
from .agent_tools import CONTROL_TOOLS, build_capability_tools
|
|
from .llm_client import MAX_IMAGES, MODEL
|
|
from .prompt_builder import build_agent_system_prompt
|
|
from .registry import load_registry
|
|
from .images import normalize_images, response_content
|
|
from .validation import safe_name
|
|
|
|
MAX_STEPS = 60
|
|
|
|
|
|
class AgentGenerationError(Exception):
|
|
"""多轮agent循环始终无法收尾(比如超过步数上限)时抛出。"""
|
|
|
|
|
|
def _client() -> OpenAI:
|
|
import os
|
|
|
|
api_key = os.environ.get("OPENAI_API_KEY")
|
|
if not api_key:
|
|
raise AgentGenerationError("未找到 OPENAI_API_KEY,请检查 .env 文件")
|
|
return OpenAI(api_key=api_key)
|
|
|
|
|
|
def _to_input_item(item):
|
|
"""responses API返回的输出项(reasoning/function_call等)要原样喂回下一轮
|
|
input里维持会话连续性;SDK对象需要先转成普通dict才能重新序列化提交。"""
|
|
if hasattr(item, "model_dump"):
|
|
return item.model_dump(exclude_none=True)
|
|
return item
|
|
|
|
|
|
def generate_from_description_agentic(
|
|
user_text: str, base_dir: Path, image_base64: str | list[str] | None = None
|
|
) -> dict:
|
|
client = _client()
|
|
registry = load_registry(base_dir / "capabilities")
|
|
tools = build_capability_tools(registry) + CONTROL_TOOLS
|
|
system_prompt = build_agent_system_prompt(base_dir / "capabilities", user_text)
|
|
|
|
# 参考图可能不止一张(正视图/侧视图/俯视图分开拍),最多给MAX_IMAGES张,
|
|
# 跟一次性生成模式的build_user_content用的是同一条上限,行为保持一致。
|
|
images = normalize_images(image_base64)
|
|
user_content = response_content(user_text, images)
|
|
|
|
input_items = [
|
|
{"role": "system", "content": system_prompt},
|
|
{"role": "user", "content": user_content},
|
|
]
|
|
|
|
session = AgentSession(registry)
|
|
default_name = "agentic_part"
|
|
print(f"[agent] 开始多轮建模会话...")
|
|
|
|
for step in range(1, MAX_STEPS + 1):
|
|
resp = client.responses.create(
|
|
model=MODEL,
|
|
input=input_items,
|
|
tools=tools,
|
|
reasoning={"effort": "medium"},
|
|
)
|
|
input_items += [_to_input_item(item) for item in resp.output]
|
|
|
|
function_calls = [item for item in resp.output if item.type == "function_call"]
|
|
if not function_calls:
|
|
print(f"[agent] 第{step}步:模型只说话没调用工具,提醒继续")
|
|
input_items.append(
|
|
{
|
|
"role": "user",
|
|
"content": "请继续调用工具完成建模,或者调用finish提交结果,不要只输出文字说明。",
|
|
}
|
|
)
|
|
continue
|
|
|
|
for call in function_calls:
|
|
name = call.name
|
|
try:
|
|
args = json.loads(call.arguments or "{}")
|
|
except json.JSONDecodeError as e:
|
|
input_items.append(
|
|
{
|
|
"type": "function_call_output",
|
|
"call_id": call.call_id,
|
|
"output": json.dumps({"ok": False, "error": f"ARGS_JSON_ERROR: {e}"}),
|
|
}
|
|
)
|
|
continue
|
|
|
|
if name == "finish":
|
|
safe_name(args.get("name", default_name))
|
|
out_dir = base_dir / "output" / args.get("name", default_name)
|
|
result = session.finish(
|
|
output_id=args.get("output_id"),
|
|
claims=args.get("claims", []),
|
|
expected_solid_count=args.get("expected_solid_count"),
|
|
out_dir=out_dir,
|
|
)
|
|
print(f"[agent] 第{step}步:调用finish -> {'成功' if result.get('ok') else '未通过: ' + result.get('error','')[:150]}")
|
|
if result.get("ok"):
|
|
result["part_name"] = args.get("name", default_name)
|
|
result["llm_assumptions"] = args.get("assumptions", [])
|
|
result["llm_model"] = MODEL
|
|
result["source_description"] = user_text
|
|
result["agentic"] = True
|
|
result["step_count"] = step
|
|
(out_dir / "report.json").write_text(
|
|
json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8"
|
|
)
|
|
return result
|
|
input_items.append(
|
|
{
|
|
"type": "function_call_output",
|
|
"call_id": call.call_id,
|
|
"output": json.dumps(result, ensure_ascii=False),
|
|
}
|
|
)
|
|
continue
|
|
|
|
if name == "inspect_state":
|
|
result = session.get_state()
|
|
elif name == "describe_shape":
|
|
result = session.describe(args.get("id", ""))
|
|
elif name == "render_snapshot":
|
|
png = session.render_png(args.get("id", ""), view=args.get("view", "iso"))
|
|
if png is None:
|
|
result = {"ok": False, "error": f"UNKNOWN_ID: '{args.get('id')}' 不存在"}
|
|
input_items.append(
|
|
{
|
|
"type": "function_call_output",
|
|
"call_id": call.call_id,
|
|
"output": json.dumps(result, ensure_ascii=False),
|
|
}
|
|
)
|
|
else:
|
|
input_items.append(
|
|
{
|
|
"type": "function_call_output",
|
|
"call_id": call.call_id,
|
|
"output": "已生成渲染图,见下一条图片消息。",
|
|
}
|
|
)
|
|
input_items.append(
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "input_image",
|
|
"image_url": f"data:image/png;base64,{base64.b64encode(png).decode()}",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
continue
|
|
elif name.startswith("op_"):
|
|
op_name = name[len("op_") :]
|
|
target = args.pop("target", None)
|
|
op_id = args.pop("id", None)
|
|
result = session.apply_op(op_name, args, target=target, op_id=op_id)
|
|
tag = "OK" if result.get("ok") else "FAIL:" + result.get("error", "")[:100]
|
|
print(f"[agent] 第{step}步: {op_name} -> {tag}")
|
|
else:
|
|
result = {"ok": False, "error": f"UNKNOWN_TOOL: {name}"}
|
|
|
|
input_items.append(
|
|
{
|
|
"type": "function_call_output",
|
|
"call_id": call.call_id,
|
|
"output": json.dumps(result, ensure_ascii=False),
|
|
}
|
|
)
|
|
|
|
raise AgentGenerationError(f"超过{MAX_STEPS}步仍未调用finish完成建模")
|