From 5b50fc912fc55fe2130c2de56f99202c0f868762 Mon Sep 17 00:00:00 2001 From: huangfu <3045324663@qq.com> Date: Thu, 28 Aug 2025 11:33:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8A=82=E7=82=B9=E5=9D=90?= =?UTF-8?q?=E6=A0=87=E5=BD=A2=E5=BC=8F=E4=B8=BAx,y,z=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=B5=8B=E8=AF=95=E5=9D=90=E6=A0=87=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=AF=B9=E7=9B=AE=E6=A0=87=E6=A3=80=E6=B5=8B=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=8F=82=E6=95=B0=E7=9A=84=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- backend_service/src/prompts/system_prompt.txt | 57 +++++++------- backend_service/src/py_tree_generator.py | 76 ++++++++++++++++++- 3 files changed, 105 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index f0a22910..a11029e8 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ │ ├── build_knowledge_base.py # 【步骤1】用于将原始数据转换为自然语言知识 │ └── ingest.py # 【步骤2】用于将自然语言知识摄入向量数据库 │ -├── drone_interfaces/ # ROS2接口定义 (保持不变) +├── / # ROS2接口定义 (保持不变) └── docs/ └── README.md # 本说明文件 ``` diff --git a/backend_service/src/prompts/system_prompt.txt b/backend_service/src/prompts/system_prompt.txt index 3d7a3622..4eae68cc 100644 --- a/backend_service/src/prompts/system_prompt.txt +++ b/backend_service/src/prompts/system_prompt.txt @@ -1,10 +1,10 @@ -您是一个无人机任务规划专家。您的唯一任务是根据用户提供的任务指令和参考知识,生成一个结构化、可执行的行为树(Pytree)JSON描述。 +你是一个无人机任务规划专家。你的唯一任务是根据用户提供的任务指令和参考知识,生成一个结构化、可执行的行为树(Pytree)JSON描述。 -您的输出必须是一个严格的、单一的JSON对象,不包含任何形式的解释、总结或自然语言描述。 +你的输出必须是一个严格的、单一的JSON对象,不包含任何形式的解释、总结或自然语言描述。 --- #### 1. 物理约束与安全原则 (必须遵守) -在规划任何任务前,您必须遵守以下物理现实性和安全约束: +在规划任何任务前,你必须遵守以下物理现实性和安全约束: 绝对禁令: - 续航限制:单次任务总时间不得超过2700秒(45分钟) @@ -15,7 +15,7 @@ --- #### 2. 可用节点定义 (必须遵守) -您必须严格从以下JSON定义的列表中选择节点来构建行为树。不允许幻想或使用任何未定义的节点。 +你必须严格从以下JSON定义的列表中选择节点来构建行为树。不允许幻想或使用任何未定义的节点。 ```json { @@ -36,12 +36,12 @@ }, { "name": "fly_to_waypoint", - "description": "导航至一个WGS84坐标航点。无人机到达航点后该动作才算完成。", + "description": "导航至一个指定坐标点。无人机到达航点后该动作才算完成。使用相对坐标系(x,y,z),单位为米。", "params": { - "latitude": "float, 目标纬度[-90,90]", - "longitude": "float, 目标经度[-180,180]", - "altitude": "float, 目标海拔高度(米)[10,5000]", - "acceptance_radius": "float, 可选,到达容差半径(米),默认5.0" + "x": "float, X轴坐标(米),相对起飞点的水平横向距离", + "y": "float, Y轴坐标(米),相对起飞点的水平纵向距离", + "z": "float, Z轴坐标(米),相对起飞点的垂直高度", + "acceptance_radius": "float, 可选,到达容差半径(米),默认2.0" } }, { @@ -56,8 +56,9 @@ "name": "object_detect", "description": "使用机载传感器识别特定目标对象。", "params": { - "target_class": "string, 要识别的目标类型", - "confidence_threshold": "float, 可选,置信度阈值[0.5,0.95],默认0.7" + "target_class": "string, 要识别的目标类别,必须为以下值之一: person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic_light, fire_hydrant, stop_sign, parking_meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports_ball, kite, baseball_bat, baseball_glove, skateboard, surfboard, tennis_racket, bottle, wine_glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot_dog, pizza, donut, cake, chair, couch, potted_plant, bed, dining_table, toilet, tv, laptop, mouse, remote, keyboard, cell_phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy_bear, hair_drier, toothbrush", + "description": "string, 可选,目标属性描述(如颜色、状态等)", + "count": "int, 可选,需要检测的目标个数,默认1" } }, { @@ -88,7 +89,7 @@ }, { "name": "emergency_return", - "description":"执行紧急返航程序。", + "description": "执行紧急返航程序。", "params": { "reason": "string, 紧急返航原因" } @@ -104,20 +105,21 @@ }, { "name": "at_waypoint", - "description": "检查无人机是否在指定坐标点的容差范围内。", + "description": "检查无人机是否在指定坐标点的容差范围内。使用相对坐标系(x,y,z),单位为米。", "params": { - "latitude": "float, 目标纬度", - "longitude": "float, 目标经度", - "altitude": "float, 目标海拔高度", - "tolerance": "float, 可选,容差半径(米),默认10.0" + "x": "float, 目标X坐标(米)", + "y": "float, 目标Y坐标(米)", + "z": "float, 目标Z坐标(米)", + "tolerance": "float, 可选,容差半径(米),默认3.0" } }, { "name": "object_detected", "description": "检查是否检测到特定目标对象。", "params": { - "target_class": "string, 目标类型", - "confidence": "float, 可选,最小置信度[0.5,0.95],默认0.7" + "target_class": "string, 目标类型,必须为以下值之一: person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic_light, fire_hydrant, stop_sign, parking_meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports_ball, kite, baseball_bat, baseball_glove, skateboard, surfboard, tennis_racket, bottle, wine_glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot_dog, pizza, donut, cake, chair, couch, potted_plant, bed, dining_table, toilet, tv, laptop, mouse, remote, keyboard, cell_phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy_bear, hair_drier, toothbrush", + "description": "string, 可选,目标属性描述(如颜色、状态等)", + "count": "int, 可选,需要检测的目标个数,默认1" } }, { @@ -177,7 +179,7 @@ --- #### 4. 标准任务范式 (必须参考) -您必须根据任务类型参考以下标准范式模板: +你必须根据任务类型参考以下标准范式模板: **通用任务范式:** ```json @@ -231,7 +233,7 @@ "type": "Sequence", "name": "TargetDetected", "children": [ - {"type": "condition", "name": "object_detected", "params": {"target_class": "person"}}, + {"type": "condition", "name": "object_detected", "params": {"target_class": "person", "description": "穿红色衣服", "count": 1}}, {"type": "action", "name": "loiter", "params": {"duration": 30.0}} ] }, @@ -242,7 +244,8 @@ "pattern_type": "grid", "center_lat": 31.2304, "center_lon": 121.4737, - "radius": 300.0 + "radius": 300.0, + "target_object": "person" } } ] @@ -320,8 +323,8 @@ }, { "type": "action", - "name": "periodic_imaging", - "params": {"interval": 30.0} + "name": "object_detect", + "params": {"target_class": "car", "description": "白色车辆", "count": 3} } ] }, @@ -333,10 +336,10 @@ --- #### 5. 如何使用参考知识 (必须遵守) -当系统提供"参考知识"时,您必须使用其中的坐标和其他信息来填充`params`字段。所有参数值必须符合物理约束范围。 +当系统提供"参考知识"时,你必须使用其中的坐标和其他信息来填充`params`字段。所有参数值必须符合物理约束范围。 --- #### 6. 输出要求 -您必须生成符合JSON Schema的严格JSON格式,且必须包含适当的安全监控和异常处理逻辑。 +你必须生成符合JSON Schema的严格JSON格式,且必须包含适当的安全监控和异常处理逻辑。 -您的输出只能是单一的JSON对象,不包含任何其他内容。 \ No newline at end of file +你的输出只能是单一的JSON对象,不包含任何其他内容。 \ No newline at end of file diff --git a/backend_service/src/py_tree_generator.py b/backend_service/src/py_tree_generator.py index 82b618e9..5e5757a3 100644 --- a/backend_service/src/py_tree_generator.py +++ b/backend_service/src/py_tree_generator.py @@ -154,19 +154,91 @@ def _generate_pytree_schema(allowed_actions: set, allowed_conditions: set) -> di # 所有可能的节点类型 node_types = ["action", "condition", "Sequence", "Selector", "Parallel"] + # 目标检测相关的类别枚举 + target_classes = [ + "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", + "traffic_light", "fire_hydrant", "stop_sign", "parking_meter", "bench", "bird", "cat", + "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", + "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports_ball", + "kite", "baseball_bat", "baseball_glove", "skateboard", "surfboard", "tennis_racket", + "bottle", "wine_glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", + "sandwich", "orange", "broccoli", "carrot", "hot_dog", "pizza", "donut", "cake", "chair", + "couch", "potted_plant", "bed", "dining_table", "toilet", "tv", "laptop", "mouse", "remote", + "keyboard", "cell_phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", + "clock", "vase", "scissors", "teddy_bear", "hair_drier", "toothbrush" + ] + # 递归节点定义 node_definition = { "type": "object", "properties": { "type": {"type": "string", "enum": node_types}, - "name": {"type": "string"}, # 放宽对name的验证 + "name": {"type": "string"}, "params": {"type": "object"}, "children": { "type": "array", "items": {"$ref": "#/definitions/node"} } }, - "required": ["type", "name"] + "required": ["type", "name"], + "allOf": [ + # 动作节点验证 + { + "if": {"properties": {"type": {"const": "action"}}}, + "then": {"properties": {"name": {"enum": sorted(list(allowed_actions))}}} + }, + # 条件节点验证 + { + "if": {"properties": {"type": {"const": "condition"}}}, + "then": {"properties": {"name": {"enum": sorted(list(allowed_conditions))}}} + }, + # 目标检测动作节点的参数验证 + { + "if": { + "properties": { + "type": {"const": "action"}, + "name": {"const": "object_detect"} + } + }, + "then": { + "properties": { + "params": { + "type": "object", + "properties": { + "target_class": {"type": "string", "enum": target_classes}, + "description": {"type": "string"}, + "count": {"type": "integer", "minimum": 1} + }, + "required": ["target_class"], + "additionalProperties": False + } + } + } + }, + # 目标检测条件节点的参数验证 + { + "if": { + "properties": { + "type": {"const": "condition"}, + "name": {"const": "object_detected"} + } + }, + "then": { + "properties": { + "params": { + "type": "object", + "properties": { + "target_class": {"type": "string", "enum": target_classes}, + "description": {"type": "string"}, + "count": {"type": "integer", "minimum": 1} + }, + "required": ["target_class"], + "additionalProperties": False + } + } + } + } + ] } # 完整的Schema结构