Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update openssa.core.programming.hierarchical.planner.HTPlanner.construct_htp(...) to reduce JSON-generation requirements #338

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions openssa/core/programming/hierarchical/_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,20 @@ def htp_prompt_template(with_resources: bool) -> str:
{problem}
```

please return a suggested task-decomposition JSON list with up to {max_subtasks_per_decomp} items
please return a sequence of up to {max_subtasks_per_decomp} sentences/paragraphs,
EACH PREPENDED by a header "[SUB-QUESTION/PROBLEM/TASK]" (EXACTLY LITERALLY SO! DO NOT SUBSTITUTE!),
describing how such top-level question/problem/task could/should be decomposed into sub-questions/problems/tasks,
per the following template:

```
[
"(textual description of 1st sub-question/problem/task to answer/solve)",
"(textual description of 2nd sub-question/problem/task to answer/solve)",
...
]
[SUB-QUESTION/PROBLEM/TASK]
<textual description of 1st sub-question/problem/task to answer/solve>
[SUB-QUESTION/PROBLEM/TASK]
<textual description of 1st sub-question/problem/task to answer/solve>
...
```

Please return ONLY the JSON LIST and no other text, not even the "```json" wrapping!
Please return ONLY the SEQUENCE OF SENTENCES/PARAGRAPHS WITH SUCH HEADERS, and no other text.
""" # noqa: E122
)

Expand Down
28 changes: 21 additions & 7 deletions openssa/core/programming/hierarchical/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from .plan import HTPDict


SUBTASK_HEADER: str = '[SUB-QUESTION/PROBLEM/TASK]\n'


@dataclass
class HTPlanner(AbstractProgrammer):
"""Hierarchical Task Planner."""
Expand All @@ -46,13 +49,24 @@ def construct_htp(self, task: Task, knowledge: set[Knowledge] | None = None, rea
reasoner: AbstractReasoner = OodaReasoner(lm=self.lm)

if self.max_depth > 0:
sub_task_descriptions: list[str] = self.lm.get_response(
prompt=SIMPLIFIED_DECOMPOSITION_PROMPT_TEMPLATE.format(
problem=task.ask,
resource_overviews={resource.unique_name: resource.overview for resource in task.resources},
max_subtasks_per_decomp=self.max_subtasks_per_decomp),
history=knowledge_injection_lm_chat_msgs(knowledge=knowledge) if knowledge else None,
json_format=True)
prompt: str = SIMPLIFIED_DECOMPOSITION_PROMPT_TEMPLATE.format(
problem=task.ask,
resource_overviews={resource.unique_name: resource.overview for resource in task.resources},
max_subtasks_per_decomp=self.max_subtasks_per_decomp)

knowledge_lm_hist: LMChatHist | None = (knowledge_injection_lm_chat_msgs(knowledge=knowledge)
if knowledge
else None)

def split_if_valid(sub_task_descriptions_combined: list[str]) -> list[str] | None:
return (sub_task_descriptions_combined.split(sep=SUBTASK_HEADER, maxsplit=-1)[1:]
if sub_task_descriptions_combined.startswith(SUBTASK_HEADER)
else None)

sub_task_descriptions: list[str] = []
while not sub_task_descriptions:
sub_task_descriptions: list[str] = split_if_valid(
sub_task_descriptions_combined=self.lm.get_response(prompt=prompt, history=knowledge_lm_hist))

sub_htplanner: HTPlanner = replace(self, max_depth=self.max_depth - 1)

Expand Down