improve TrajectorySummarizer: add type hints, detailed prompt templat…improve TrajectorySummarizer: type hints, paper §3.3 prompt template, and robust fallback#5
Conversation
…e, and robust fallback - Add module-level docstring explaining the paper §3.3 context and call signature - Add full type annotations (List, Sequence, Tuple from typing) - Replace minimal prompt with the explicit §3.3 prompt template including trajectory/subgoal sections and output constraints - Extract _format_trajectory and _format_subgoal as dedicated static methods for cleaner separation of concerns - Add _last_observation fallback that scans reversed trajectory chunks, consistent with the summarization=False branch in cme_final.py - Extract first non-empty line from LLM output to enforce the single-line output requirement stated in the prompt instructions
There was a problem hiding this comment.
Pull request overview
This PR enhances TrajectorySummarizer to better mirror HiAgent paper §3.3 by expanding documentation, adding type hints, using a more explicit prompt template, and improving robustness of the summarization output/fallback behavior.
Changes:
- Added module-level docstring describing paper context, call signature, and expected input shapes.
- Replaced the minimal prompt with the paper §3.3-style prompt template and enforced single-line output by extracting the first non-empty line.
- Refactored formatting and fallback behavior into
_format_trajectory,_format_subgoal, and_last_observationhelpers.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 以及 cme_final.py 中对 TrajectorySummarizer 的调用(cme_final.py:160-182)复刻。 | ||
|
|
||
| 调用(来源:cme_final.py): |
There was a problem hiding this comment.
模块注释里引用的 cme_final.py 行号(“cme_final.py:160-182”)与当前仓库实际位置不一致,容易误导后续维护者。建议改为当前准确行号范围,或改成用函数名/搜索关键词引用而不是固定行号。
| 以及 cme_final.py 中对 TrajectorySummarizer 的调用(cme_final.py:160-182)复刻。 | |
| 调用(来源:cme_final.py): | |
| 以及 cme_final.py 中对 TrajectorySummarizer 的调用位置(可搜索 `TrajectorySummarizer(` 或 | |
| `generate_summary(`)复刻。 | |
| 调用(来源:cme_final.py 中相关调用逻辑,可搜索 `TrajectorySummarizer(`): |
| 会被填入 cme_final.py 中作为压缩后的 Observation。 | ||
|
|
||
| LLM 接口签名(来源:agentboard/llm/openai_gpt.py:74 等): | ||
| llm_model.generate(system_message: str, prompt: str) -> Tuple[bool, str] |
There was a problem hiding this comment.
这里记录的 LLM 接口签名不准确:仓库内多个 generate() 实现失败时会返回 (False, None),因此返回类型应为 Tuple[bool, Optional[str]](或在文档中说明失败时 completion 可能为 None)。否则类型提示和实际行为不一致。
| llm_model.generate(system_message: str, prompt: str) -> Tuple[bool, str] | |
| llm_model.generate(system_message: str, prompt: str) -> Tuple[bool, Optional[str]] | |
| 其中失败时 completion 可能为 None。 |
| summaries: List[str] = [] | ||
| for trajectory, subgoal in zip(trajectories, subgoals): | ||
| if not trajectory: | ||
| summaries.append("Subgoal completed.") | ||
| continue | ||
| prompt = self._build_prompt(trajectory, subgoal) | ||
| success, summary = self.llm_model.generate( | ||
| "You are a helpful assistant that summarizes agent trajectories concisely.", | ||
| prompt, | ||
| formatted_trajectory = self._format_trajectory(trajectory) | ||
| subgoal_text = self._format_subgoal(subgoal) | ||
| prompt = _PROMPT_TEMPLATE.format( |
There was a problem hiding this comment.
当前实现移除了空 trajectory 的显式处理;当 trajectory 为空或不包含 Observation 时,_last_observation() 会返回空字符串,最终会把空 Observation 写回历史(cme_final.py 会 new_history.append(..., ("Observation", summary)))。建议在 generate_summary 中对空/无 Observation 的情况返回一个非空占位文本(例如沿用旧实现的 "Subgoal completed."),避免下游出现空 observation。
…e, and robust fallback