• unwind ai
  • Posts
  • Build a Team of AI Agents to Create an AI Financial Analyst

Build a Team of AI Agents to Create an AI Financial Analyst

Multi-agent app with web access in just 20 lines of Python Code (step-by-step instructions)

AI tools are changing how we handle financial data, and building a team of AI agents that can act as financial analysts makes it even better.

This guide shows you how to set up a multi-agent financial analyst system using GPT-4o in just 20 lines of Python code. The system integrates real-time web search and financial data analysis as A agents work together to deliver meaningful financial insights quickly.

We are using Phidata, a framework designed for building agent-based systems to streamline the entire setup.

🎁 $50 worth AI Bonus Content at the end!

What We’re Building

This script demonstrates how to build a team of AI agents that work together as a financial analyst using GPT-4o in just 20 lines of Python code. The system combines web search capabilities with financial data analysis tools to provide comprehensive financial insights.

Features:

  • Multi-agent system with specialized roles:

    • Web Agent for general internet research

    • Finance Agent for detailed financial analysis

    • Team Agent for coordinating between agents

  • Real-time financial data access through YFinance

  • Web search capabilities using DuckDuckGo

  • Persistent storage of agent interactions using SQLite

Prerequisites

Before we begin, make sure you have:

  1. Python installed on your machine (version 3.7 or higher is recommended)

  2. Your OpenAI API Key (or another LLM API key you want to use)

  3. Basic familiarity with Python programming

  4. A code editor of your choice (we recommend VS Code or PyCharm for their excellent Python support)

Step-by-Step Instructions

Setting Up the Environment

First, let's get our development environment ready:

  1. Clone the GitHub repository:

git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
  1. Go to the ai_finance_agent_team folder:

cd ai_finance_agent_team
pip install -r requirements.txt
  1. Get your OpenAI API Key: Sign up for an OpenAI account (or the LLM provider of your choice) and obtain your API key. Set your OpenAI API key as an environment variable:

export OPENAI_API_KEY='your-api-key-here'

Creating the App

Let’s create our app. Create a new file finance_agent_team.py and add the following code:

  1. Import Required Libraries: 
    • Phi framework for agent creation

    • OpenAI chat model integration

    • SQLite storage for agent history

    • Tools for web search and financial data

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.storage.agent.sqlite import SqlAgentStorage
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools
from phi.playground import Playground, serve_playground_app
  1. Let's create the Web AI Agent first:

    • Specialized for web searches

    • Uses DuckDuckGo as a search tool

    • Stores conversation history in SQLite

    • Enables markdown output

web_agent = Agent(
    name="Web Agent",
    role="Search the web for information",
    model=OpenAIChat(id="gpt-4o"),
    tools=[DuckDuckGo()],
    storage=SqlAgentStorage(table_name="web_agent", db_file="agents.db"),
    add_history_to_messages=True,
    markdown=True,
)
  1. Let's create the AI Finance Agent now:

    • Specialized for financial data

    • Uses YFinance tools for market data

    • Configured to display data in tables

    • Maintains its own conversation history

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, company_news=True)],
    instructions=["Always use tables to display data"],
    storage=SqlAgentStorage(table_name="finance_agent", db_file="agents.db"),
    add_history_to_messages=True,
    markdown=True,
)
  1. Combine the agent to create an AI Agent Team:

    • Combines web and finance agents

    • Shows tool calls for transparency

    • Uses GPT-4o as the coordinator

agent_team = Agent(
    team=[web_agent, finance_agent],
    name="Agent Team (Web+Finance)",
    model=OpenAIChat(id="gpt-4o"),
    show_tool_calls=True,
    markdown=True,
)
  1. Setting up the Playground:

    • Creates an interactive playground

    • Includes our agent team

app = Playground(agents=[agent_team]).get_app()
  1. Finally, we serve the Playground app:

    • Serves the Playground app

    • Enables hot reloading for development

if __name__ == "__main__":
    serve_playground_app("finance_agent_team:app", reload=True)

How the Code Works

  • Web Agent: Uses DuckDuckGo to search the web and stores search results in SQLite.

  • Finance Agent: Fetches real-time stock prices, news, and other financial data from YFinance. It also formats data into tables for easier understanding.

  • Team Agent: Coordinates interactions between the Web and Finance Agents, making sure they work together efficiently.

  • Playground Interface: The Playground app allows you to test and interact with the agents directly through a browser.

Running the App

With our code in place, it's time to launch the app.

  • Before running the app, authenticate using 'phi auth' in your terminal. Then, navigate to the project folder, and run the following command

python3 finance_agent_team.py
  • Open your web browser and navigate to the URL provided in the console output to interact with your multi-agent app through the playground interface.

Working Application Demo

Conclusion

And you’ve just built a multi-agent financial analyst system powered by GPT-4o. This tool is more than just a chatbot—it brings together real-time financial data, web search, and persistent memory to deliver actionable insights in one place. With only a few lines of Python, you’ve created a team of AI agents capable of handling complex financial queries.

This setup can now be expanded further:

  • Track multiple portfolios: Add features to monitor various stock portfolios in real-time.

  • Automate alerts: Set up automatic alerts for stock price changes, news updates, or specific market events.

Keep experimenting and refining to build even smarter AI solutions!

We share hands-on tutorials like this 2-3 times a week, to help you stay ahead in the world of AI. If you're serious about levelling up your AI skills and staying ahead of the curve, subscribe now and be the first to access our latest tutorials.

Bonus worth $50 💵💰

Share this newsletter on your social channels and tag Unwind AI (X, LinkedIn, Threads, Facebook) to get AI resource pack worth $50 for FREE. Valid for limited time only!

Reply

or to participate.