生成多阶段任务计划 Args: task_description: 任务描述 planner_client: Planner客户端 planner_model: Planner模型名称 experience_results: 经验检索结果(可选) temperature: 采样温度 Returns: Plan对象,生成失败返回None
(
task_description: str,
planner_client: OpenAI,
planner_model: str,
experience_results: str = "",
temperature: float = 0.0
)
| 265 | |
| 266 | |
| 267 | def generate_plan( |
| 268 | task_description: str, |
| 269 | planner_client: OpenAI, |
| 270 | planner_model: str, |
| 271 | experience_results: str = "", |
| 272 | temperature: float = 0.0 |
| 273 | ) -> Optional[Plan]: |
| 274 | """ |
| 275 | 生成多阶段任务计划 |
| 276 | |
| 277 | Args: |
| 278 | task_description: 任务描述 |
| 279 | planner_client: Planner客户端 |
| 280 | planner_model: Planner模型名称 |
| 281 | experience_results: 经验检索结果(可选) |
| 282 | temperature: 采样温度 |
| 283 | |
| 284 | Returns: |
| 285 | Plan对象,生成失败返回None |
| 286 | """ |
| 287 | # 检索相关经验 - 多阶段任务使用多阶段模板 |
| 288 | if PromptTemplateSearch and default_multistage_template_path.exists(): |
| 289 | try: |
| 290 | search_engine = PromptTemplateSearch(template_path=default_multistage_template_path, storage_dir=DEFAULT_MULTISTORAGE_STORAGE_DIR) |
| 291 | experience_results = search_engine.get_experience( |
| 292 | task_description, |
| 293 | 1 # 获取1个最相关经验用于多阶段任务规划 |
| 294 | ) |
| 295 | logging.info(f"计划生成时检索到的相关经验:\n{experience_results}") |
| 296 | except Exception as e: |
| 297 | logging.warning(f"经验检索失败: {e}") |
| 298 | experience_results = "" |
| 299 | |
| 300 | prompt = PLANNER_PLAN_GENERATION_PROMPT.format( |
| 301 | task_description=task_description, |
| 302 | experience_content=experience_results or "无相关经验", |
| 303 | app_list = APP_LIST |
| 304 | ) |
| 305 | |
| 306 | logging.info(f"计划生成prompt: \n{prompt}") |
| 307 | |
| 308 | response = planner_client.chat.completions.create( |
| 309 | model=planner_model, |
| 310 | messages=[{"role": "user", "content": prompt}], |
| 311 | temperature=temperature |
| 312 | ) |
| 313 | |
| 314 | response_str = response.choices[0].message.content |
| 315 | logging.info(f"计划生成响应: \n{response_str}") |
| 316 | |
| 317 | try: |
| 318 | plan_data = parse_planner_response(response_str) |
| 319 | plan = Plan.model_validate(plan_data) |
| 320 | logging.info(f"生成的计划: {plan}") |
| 321 | return plan |
| 322 | except Exception as e: |
| 323 | logging.error(f"生成计划失败: {e}") |
| 324 | return None |
no test coverage detected