Skip to main content
In this comprehensive cookbook, we’ll build a sophisticated stock market analysis tool that combines the power of Groq’s fast LLM inference with function calling capabilities. Our tool will be able to understand natural language queries about stocks and automatically fetch data, perform analysis, and create beautiful visualizations.

Prerequisites

  • Python 3.8+
  • Groq API key (free at Groq Console)
  • Maxim API key (for logging - Maxim Console)

Step 1: Setting Up Dependencies

First, let’s install all the required packages:
What each package does:
  • groq: Fast LLM inference with function calling support
  • yfinance: Yahoo Finance data retrieval
  • pandas: Data manipulation and analysis
  • plotly: Interactive data visualization
  • maxim-py: AI observability and logging

Step 2: Environment Setup and API Configuration

Step 3: Initialize Maxim Logging and Groq Client

Why this matters: The Maxim integration gives us detailed insights into our AI model’s performance, token usage, and function call patterns - essential for production applications.

Step 4: Building Core Data Retrieval Functions

Stock Information Function

This function fetches specific financial metrics like market cap, beta, P/E ratio, etc.

Date Parsing for Natural Language

One of the biggest challenges is handling natural language date expressions. Here’s our solution:
Key insight: This function transforms user-friendly expressions like “6 months ago” into API-compatible date formats.

Historical Price Data Function

Step 5: Creating Stunning Visualizations

Why Plotly: Interactive charts that users can zoom, pan, and hover for details - much better than static matplotlib charts.

Step 6: Defining Function Schemas for Groq

This is where the magic happens. We define our functions in a schema that Groq can understand:
Critical detail: Notice how we explicitly mention that relative dates are accepted. This guides the AI on how to use our functions.

Step 7: The Brain - Function Execution Handler

This simple dispatcher routes function calls to the appropriate Python functions.

Step 8: The Complete Query Processing Engine

Here’s where everything comes together:
The flow:
  1. Send user query to Groq
  2. If Groq decides to call functions, execute them
  3. Send results back to Groq for final analysis
  4. Automatically create charts for historical data
  5. Return comprehensive analysis

Step 9: Testing Our Creation

Let’s put our system through its paces:
LLM Response for Apple’s stock price:
LLM Response for Tesla’s stock price:
Stock Comparison Plot:

Maxim Observability

Conclusion

We’ve built a powerful, AI-driven stock market analysis tool that demonstrates the incredible potential of combining fast LLM inference with function calling. The system understands natural language, fetches real-time data, creates beautiful visualizations, and provides intelligent analysis - all from simple English queries.

Resources

Cookbook Code

Python Notebook for Groq & Maxim