Create a Personal Finance Agent with GPT-4o

As you know we can create tonnes of apps and other tools with AI. Today, in this post I will share a guide on creating an AI Personal Finance Agent with GPT-4o with Python code.

As mentioned, we will be using Python programming language to build our Personal Finance Agent with GPT-4o, so you need to download Python first. You can head to the Python official website and download the Python version according to your system (like Windows, Mac, or Linux).

You can get the tutorial on installing Python on YouTube and VS Code (recommend) or any other IDE (PyCharm).

Let’s get back to the topic and follow the below steps-

1. You need to Install the necessary Python Libraries Run the following commands from your terminal to install the required libraries:

2. Import necessary libraries

  • Streamlit for building the web app
  • phi for building AI agents and tools
  • OpenAI for using GPT-4
  • SerpAPI for web search functionality

3. Set up the Streamlit App

  • Add a title to the app using ‘st.title()’
  • Add a description for the app using ‘st.caption()’

4. Create and Initialize the AI Assistants Create instances of Assistants:

  • Researcher: Searches for financial advice, investment opportunities, and savings strategies based on user preferences
  • Planner: Generates a personalized plan based on user preferences.

5. Get User Input and Generate Financial Plan

  • Create text inputs for the user to enter their financial goals and current financial situation.
  • When the button is clicked, run the Planner assistant to generate the financial plan and display it using ‘st.write()’

Full Source Code

from textwrap import dedent
from phi.assistant import Assistant
from phi.tools.serpapi_tools import SerpApiTools
import streamlit as st
from phi.llm.openai import OpenAIChat

# Set up the Streamlit app
st.title("AI Personal Finance Planner đź’°")
st.caption("Manage your finances with AI Personal Finance Manager by creating personalized budgets, investment plans, and savings strategies using GPT-4o")

# Get OpenAI API key from user
openai_api_key = st.text_input("Enter OpenAI API Key to access GPT-4o", type="password")

# Get SerpAPI key from the user
serp_api_key = st.text_input("Enter Serp API Key for Search functionality", type="password")

if openai_api_key and serp_api_key:
    researcher = Assistant(
        name="Researcher",
        role="Searches for financial advice, investment opportunities, and savings strategies based on user preferences",
        llm=OpenAIChat(model="gpt-4o", api_key=openai_api_key),
        description=dedent(
            """\
        You are a world-class financial researcher. Given a user's financial goals and current financial situation,
        generate a list of search terms for finding relevant financial advice, investment opportunities, and savings strategies.
        Then search the web for each term, analyze the results, and return the 10 most relevant results.
        """
        ),
        instructions=[
            "Given a user's financial goals and current financial situation, first generate a list of 3 search terms related to those goals.",
            "For each search term, `search_google` and analyze the results.",
            "From the results of all searches, return the 10 most relevant results to the user's preferences.",
            "Remember: the quality of the results is important.",
        ],
        tools=[SerpApiTools(api_key=serp_api_key)],
        add_datetime_to_instructions=True,
    )
    planner = Assistant(
        name="Planner",
        role="Generates a personalized financial plan based on user preferences and research results",
        llm=OpenAIChat(model="gpt-4o", api_key=openai_api_key),
        description=dedent(
            """\
        You are a senior financial planner. Given a user's financial goals, current financial situation, and a list of research results,
        your goal is to generate a personalized financial plan that meets the user's needs and preferences.
        """
        ),
        instructions=[
            "Given a user's financial goals, current financial situation, and a list of research results, generate a personalized financial plan that includes suggested budgets, investment plans, and savings strategies.",
            "Ensure the plan is well-structured, informative, and engaging.",
            "Ensure you provide a nuanced and balanced plan, quoting facts where possible.",
            "Remember: the quality of the plan is important.",
            "Focus on clarity, coherence, and overall quality.",
            "Never make up facts or plagiarize. Always provide proper attribution.",
        ],
        add_datetime_to_instructions=True,
        add_chat_history_to_prompt=True,
        num_history_messages=3,
    )

    # Input fields for the user's financial goals and current financial situation
    financial_goals = st.text_input("What are your financial goals?")
    current_situation = st.text_area("Describe your current financial situation")

    if st.button("Generate Financial Plan"):
        with st.spinner("Processing..."):
            # Get the response from the assistant
            response = planner.run(f"Financial goals: {financial_goals}, Current situation: {current_situation}", stream=False)
            st.write(response)

In Action – Demo

Special thanks to @Saboo_Shubham_ for the guide and you check his GitHub repository for code.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish