Open In App

Building AI Agents with Phidata

Last Updated : 03 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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:

  1. Create a new folder in your file manager (local machine)
  2. Open Anaconda navigator
  3. Launch VS code
  4. Open new terminal and type ( conda create -p venv python==3.12)
  5. Then click y to give consent
  6. Conda activate
  7. 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:

  1. Visit the PHIDATA side and sign up for an account.
  2. Navigate to account settings, and find the API key option within the dashboard
  3. 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:

  1. Visit the GroqCloud website and sign up for an account.
  2. Go to the Developers section and generate a free API key.
  3. 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
requirements
Required Packages

You 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

APIkeys
Set Up Environment Variables

Replace 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:

  • Web Search Agent:
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.
  • Financial Agent:
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:

agent
Building AI Agents with Phidata

The print_response method executes the query and displays the result, streaming the output if stream=True.

agent-
Building AI Agents with Phidata

By 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