Building Your First Agentic AI Financial Assistant with Phidata - A Complete Tutorial
Hello everyone! This is Krish, and today we're going to build something really exciting - an end-to-end agentic AI application using Phidata. This is going to be a complete tutorial where we'll create a financial analysis assistant that can analyze stocks, provide recommendations, and much more.
What is Phidata and Why Should You Care?
Before we dive into the coding part, let me explain what Phidata is. Phidata is an amazing open-source framework that allows us to:
- Build autonomous AI agents that can work independently
- Create complex multi-agent systems
- Integrate with various LLM providers (OpenAI, Anthropic, AWS Bedrock, Groq, etc.)
- Deploy and monitor our agents in production
- Add custom tools and knowledge bases to our agents
Project Prerequisites
Before we start, make sure you have the following:
- Python 3.12 installed on your system
- Basic understanding of Python and APIs
- API keys for Groq (we'll use this as our LLM provider)
- Phidata API key (you can get this from their platform)
Project Structure
financial_assistant/
βββ .env
βββ requirements.txt
βββ financial_agent.py
βββ playground.py
Step 1: Setting Up the Environment
First, let's create our virtual environment and install the required packages:
# Create virtual environment
conda create -p venv python=3.12
conda activate venv
# Create requirements.txt with following content:
phidata
python-dotenv
yfinance
duckduckgo-search
fastapi
uvicorn
groq
# Install requirements
pip install -r requirements.txt
Step 2: Setting Up Environment Variables
Create a .env file with your API keys:
PHIDATA_API_KEY=your_phidata_key
GROQ_API_KEY=your_groq_key
Step 3: Creating Our First Agent - Web Search Agent
This agent will be responsible for fetching latest news and information from the web:
from phi.agent import Agent
from phi.tools.duckduckgo import DuckDuckGoSearchTool
web_search_agent = Agent(
name="Web Search Agent",
role="Search the web for latest financial news and information",
model=Groq(model="llama3-8b-preview"),
tools=[DuckDuckGoSearchTool()],
instructions=[
"Always include sources in your response",
"Focus on financial news and market analysis",
"Provide recent and relevant information"
]
)
Step 4: Creating the Financial Analysis Agent
This agent will handle stock analysis and recommendations:
from phi.tools.yfinance import YFinanceTool
finance_agent = Agent(
name="Finance AI Agent",
role="Analyze stocks and provide financial recommendations",
model=Groq(model="llama3-8b-preview"),
tools=[YFinanceTool(
analyze_recommendation=True,
company_news=True,
technical_indicators=True,
stock_fundamentals=True
)],
instructions=[
"Present data in organized tables",
"Provide both technical and fundamental analysis",
"Include risk factors in recommendations"
]
)
Step 5: Creating the Multi-Agent System
Now, let's combine both agents to create a powerful financial assistant:
multi_agent = Agent(
name="Financial Assistant",
team=[web_search_agent, finance_agent],
instructions=[
"Combine web information with financial data",
"Provide comprehensive analysis",
"Present information in a clear, structured format",
"Always include both technical and fundamental factors"
],
markdown=True,
show_tool_calls=True
)
Step 6: Creating the Interactive Playground
from phi.app import Playground
from phi.app.serve import serve_playground_app
app = Playground(
agents=[finance_agent, web_search_agent],
title="Financial Analysis Assistant",
description="AI-powered stock analysis and market insights"
).get_app()
if __name__ == "__main__":
serve_playground_app(app=app, reload=True)
How to Use the Financial Assistant
Once your playground is running, you can ask questions like:
- "Analyze Tesla stock and provide latest news and recommendations"
- "Compare NVIDIA and AMD stocks with technical analysis"
- "What are the top performing tech stocks this week?"
Advanced Features and Capabilities
Custom Knowledge Integration
You can enhance your agents by adding custom knowledge bases:
from phi.knowledge import Knowledge
financial_knowledge = Knowledge(
sources=["path/to/financial_docs"],
description="Expert financial analysis guidelines"
)
# Add to agent
finance_agent.add_knowledge(financial_knowledge)
Production Deployment
For production deployment, Phidata provides several options:
- Docker containerization
- Cloud deployment (AWS, GCP, Azure)
- Monitoring and logging capabilities
- API endpoint creation
Common Issues and Solutions
- API Key Issues: Always ensure your environment variables are properly set
- Memory Usage: Monitor RAM usage when running multiple agents
- Rate Limits: Be aware of API rate limits from different providers
Conclusion
Guys, this is just the beginning of what we can do with Phidata. In upcoming videos, we'll explore more complex workflows and integrations. Don't forget to like, share, and subscribe to my channel for more such content!
Next Steps
- Experiment with different LLM providers
- Add more specialized tools and knowledge bases
- Create industry-specific agents
- Explore advanced monitoring and deployment options