161 lines
6.6 KiB
Python
161 lines
6.6 KiB
Python
"""把 capabilities/*.yaml 的契约转换成OpenAI tool-calling用的JSON Schema工具定义,
|
||
供理解层的多轮agent循环使用。跟prompt_builder.py给一次性生成模式用的"文字说明"是
|
||
同一份契约文件的另一种用途——新增能力时这里同样不需要改代码。
|
||
"""
|
||
|
||
_TYPE_MAP = {
|
||
"float": "number",
|
||
"int": "integer",
|
||
"bool": "boolean",
|
||
"str": "string",
|
||
"enum": "string",
|
||
}
|
||
|
||
|
||
def _param_schema(p: dict) -> dict:
|
||
if p.get('schema'):
|
||
from copy import deepcopy
|
||
schema = deepcopy(p['schema'])
|
||
if p.get('description'): schema['description'] = p['description']
|
||
return schema
|
||
ptype = p.get("type")
|
||
if ptype == "vector":
|
||
return {"type": "array", "items": {"type": "number"}, "minItems": 3, "maxItems": 3, "description": p.get("description", "世界XYZ向量")}
|
||
if ptype == "points":
|
||
return {
|
||
"type": "array",
|
||
"items": {"type": "array", "items": {"type": "number"}, "minItems": 2, "maxItems": 2},
|
||
"description": p.get("description", "二维顶点坐标序列 [[x1,y1],[x2,y2],...]"),
|
||
}
|
||
if ptype == "list_str":
|
||
return {"type": "array", "items": {"type": "string"}}
|
||
|
||
schema = {"type": _TYPE_MAP.get(ptype, "string")}
|
||
bits = [p['description']] if p.get('description') else []
|
||
if p.get("unit"):
|
||
bits.append(f"单位{p['unit']}")
|
||
if p.get("range"):
|
||
lo, hi = p["range"]
|
||
bits.append(f"范围[{lo},{hi}]")
|
||
if p.get("options"):
|
||
schema["enum"] = p["options"]
|
||
if bits:
|
||
schema["description"] = "; ".join(bits)
|
||
return schema
|
||
|
||
|
||
def build_capability_tools(registry: dict) -> list:
|
||
tools = []
|
||
for name, cap in registry.items():
|
||
contract = cap.contract
|
||
props = {}
|
||
required = []
|
||
for p in contract.get("parameters", []):
|
||
props[p["name"]] = _param_schema(p)
|
||
if p.get("required"):
|
||
required.append(p["name"])
|
||
|
||
desc = contract.get("description", "").strip()
|
||
pre = contract.get("preconditions", [])
|
||
if pre:
|
||
desc = desc + "\n前置条件: " + "; ".join(pre)
|
||
|
||
tools.append(
|
||
{
|
||
"type": "function",
|
||
"name": f"op_{name}",
|
||
"description": desc[:2000],
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"target": {
|
||
"type": "string",
|
||
"description": "可选,本步操作的输入取自哪个之前的id;独立生成新实体的能力不需要这个字段",
|
||
},
|
||
"id": {"type": "string", "description": "可选,本步结果的标识符,省略则自动分配"},
|
||
**props,
|
||
},
|
||
"required": required,
|
||
},
|
||
}
|
||
)
|
||
return tools
|
||
|
||
|
||
CONTROL_TOOLS = [
|
||
{
|
||
"type": "function",
|
||
"name": "inspect_state",
|
||
"description": "查看当前会话已经生成过哪些id、最近一步是什么,用来确认自己的进度、避免忘记已有哪些中间结果",
|
||
"parameters": {"type": "object", "properties": {}},
|
||
},
|
||
{
|
||
"type": "function",
|
||
"name": "describe_shape",
|
||
"description": "查看某个id对应实体的具体数据:包围盒、体积、实体数量、是否几何合法",
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {"id": {"type": "string"}},
|
||
"required": ["id"],
|
||
},
|
||
},
|
||
{
|
||
"type": "function",
|
||
"name": "render_snapshot",
|
||
"description": (
|
||
"把某个id对应的当前几何渲染成一张示意图返回给你看(不是精细渲染,只是帮你确认大致轮廓/"
|
||
"孔洞位置/比例对不对)。建议在完成一个重要阶段性结构之后调用,不需要每一步都调用,会拖慢速度。"
|
||
"只看一个角度容易漏掉某个方向上的错误(比如孔偏了但正好被这个视角挡住),"
|
||
"对复杂结构或者最终提交前,建议至少换一个视角(比如iso之外再看一次top/front)确认"
|
||
),
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"id": {"type": "string"},
|
||
"view": {
|
||
"type": "string",
|
||
"enum": ["iso", "front", "top", "side"],
|
||
"description": "观察角度,不填默认iso(等轴测)",
|
||
},
|
||
},
|
||
"required": ["id"],
|
||
},
|
||
},
|
||
{
|
||
"type": "function",
|
||
"name": "finish",
|
||
"description": (
|
||
"声明建模完成,提交最终输出。执行器会自动核对:最终实体是否几何合法、(可选)独立实体数量"
|
||
"是否符合expected_solid_count、(可选)claims里声明的每条结构事实是否真的成立。核对不通过"
|
||
"会把具体原因告诉你,你可以在同一个会话里继续修正、再次调用finish,不需要从头开始"
|
||
),
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"output_id": {"type": "string", "description": "作为最终零件输出的那个id"},
|
||
"name": {"type": "string", "description": "零件的简短英文/拼音标识,如 flange_bracket_01"},
|
||
"expected_solid_count": {
|
||
"type": "integer",
|
||
"description": "可选,明确知道独立实体数量就填;用了标准件库能力(比如bearing)的话不要填",
|
||
},
|
||
"assumptions": {
|
||
"type": "array",
|
||
"items": {"type": "string"},
|
||
"description": "用中文列出你自行假设的关键尺寸/数值",
|
||
},
|
||
"claims": {
|
||
"type": "array",
|
||
"description": (
|
||
"机器可核验的结构声明。强制:用户明确给出的每一个数字都必须配一条对应的claim,"
|
||
"留空等于这次提交完全没有验证过任何一个用户给的数字。"
|
||
"每条是一个对象,type取值只能是: hole/axis_direction/real_overlap/no_overlap/dimension/arc/opening,"
|
||
"字段跟一次性生成模式下claims的格式完全一样"
|
||
),
|
||
"items": {"type": "object"},
|
||
},
|
||
},
|
||
"required": ["output_id", "name"],
|
||
},
|
||
},
|
||
]
|