To achieve the sophisticated Master Planner system you've envisioned, we'll need to extend and integrate the existing codebase you provided. Here's a comprehensive plan outlining the necessary components, modifications, and new implementations required to realize this functionality.
- High-Level Architecture
- Key Components
- Detailed Implementation
- Integrating with Existing Code
- Example Code Snippets
- Workflow Overview
- Considerations and Best Practices
The proposed system consists of several interacting agents, each responsible for different aspects of task management and execution:
- Master Planner Agent: Orchestrates the overall task by breaking it down into steps, selecting appropriate tools, and managing sub-agents.
- Prompt Creator Agent: Generates optimized prompts for each step, incorporating self-reflection and advanced techniques.
- Step Agents: Execute individual steps of the task using assigned tools and prompts.
- RAG (Retrieval-Augmented Generation) System: Stores and retrieves agent descriptions and metadata for reuse.
- User Interface: Facilitates user interactions, approvals, and customizations.
-
Agent Management:
- Creation, configuration, and storage of agent instances.
- Reusability of agents through RAG.
-
Tool Selection:
- Based on task requirements, selecting appropriate tools from the available list.
-
Prompt Generation:
- Crafting effective prompts for each task step, utilizing self-reflection.
-
Inter-Agent Communication:
- Mechanism for agents to send messages to each other.
-
User Interaction:
- Approval processes for agent plans.
- Customization of prompts and tool selections by the user.
Responsibilities:
- Parse the high-level task.
- Break down the task into actionable steps.
- Select tools for each step.
- Create or reuse step agents with appropriate prompts and tools.
- Initiate and manage the execution flow.
Responsibilities:
- Generate effective prompts for each task step.
- Incorporate self-reflection and other advanced techniques to optimize prompt quality.
Responsibilities:
- Dynamically create agents with specified prompts and tools.
- Store agent metadata in the RAG system for future reuse.
- Manage the lifecycle of agents, including activation and deactivation.
Responsibilities:
- Facilitate messaging between agents.
- Enable agents to send tasks, updates, or requests to other agents in the plan.
Responsibilities:
- Maintain a database of existing agents with their descriptions and metadata.
- Retrieve relevant agents based on task similarity to promote reuse.
Responsibilities:
- Present generated plans and agent configurations to the user for approval.
- Allow users to customize prompts and tool selections before execution.
- Finalize agent cohorts for autonomous task handling based on user satisfaction.
Your existing codebase provides a solid foundation for tool management, dialog handling, and utility functions. We'll build upon these to implement the Master Planner system.
tools.py
: Define new tools required for agent management and inter-agent communication.leggo.py
: Extend functionalities to include agent orchestration.dialog.py
: Utilize existing dialog structures for inter-agent and user communications.vk.py
: Manage tool queues and current tool states, which can be adapted for agent management.func_utils.py
: Leverage utility functions for dynamic function handling and type management.
Below are example implementations to guide the development of the Master Planner system.
File Path: src/ayy/leggo.py
# Within leggo.py
def create_master_planner(valkey_client: Valkey, creator: Instructor | AsyncInstructor) -> Dialog:
"""
Initializes the Master Planner agent.
"""
master_planner_tool = Tool(
chain_of_thought="Master Planner responsible for orchestrating tasks.",
name="master_planner",
prompt="You are the Master Planner. Break down the task into steps and assign tools.",
)
get_selected_tools(valkey_client, [master_planner_tool])
dialog = Dialog(system="Master Planner initialized.")
return run_selected_tool(valkey_client, creator, dialog, master_planner_tool)
File Path: src/ayy/tools.py
# Add Prompt Creator Tool
def create_prompt(step_description: str) -> str:
"""
Generates a refined prompt for a given step.
Incorporates self-reflection techniques.
"""
prompt_creator_tool = Tool(
chain_of_thought="Prompt Creator for generating optimized prompts.",
name="prompt_creator",
prompt=f"Generate an effective prompt for the following step: {step_description}",
)
return prompt_creator_tool
File Path: src/ayy/vk.py
# Add functions to manage agent metadata in RAG
def store_agent_metadata(valkey_client: Valkey, agent_metadata: dict) -> None:
"""
Stores agent metadata in the RAG system.
"""
rag_key = "agent_rag"
existing_rag = valkey_client.get(rag_key)
rag = dill.loads(existing_rag) if existing_rag else {}
rag[agent_metadata["id"]] = agent_metadata
valkey_client.set(rag_key, dill.dumps(rag))
def retrieve_matching_agents(valkey_client: Valkey, task_description: str) -> list[dict]:
"""
Retrieves agents from RAG that match the task description.
"""
rag_key = "agent_rag"
existing_rag = valkey_client.get(rag_key)
rag = dill.loads(existing_rag) if existing_rag else {}
# Simple matching based on keyword overlap; can be enhanced with embeddings
matches = [
metadata for metadata in rag.values()
if any(keyword.lower() in task_description.lower() for keyword in metadata.get("keywords", []))
]
return matches
File Path: src/ayy/dialog.py
# Extend Dialog model if necessary to include agent metadata
class Dialog(BaseModel):
system: Content = ""
messages: Messages = Field(default_factory=list)
model_name: str = MODEL_NAME
creation_config: dict = dict(temperature=TEMPERATURE, max_tokens=MAX_TOKENS)
memory_tags: list[Literal["core", "recall"]] = Field(default_factory=list)
agent_id: str | None = None # Optional field to link to agent metadata
File Path: src/ayy/leggo.py
# Add method for user approval
def get_user_approval(valkey_client: Valkey, creator: Instructor | AsyncInstructor, dialog: Dialog, proposed_agents: list[dict]) -> bool:
"""
Presents proposed agents to the user for approval.
Allows customization of prompts and tools.
"""
approval_prompt = "Please review the proposed agents for this task:\n"
for agent in proposed_agents:
approval_prompt += f"Agent ID: {agent['id']}\nDescription: {agent['description']}\nPrompt: {agent['prompt']}\nTools: {agent['tools']}\n\n"
approval_prompt += "Do you approve these agents? (yes/no)"
approval_tool = Tool(
chain_of_thought="User approval for agent setup.",
name="ask_user",
prompt=approval_prompt,
)
dialog = run_selected_tool(valkey_client, creator, dialog, approval_tool)
# Check last user message for approval
if dialog.messages and dialog.messages[-1]["role"] == "user":
response = dialog.messages[-1]["content"].strip().lower()
return response in ["yes", "y"]
return False
-
Task Initiation:
- User provides a high-level task.
- Master Planner Agent receives the task and begins orchestration.
-
Task Breakdown:
- Master Planner decomposes the task into discrete steps.
- For each step, it interacts with the Prompt Creator to generate optimized prompts.
-
Agent Assignment:
- For each step, Master Planner selects or creates a Step Agent.
- Checks RAG to reuse existing agents if applicable.
- If reusing, retrieves agent configurations; otherwise, creates new agents and stores their metadata in RAG.
-
User Approval:
- Before executing the plan, presents the proposed agents and their configurations to the user.
- User reviews and approves or customizes the agents.
-
Execution:
- Once approved, the Master Planner initiates the Step Agents.
- Agents execute their assigned steps using the selected tools and prompts.
- Agents can communicate with each other as needed via inter-agent communication tools.
-
Autonomy and Learning:
- After successful execution and user satisfaction, the agent cohort is stored in RAG.
- Future similar tasks can leverage this cohort for faster and more efficient execution without recreating agents.
- Scalability: Ensure that agent creation and management are efficient to handle multiple concurrent tasks.
- Security: Validate and sanitize all inputs, especially those involving user interactions and external tool integrations.
- Extensibility: Design the system to easily incorporate new tools and agent types as needed.
- User Experience: Streamline the approval and customization processes to be intuitive and non-intrusive.
- Error Handling: Implement robust error handling to manage failures in agent creation, tool execution, or inter-agent communication.
- Logging and Monitoring: Maintain detailed logs to monitor agent activities and diagnose issues.
By following this plan and integrating the provided code snippets, you can develop a robust Master Planner system that orchestrates complex tasks, leverages reusable agents, and incorporates user feedback for continuous improvement.