-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestration.py
More file actions
56 lines (46 loc) · 2.15 KB
/
Copy pathorchestration.py
File metadata and controls
56 lines (46 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from typing import Dict, List
from config import MAX_TASKS, MAX_DEPTH
from task_manager import TaskManager
from llm_interaction import a_transform_prompt, a_decompose_subtasks, a_select_tool
from task_execution import execute_task
async def a_generate_task_tree(prompt: str, schema: Dict, task_manager: TaskManager, max_depth: int = MAX_DEPTH):
# print(f"{prompt=}, {schema=}")
# print(f"{schema['$defs']['Task'].keys()=}")
# TODO: clean up
task = await a_transform_prompt(prompt, schema, "")
task["ingests"] = []
if not task:
raise Exception("Failed to generate task from user prompt")
task_queue = [(task, 0, None, "")]
root_task = None
tasks_by_depth = {}
while task_queue:
current_task, current_depth, parent_task, parent_context = task_queue.pop(0)
if task_manager.get_task_count() >= task_manager.max_tasks:
break
selected_tool = await a_select_tool(current_task, schema, current_depth, max_depth)
if not selected_tool:
continue
current_task['selected_tool'] = selected_tool
current_task['depth'] = current_depth
if not task_manager.add_task(current_task):
break
if root_task is None:
root_task = current_task
if parent_task:
if 'subtasks' not in parent_task:
parent_task['subtasks'] = []
parent_task['subtasks'].append(current_task)
if current_depth not in tasks_by_depth:
tasks_by_depth[current_depth] = []
tasks_by_depth[current_depth].append(current_task)
print(current_task)
if selected_tool == 'D': # Only decompose if "Mix of Tools" is selected
subtasks = await a_decompose_subtasks(current_task, schema, parent_context)
if subtasks:
new_parent_context = f"{parent_context}\nParent task: {current_task['task_description']}"
for subtask in subtasks:
task_queue.append((subtask, current_depth + 1, current_task, new_parent_context))
else:
current_task['result'] = await execute_task(current_task)
return root_task, tasks_by_depth