Building AI Agents with Phidata
Last Updated :
03 Feb, 2025
In this article, we’ll walk you through the process of building AI agents using Phidata, a powerful framework for creating intelligent agents. In this guide, we'll walk through the process of setting up a multi-agent system capable of web searching and financial analysis. The code provided demonstrates how to define individual agents and combine them into a cohesive unit.
Step 1: Setting Up the Environment
Before diving into building AI agents, you need to set up your development environment. This involves creating a virtual environment, installing the necessary dependencies, and organizing your project.
Create a Virtual Environment
To isolate your project dependencies, create a virtual environment using venv
or conda
. Follow the steps below:
- Create a new folder in your file manager (local machine)
- Open Anaconda navigator
- Launch VS code
- Open new terminal and type ( conda create -p venv python==3.12)
- Then click y to give consent
- Conda activate
- Add python extension
For a detailed guide, refer to our article on How to setup VS code for data science projects.
Step 2: Getting the Phidata API Key
To use Phidata, you’ll need a Phidata API key. If you don’t already have one, follow these steps:
- Visit the PHIDATA side and sign up for an account.
- Navigate to account settings, and find the API key option within the dashboard
- Copy the key and store it securely.
Step 3: Getting the Groq API Key
GROQ provides high-performance language models that power your AI agents. To get a GROQ API key:
- Visit the GroqCloud website and sign up for an account.
- Go to the Developers section and generate a free API key.
- Copy the key.
For a step-by-step guide, check out our article on How to Get a Groq API Key.
Step 4: Building and Running AI Agents
Now that your environment is set up and you have your API keys, it’s time to build and run your AI agents. Below is the Python code to create a web search agent, a financial agent, and a multi-agent system.
1. Install Required Packages
Create an file naming ; requirements.txt and Install the necessary python packages using pip
.
- phidata
- python-dotenv
- yfinance
- packaging
- duckduckgo-search
- fastapi
- uvicorn
- groq
- python-multipart
Required PackagesYou can install them together by giving the command in terminal:
pip install -r requirements.txt
2. Set Up Environment Variables
Now, store your API keys securely using environment variables. Create a .env
file in your project directory with the following content:
GROQ_API_KEY=your_groq_api_key_here
PHI_API_KEY=your_phi_api_key_here
Set Up Environment VariablesReplace your_groq_api_key_here and your_phi_api_key_here with your actual API keys.
3. Import Necessary Libraries
Begin by importing the required libraries in your Python script:
Python
from phi.agent import Agent
from phi.model.groq import Groq
from phi.tools.yfinance import YFinanceTools
from phi.tools.duckduckgo import DuckDuckGo
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
phi.agent
: provides the Agent
class, which is central to creating AI agents in Phidata.phi.model.groq
: allows integration with Groq models.phi.tools.yfinance
: provides tools for financial data analysis using the yfinance
library.phi.tools.duckduckgo
: enables web search capabilities using the DuckDuckGo search engine.os
: A standard Python library for interacting with the operating system.dotenv
: This library is used to load environment variables from a .env
file.
Step 4: Define Individual Agents
Create agents for specific tasks, such as web searching and financial analysis:
Python
web_search_agent = Agent(
name="Web Search Agent",
role="Search the web for information",
model=Groq(id="llama-3.3-70b-versatile"),
tools=[DuckDuckGo()],
instructions=["Provide the latest news", "Always include sources"],
show_tools_calls=True,
markdown=True,
)
In this configuration:
name
: Specifies the agent's name.role
: Defines the agent's purpose.model
: Specifies the Groq model to the agent.tools
: Lists the tools the agent can use, in this case, the DuckDuckGo search tool.instructions
: Provides guidelines for the agent's responses.show_tools_calls
: If set to True
, displays the tools called during the agent's operation.markdown
: If set to True
, formats the agent's responses in Markdown.
Python
finance_agent = Agent(
name="Financial AI Agent",
model=Groq(id="llama-3.3-70b-versatile"),
tools=[YFinanceTools(
stock_price=True,
analyst_recommendations=True,
stock_fundamentals=True,
company_news=True,
technical_indicators=True,
historical_prices=True,
)],
instructions=["Use tables to display the data"],
show_tool_calls=True,
markdown=True,
)
Same specifications are used, whereas; tools
: Provides the agent with financial data analysis tools from the YFinanceTools
module.
4. Combine Agents into a Multi-Agent System
Combine the web search and financial agents into a multi-agent system that can handle complex tasks.
Python
multi_ai_agent = Agent(
model=Groq(id="llama-3.3-70b-versatile"),
team=[web_search_agent, finance_agent],
instructions=["Always include sources", "Use tables to display the data"],
markdown=True,
debug_mode=True,
)
team
: A list of agents that work together to accomplish tasks.debug_mode
: Enables detailed logging for troubleshooting.
In this setup, the multi_ai_agent
can process requests that require both web search capabilities and financial data analysis.
4.5 Run the Multi-Agent System
Finally, use the multi-agent system to summarize analyst recommendations and fetch the latest news for a specific stock (e.g., NVDA).
Python
multi_ai_agent.print_response("Summarize analyst recommendations and share the latest news for NVDA", stream=True)
Output:
Building AI Agents with PhidataThe print_response
method executes the query and displays the result, streaming the output if stream=True
.
Building AI Agents with PhidataBy following the steps outlined, you've not only built agents capable of retrieving and analyzing financial data but also created a foundation for expanding into other domains.
The key takeaway is that Phidata simplifies the creation of AI agents by providing a modular and intuitive framework. Whether you’re building a single-purpose agent or a multi-agent system, Phidata’s flexibility allows you to integrate various tools and models seamlessly. Additionally, the use of Markdown formatting and real-time streaming ensures that your agents deliver clear, structured, and actionable insights.
Similar Reads
Utility-Based Agents in AI Artificial Intelligence has boomed in growth in recent years. Various types of intelligent agents are being developed to solve complex problems. Utility-based agents hold a strong position due to their ability to make rational decisions based on a utility function. These agents are designed to optim
9 min read
Perception in AI Agents Perception stands as a foundational concept in the realm of AI, enabling agents to glean insights from their environment through sensory inputs. From visual interpretation to auditory recognition, perceptions empower AI agents to make informed decisions, adapt to dynamic conditions, and interact mea
7 min read
Intelligent Agent in AI In the realm of AI, Intelligent Agents stand as pivotal entities, driving automation and decision-making with cognitive abilities. This article explores the concept, architecture, functionalities, and real-world applications of these agents, shaping the modern AI landscape. Table of Content Understa
7 min read
Rational Agent in AI Artificial Intelligence (AI) is revolutionizing our lives, from self-driving cars to personalized recommendations on streaming platforms. The concept of a rational agent is at the core of many AI systems. A rational agent is an entity that acts to achieve the best outcome, given its knowledge and ca
6 min read
Learning Agents in AI Learning agents are a shining example of scientific advancement in the field of artificial intelligence. This innovative approach to problem-solving puts an end to the static nature of classical planning by rejecting the conclusions based on the trivial pursuit of perfect knowledge. This article dis
4 min read
Types of Agents in AI Types of Agents in AI, agents are the entities that perceive their environment and take actions to achieve specific goals. These agents exhibit diverse behaviours and capabilities, ranging from simple reactive responses to sophisticated decision-making. This article explores the different types of A
11 min read