Plan Agent
Goal
Given a context, create and optimise a plan
User story
- I want to create a plan so that I can structure and sequence my workflow
- I want to optimise a plan so that my workflow is cheaper, faster or better
Variations
- create and optimise a query plan
- create and optimise a task plan
Note
Plan agents don't execute plan actions or answer plan questions. That requires a separate agent or workflow.
Query plan
A query plan breaks down a parent question into sub-questions. I find there are two ways to do this. A single step is often sufficient to generate a query plan. Optionally, adding two further steps - generating a list of suggestions then implementing them to revise the query plan - results in a more refined and precise output.
Step 1
The single step query plan below uses the following group of Pydantic classes
Code
this is code
name = "gist"
def uppercase(name):
upper = name.upper
return upper
class QueryType(str, enum.Enum):
"""Enumeration representing the types of queries that can be asked to a question answer system."""
SINGLE_QUESTION = "SINGLE"
MERGE_MULTIPLE_RESPONSES = "MERGE_MULTIPLE_RESPONSES"
class QueryType(str, enum.Enum):
"""Enumeration representing the types of queries that can be asked to a question answer system."""
SINGLE_QUESTION = "SINGLE"
MERGE_MULTIPLE_RESPONSES = "MERGE_MULTIPLE_RESPONSES"
class Query(BaseModel):
"""Class representing a single question in a query plan."""
id: int = Field(..., description="Unique id of the query")
question: str = Field(
...,
description="Question asked using a question answering system",
)
dependencies: List[int] = Field(
default_factory=list,
description="List of sub questions that need to be answered before asking this question",
)
node_type: QueryType = Field(
default=QueryType.SINGLE_QUESTION,
description="Type of question, either a single question or a multi-question merge",
)
class QueryPlan(BaseModel):
"""Container class representing a tree of questions to ask a question answering system."""
query_graph: List[Query] = Field(
..., description="The query graph representing the plan"
)
def _dependencies(self, ids: List[int]) -> List[Query]:
"""Returns the dependencies of a query given their ids."""
return [q for q in self.query_graph if q.id in ids]
Example
Input : System Prompt
You are a world class query planning algorithm capable of breaking apart questions into its dependency queries such that the answers can be used to inform the parent question.Do not answer the questions, simply provide a correct compute graph with good specific questions to ask and relevant dependencies. Your compute graph should both decompose and recompose queries in order to answer the parent question. Before you call the function, think step-by-step to get a better understanding of the problem.
Input : User Prompt
Consider the following question : How have Japanese and Chinese tea ceremonies impacted the cultivation and production techniques of Matcha and Pu-erh, and how has globalisation altered these ancient traditions?.
Generate the correct, complete query plan to answer this parent question
"Output"
{'query_graph': [{'id': 1,
'question': 'What are the characteristics of Japanese tea ceremonies?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 2,
'question': 'What are the characteristics of Chinese tea ceremonies?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 3,
'question': 'What are the cultivation and production techniques of Matcha?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 4,
'question': 'What are the cultivation and production techniques of Pu-erh?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 5,
'question': 'How have Japanese tea ceremonies influenced the cultivation and production techniques of Matcha?',
'dependencies': [1, 3],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 6,
'question': 'How have Chinese tea ceremonies influenced the cultivation and production techniques of Pu-erh?',
'dependencies': [2, 4],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 7,
'question': 'How has globalization impacted Japanese tea ceremonies?',
'dependencies': [1],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 8,
'question': 'How has globalization impacted Chinese tea ceremonies?',
'dependencies': [2],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 9,
'question': 'How has globalization influenced the cultivation and production techniques of Matcha?',
'dependencies': [3],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 10,
'question': 'How has globalization influenced the cultivation and production techniques of Pu-erh?',
'dependencies': [4],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 11,
'question': 'How have Japanese and Chinese tea ceremonies impacted the cultivation and production techniques of Matcha and Pu-erh?',
'dependencies': [5, 6],
'node_type': <QueryType.MERGE_MULTIPLE_RESPONSES: 'MERGE_MULTIPLE_RESPONSES'>},
{'id': 12,
'question': 'How has globalization altered these ancient traditions?',
'dependencies': [7, 8],
'node_type': <QueryType.MERGE_MULTIPLE_RESPONSES: 'MERGE_MULTIPLE_RESPONSES'>},
{'id': 13,
'question': 'Merge final findings: How have Japanese and Chinese tea ceremonies impacted the cultivation and production techniques of Matcha and Pu-erh, and how has globalization altered these ancient traditions?',
'dependencies': [11, 9, 10, 12],
'node_type': <QueryType.MERGE_MULTIPLE_RESPONSES: 'MERGE_MULTIPLE_RESPONSES'>}]}
Step 2
As the name implies, the suggestion list class generates a list of improvements to the compute graph without implementing the recommendations.
class SuggestionList(BaseModel):
"""A detailed list of suggestions to optimise the query plan"""
suggestions: List[str]
Example
Input : System Prompt
You are a highly sophisticated query optimization algorithm designed to enhance the quality of query plans. Your primary function is not to answer the queries but to meticulously improve the query graph. This involves ensuring each step is clearly defined and leverages detailed dependencies to construct a comprehensive and precise query sequence. Before making any modifications, thoroughly evaluate the existing query plan to identify opportunities where additional or more detailed questions or steps could enhance clarity or add value. Your output should detail an optimised query graph where each question is well-defined and logically sequenced based on its dependencies. Ensure no loss of necessary detail and increase the questions or steps where it adds to the robustness and precision of the information-gathering process.
Input : User Prompt
[Add in the query plan generated earlier]
Output
- Insert a guiding node comparing the overall tea ceremonies before discussing their influence on cultivation and production techniques.
- Add nodes to analyze broad categories for different types of tea other than Matcha and Pu-erh to provide context about other tea influences.
- Introduce a node discussing significant historical periods impacting tea ceremonies and their relations before globalization discussion.
- Implement query nodes which explore the more contemporary aspects of tea ceremonies, delving into modernization and adaptation in global contexts.
- Create more detailed dependency links between globalization impacts on tea ceremonies and specific changes in cultivation techniques.
Step 3
After generating the list of suggestions, send this back to the LLM along with the query plan to generate a revised plan. It uses the same Pydantic class as the original Query Plan for structuring the response.
Example
Input : System prompt
[Same as system prompt in Multi step A]
Input : User Prompt
- Consider the original parent question : {insert question here}
- Consider the list of suggestions for optimising the query plan: {insert list of suggestions here}.
- Apply them to improve the original query plan : {insert original query plan here}.
- Generate a revised, optimised query plan to answer the parent question.
- Query Ids and dependencies should be listed sequentially.
Output
{'query_graph': [{'id': 1,
'question': 'What are the characteristics of Japanese tea ceremonies?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 2,
'question': 'What are the characteristics of Chinese tea ceremonies?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 3,
'question': 'What are the cultivation and production techniques of Matcha?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 4,
'question': 'What are the cultivation and production techniques of Pu-erh?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 5,
'question': 'A comparison of Japanese and Chinese tea ceremonies.',
'dependencies': [1, 2],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 6,
'question': 'What are the cultivation and production techniques of other significant tea types?',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 7,
'question': 'Historical overview of significant periods impacting tea ceremonies.',
'dependencies': [],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 8,
'question': 'How have Japanese tea ceremonies influenced the cultivation and production techniques of Matcha?',
'dependencies': [1, 3],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 9,
'question': 'How have Chinese tea ceremonies influenced the cultivation and production techniques of Pu-erh?',
'dependencies': [2, 4],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 10,
'question': 'How has globalization impacted Japanese tea ceremonies?',
'dependencies': [1],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 11,
'question': 'How has globalization impacted Chinese tea ceremonies?',
'dependencies': [2],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 12,
'question': 'How has globalization influenced the cultivation and production techniques of Matcha?',
'dependencies': [3],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 13,
'question': 'How has globalization influenced the cultivation and production techniques of Pu-erh?',
'dependencies': [4],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 14,
'question': 'How have Japanese and Chinese tea ceremonies impacted the cultivation and production techniques of Matcha and Pu-erh?',
'dependencies': [8, 9],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 15,
'question': 'How has globalization altered these ancient traditions?',
'dependencies': [10, 11],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 16,
'question': 'Modernization and adaptation in global contexts related to tea ceremonies.',
'dependencies': [15],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 17,
'question': 'Linking globalization impacts on tea ceremonies to specific changes in cultivation techniques.',
'dependencies': [12, 13, 14],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>},
{'id': 18,
'question': 'Merge final findings: How have Japanese and Chinese tea ceremonies impacted the cultivation and production techniques of Matcha and Pu-erh, and how has globalization altered these ancient traditions?',
'dependencies': [14, 16, 17],
'node_type': <QueryType.SINGLE_QUESTION: 'SINGLE'>}]}