Implement Agentic Query Planning

implementationChallenge

Prompt Content

Building upon your LlamaIndex data pipeline, implement an agentic query engine capable of handling complex queries. The agent should be able to break down a high-level query like 'Analyze the strategic implications of the Musk vs. OpenAI lawsuit by summarizing key legal points and market reactions' into sub-queries. Utilize `QueryEngineTool` and `LlamaPack` or a custom `RouterQueryEngine` to orchestrate this process with GPT-4o. Show how the agent routes questions to specific sub-query engines or tools. Include Python code.

```python
from llama_index.core import VectorStoreIndex, Document
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.agent import AgentRunner
from llama_index.llms.openai import OpenAI
from llama_index.vector_stores.pinecone import PineconeVectorStore
# ... other necessary imports

# Assume 'legal_index' and 'news_index' are already created VectorStoreIndex instances
# backed by PineconeVectorStore

legal_query_engine = legal_index.as_query_engine(similarity_top_k=3)
news_query_engine = news_index.as_query_engine(similarity_top_k=5)

legal_tool = QueryEngineTool(query_engine=legal_query_engine, metadata=ToolMetadata(name='legal_analyzer', description='Provides summaries and context from legal documents and filings.'))
news_tool = QueryEngineTool(query_engine=news_query_engine, metadata=ToolMetadata(name='market_news_analyzer', description='Provides insights and sentiment from market news articles and reports.'))

llm = OpenAI(model='gpt-4o', api_key='YOUR_OPENAI_API_KEY')

# Your task: Initialize an agent (e.g., FunctionCallingAgentWorker or ReActAgent) with these tools
# and demonstrate how it handles a complex query.
# For example, using AgentRunner with a custom agent worker:
# agent = AgentRunner(your_agent_worker)
# response = agent.chat('Analyze the strategic implications of the Musk vs. OpenAI lawsuit...')
```

Try this prompt

Open the workspace to execute this prompt with free credits, or use your own API keys for unlimited usage.

Usage Tips

Copy the prompt and paste it into your preferred AI tool (Claude, ChatGPT, Gemini)

Customize placeholder values with your specific requirements and context

For best results, provide clear examples and test different variations