• unwind ai
  • Posts
  • Build AI Investment Agent with GPT-4o 💸

Build AI Investment Agent with GPT-4o 💸

LLM App in just 15 lines of Python Code (step-by-step instructions)

Are you interested in comparing stock performance with the power of AI, but don’t want to rely on expensive software? Well, today is your lucky day.

In this tutorial, we’ll create an AI-powered investment agent that runs locally, compares two stocks, and generates a detailed report—entirely for free!

What We’re Building

We’re creating a web-based AI investment agent using the GPT-4o language model and Yahoo Finance data. This application will help you make informed decisions by comparing the performance of two stocks, fetching news, company info, and analyst recommendations—all running on your local machine.

Prerequisites

Before we begin, make sure you have:

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

  2. Basic familiarity with Python programming

  3. Your OpenAI API Key (or an alternative LLM provider’s API key)

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

Code Walkthrough: Step-by-Step Instructions

Step 1: 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. Install the required dependencies:

pip install -r requirements.txt
  1. Get Your OpenAI API Key:

    Sign up for an OpenAI account and generate an API key.

Step 2: Creating the Streamlit App

Now that the environment is set, let’s create our Streamlit app. Create a new file named investment_agent.py and add the following code:

  • Import Required Libraries: At the top of your file, add:

import streamlit as st
from phi.assistant import Assistant
from phi.llm.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools
  • Set Up the Streamlit App: Define the structure of the Streamlit app by adding the following code:

st.title("AI Investment Agent 📈🤖")
st.caption("This app allows you to compare the performance of two stocks and generate detailed reports.")
  • Get OpenAI API Key from the User: Add an input field to accept the OpenAI API key from the user:

openai_api_key = st.text_input("OpenAI API Key", type="password")
  • Create the Assistant Instance: Once a valid API key is provided, initialize the Assistant with GPT-4o and set up the Yahoo Finance tools:

if openai_api_key:
    # Create an instance of the Assistant
    assistant = Assistant(
        llm=OpenAIChat(model="gpt-4o", api_key=openai_api_key),
        tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],
        show_tool_calls=True,
    )
  • Add Stock Input Fields: Create input fields to accept two stock symbols for comparison:

    stock1 = st.text_input("Enter the first stock symbol")
    stock2 = st.text_input("Enter the second stock symbol")
  • Generate the Comparison Report: Once the user enters two stock symbols, the app will query the AI model for a detailed comparison:

    if stock1 and stock2:
        # Get the response from the assistant
        query = f"Compare {stock1} to {stock2}. Use every tool you have."
        response = assistant.run(query, stream=False)
        st.write(response)

Step 3: Running the App

With our code in place, it's time to launch the app and start comparing stocks.

  • Start the Streamlit App In your terminal, navigate to the project folder, and run the following command:

streamlit run investment_agent.py
  • Access Your AI Assistant: Streamlit will provide a local URL (typically http://localhost:8501). Open this in your web browser and start comparing stocks by entering their symbols.

Conclusion

Congratulations! You’ve just built an AI-powered investment agent using GPT-4o and Yahoo Finance data. This tool will allow you to easily compare the performance of two stocks, fetch news, and provide analyst recommendations, all from the comfort of your local machine.

This is a great start, but there’s always room for improvement. You can expand the app’s functionality, use different LLM models, or even add more financial tools to make the application even more insightful.

If you enjoyed this tutorial, make sure to subscribe to Unwind AI for more tutorials like this. And don’t forget to share this guide with your friends!

Reply

or to participate.