DEV Community

Cover image for Step-by-Step Guide to Building Domain-Specific AI Agents with Phidata SDK
Farhan Ahmad
Farhan Ahmad

Posted on

Step-by-Step Guide to Building Domain-Specific AI Agents with Phidata SDK

AI Agents are gaining a lot of popularity these days and businesses are rushing to build their own agents. Some prefer building an AI Agent from scratch, but what often bothers devs the most is the time is takes to build and test different AI Tools (for function calling). This can lead to a high development time.

However as time passes we're seeing new solutions popping up every now and then to help develoeprs streamline the process of building AI Agents. One such solution is Phidata.

What is Phidata?

Phidata SDK makes it super easy to build AI Agents in just a few lines of Python code. The best part is that it provides useful AI Tools right out of the box, so you don't need to write them from scratch yourself.

Some of the AI Tools Phidata offers out of the box:

  • Google Search, Exa (for Web Search)
  • Resend (to send Emails)
  • Crawl4AI and Firecrawl (to crawl the web)
  • DuckDB (for Data Analysis)
  • Python Agent (to write and run Python code)
  • Files (for reading files to build a RAG Agent)
  • GitHub (to interact with GitHub)
  • CalCom Agent (to schedule meetings using Cal.com)

and many more!
You can see an exhaustive list of Tools provided by Phidata here.

Using one or a combination of the above tools, we can build really complex and exciting AI Agents, like:

  • Data Analyst Agent
  • Research Agent
  • Sales Agent
  • Shopping Agent

just to name a few.

Creating Advanced Agents using "Team Agent"

Something cool about Phidata SDK is that you have the option to combine multiple Tools into a team, known as a "Team Agent". For example, you can create a team with the "DuckDuckGo" and "Yahoo Finance" Tools which can get the data from both sources, the web as well as Yahoo Finance.
This Team Agent will work the following way:

  • The user gives the following command to the Agent: "Summarize analyst recommendations and share the latest news for NVDA"
  • Our agent searches the web for the latest news on the company Nvidia
  • Agent also searches for the ticker "NVDA" on Yahoo Finance to get the company's financial data
  • Finally, our Agent presents the data from both sources (the Web and Yahoo Finance) in a nice table.

Pretty cool huh?

So now that we have some idea of what's possible using the Phidata SDK, let's go ahead and see how we can use it to create a simple Financial Analyst Agent.

Getting Started with Phidata SDK

Let's start building our Financial Analyst Agent. It's going to be super beginner-friendly so don't worry about falling behind.

We're going to create our Agent inside a Jupyter Notebook on Google Colab, which is very interactive and easy to share.

Step 1 - Create a new Notebook on Google Colab

Go to Google Colab using by clicking here and you should see this screen:
Go to Google Colab

Now click on the "New Notebook" button:
Click on New Notebook button

It may load for a while, but after that you should arrive at the newly created notebook which will look like this:
New Notebook created on Google Colab

Great, let's move to the next step.

Step 2 - Installing all required libraries in the Notebook

Before we can start creating our AI agent, we need to make sure we have the required dependencies available inside the Notebook. Note that Google Colab comes with some common libraries pre-installed in the Notebook, but to make sure we have all the libraries we need, we'll install all of them anyway.

We'll install the following libraries:

  • yfinance - To get company's financial data
  • openai - To allow Phidata to use OpenAI's LLM to make the AI agent come to life
  • duckduckgo-search - To search the web using DuckDuckGo
  • Phidata - To load up the pre-written AI Tools for function calling and to create a Team Agent.

To install these libraries, copy the below command and paste it inside the first cell block:

pip install openai yfinance duckduckgo-search phidata
Enter fullscreen mode Exit fullscreen mode

It should look like this:
Installing all libraries inside Google Colab Notebook

Next, click on the play icon on the left as shown below:
Run the cell

Now give it some time to install all dependencies. Once done installing, you should see a tiny green check mark on the left of the Run button like this:
Image description

Let's hide the cell's output since it's taking a lot of space in the Notebook. Click on the button right below the "Run" button and then click on "Show/hide output".
Hide the Cell output

Step 3 - Adding the OPENAI_API_KEY environment variable

Now we need to add the OpenAI API key to our environment. Go ahead and add a new cell to the Notebook by clicking on the button as shown below:
Add a new cell to Notebook

Now inside this new cell paste the following code and Run it. Replace the your_api_key value with your actual OpenAI API key that you can get from https://platform.openai.com/api-keys.

import os

os.environ['OPENAI_API_KEY'] = "your_api_key"
Enter fullscreen mode Exit fullscreen mode

It should look something like this:
Setting the OPENAI_API_KEY environment variable

Step 4 - Write code for the Agent

In this last step we'll write the actual code for the Agent. Since this is an "Agent Team" (which means that it is an AI agent made of multiple agents), we'll start by creating 2 agents using the Phidata SDK, namely web_agent and finance_agent. The web agent will search the web for news about the company and the finance agent will search Yahoo Finance for the company's financial data. Finally, we'll create a third Agent by passing these 2 agents to the "teams" array of this Agent, which will result in the creation of an "Agent Team".
This third Agent will be the final Agent that we'll use for getting a company's data from web and Yahoo Finance.

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools

web_agent = Agent(
    name="Web Agent",
    role="Search the web for information",
    model=OpenAIChat(id="gpt-4o"),
    tools=[DuckDuckGo()],
    instructions=["Always include sources"],
    show_tool_calls=True,
    markdown=True,
)

finance_agent = Agent(
    name="Finance Agent",
    role="Get financial data",
    model=OpenAIChat(id="gpt-4o"),
    tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
    instructions=["Use tables to display data"],
    show_tool_calls=True,
    markdown=True,
)

agent_team = Agent(
    team=[web_agent, finance_agent],
    instructions=["Always include sources", "Use tables to display data"],
    show_tool_calls=True,
    markdown=True,
)

agent_team.print_response("Summarize analyst recommendations and share the latest news for NVDA", stream=True)

Enter fullscreen mode Exit fullscreen mode

Add a new cell to your Notebook. Then copy the code above and paste it into the new cell.

That's pretty much it! Go ahead and run the cell. It will take some time to finish running and once it's done you'll be able to see the output as follows (you'll need to scroll to the bottom):

Financial Analysis done by AI Agent

The Output text looks really small because I had zoomed out in order to fit the entire output in a single screenshot.

So we were able to build this Financial Analyst Agent in a very short amount of time. It's obvious the report is sort of basic and could be more detailed, but we can always improve our Agent to add more data from different sources by adding new Agents to the Team (or by building our own function Tools from scratch).

You can Follow me on LinkedIn to learn more about AI Agents!

Top comments (0)