- unwind ai
- Posts
- Build an AI Finance Agent with xAI API
Build an AI Finance Agent with xAI API
AI agent with web access using xAI API in just 20 lines of Python Code (step-by-step instructions)
xAI API is finally here with the new grok-beta model. This model comes with 128k token context and function calling support, and comparable performance to Grok 2 but with improved efficiency, speed, and capabilities. Till 2024 end, you'll even have $25 free credit per month!
We just couldn’t resist building something with this model so here it is! Today we are making an AI Finance Agent that uses real-time stock data from YFinance and web search using DuckDuckGo to answer your investment-related queries.
It can provide current stock prices, fundamental data, analyst recommendations for stocks, and search for the latest news of a company to give a holistic picture.
The agent is powered by xAI's Grok for analyzing and summarizing the information and Phidata for agent orchestration.
What We’re Building
This application creates a financial analysis agent powered by xAI's Grok model, combining real-time stock data with web search capabilities. It provides structured financial insights through an interactive playground interface.
Features:
Powered by xAI's Grok-beta model
Real-time stock data analysis via YFinance
Web search capabilities through DuckDuckGo
Formatted output with tables for financial data
Interactive playground interface
Prerequisites
Before we begin, make sure you have:
Python installed on your machine (version 3.7 or higher is recommended)
Your xAI API Key
Basic familiarity with Python programming
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:
Clone the GitHub repository:
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
Go to the xai_finance_agent folder:
cd ai_agent_tutorials/xai_finance_agent
Install the required dependencies:
pip install -r requirements.txt
Get your xAI API Key: Sign up for xAI account > Go to console.x.ai > Complete the onboarding process to create a team and invite your teammates (if required) > Navigate to the API Keys page to create your first API key
Code Walkthrough
Let’s create our app. Create a new file xai_finance_agent.py
and add the following code:
Import necessary libraries:
• xAI Grok for LLM• Phidata for AI agent creation
• YFinance for financial data
• DuckDuckGo for search capability
from phi.agent import Agent
from phi.model.xai import xAI
from phi.tools.yfinance import YFinanceTools
from phi.tools.duckduckgo import DuckDuckGo
from phi.playground import Playground, serve_playground_app
Let's create the AI Finance Agent:
• Specialized for financial data
• Uses YFinance tools for market data
• Configured to display financial data in tables
agent = Agent(
name="xAI Finance Agent",
model = xAI(id="grok-beta"),
tools=[DuckDuckGo(), YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
instructions = ["Always use tables to display financial/numerical data. For text data use bullet points and small paragrpahs."],
show_tool_calls = True,
markdown = True,
)
Create and serve the user interface for finance agent:
• Creates interactive user interface
• Serves the Playground app
• Enables hot reloading
app = Playground(agents=[agent]).get_app()
if __name__ == "__main__":
serve_playground_app("xai_finance_agent:app", reload=True)
Running the App
With our code in place, it's time to launch the app.
Before running the app, you need to authenticate your local environment with Phidata. This ensures that your setup is properly configured to run the Agent UI locally. To do that, run the following command in your Terminal
phi auth
Once done, in your terminal, navigate to the project folder, and run the following command
python xai_finance_agent.py
Phidata will provide a local URL (typically localhost:7777). Open this in your web browser, put in your API keys, and have fun with the agent!
Working Application Demo
Conclusion
In just 20 lines of code, you’ve built a powerful AI Finance Agent that provides real-time financial insights and context through web search. This agent can be a great addition to any financial app or analysis tool.
To enhance the agent further, you can consider:
Adding Custom Indicators: Integrate technical indicators like moving averages or RSI for more in-depth stock analysis.
Incorporating News Sentiment: Analyze recent news articles about stocks to gauge public sentiment.
Expanding Financial Data Sources: Pull in data from other APIs, such as Forex or cryptocurrency exchanges.
Personalized Recommendations: Add features for personalized financial advice or custom watchlists.
This finance agent provides a solid foundation for building personalized, data-rich financial applications.
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.
Reply