Skip to content

gmartincor/Euler-LanGraph-Agent

Repository files navigation

ReAct Agent for Integral Calculus

A professional intelligent agent using ReAct (Reasoning and Acting) methodology to solve mathematical integrals and visualize results with full persistence in PostgreSQL.

Overview

This project implements a sophisticated ReAct (Reasoning and Acting) agent that combines advanced AI capabilities with mathematical computation tools. The agent can understand natural language requests for integral calculus problems, reason about the appropriate solution approach, execute calculations, and generate visualizations.

Features

  • ReAct Agent: Intelligent reasoning and action cycles using LangGraph
  • BigTool Integration: Automatic tool selection via semantic search
  • Google Gemini 2.5 Flash: Advanced LLM for mathematical reasoning
  • Interactive Visualizations: Dynamic plots with Matplotlib and Plotly
  • Full Persistence: PostgreSQL for conversations, cache, and metrics
  • Web Interface: Professional Streamlit application
  • Docker Deployment: Complete containerized solution
  • Comprehensive Testing: Professional test suite with Jupyter notebooks

πŸ§ͺ Testing & Development Notebooks

The project includes a comprehensive suite of Jupyter notebooks for testing, debugging, and development:

Master Test Suite

  • 00_master_test_suite.ipynb - Orchestrates all tests with comprehensive reporting

Core Testing Notebooks

  • 01_environment_test.ipynb - Environment and dependency validation
  • 02_agent_core_test.ipynb - Core agent components testing
  • 03_workflow_integration_test.ipynb - End-to-end workflow testing
  • 04_database_test.ipynb - Database integration and persistence
  • 05_bigtool_integration_test.ipynb - BigTool semantic search testing
  • 06_error_debugging.ipynb - Comprehensive error detection and debugging
  • 07_performance_optimization.ipynb - Performance analysis and load testing

Quick Start Testing

# Start the environment
make up

# Run Jupyter Lab to access notebooks
make jupyter

# Or run automated test validation
make test-notebooks

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    PRESENTATION LAYER                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  Streamlit UI   β”‚    β”‚  Jupyter Lab    β”‚    β”‚   REST API  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚ User Input: "Calculate integral of x^2 from 0 to 3"
            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  LANGGRAPH REACT WORKFLOW                       β”‚
β”‚                                                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚   THINK     │────▢│     ACT     │────▢│    OBSERVE      β”‚    β”‚
β”‚  β”‚             β”‚     β”‚             β”‚     β”‚                 β”‚    β”‚
β”‚  β”‚ β€’ Analyze   β”‚     β”‚ β€’ Select    β”‚     β”‚ β€’ Evaluate      β”‚    β”‚
β”‚  β”‚   request   β”‚     β”‚   tools     β”‚     β”‚   results       β”‚    β”‚
β”‚  β”‚ β€’ Plan      β”‚     β”‚ β€’ Execute   β”‚     β”‚ β€’ Check         β”‚    β”‚
β”‚  β”‚   approach  β”‚     β”‚   actions   β”‚     β”‚   completion    β”‚    β”‚
β”‚  β”‚ β€’ Reason    β”‚     β”‚ β€’ Call APIs β”‚     β”‚ β€’ Learn from    β”‚    β”‚
β”‚  β”‚   about     β”‚     β”‚ β€’ Calculate β”‚     β”‚   feedback      β”‚    β”‚
β”‚  β”‚   math      β”‚     β”‚   integrals β”‚     β”‚ β€’ Decide next   β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚        β”‚                   β”‚                     β”‚              β”‚
β”‚        β”‚                   β–Ό                     β”‚              β”‚
β”‚        β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚              β”‚
β”‚        β”‚    β”‚      Google Gemini 2.5 Flash   β”‚  β”‚              β”‚
β”‚        β”‚    β”‚   (Mathematical Reasoning &     β”‚  β”‚              β”‚
β”‚        β”‚    β”‚    Natural Language Processing) β”‚  β”‚              β”‚
β”‚        β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚              β”‚
β”‚        β”‚                                         β”‚              β”‚
β”‚        └─────────────────────────────────────────┴───────────────
β”‚                                                                 β”‚
β”‚        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ WORKFLOW CYCLE ────────────────────────┐    β”‚
β”‚        β”‚ Continue until problem is solved OR max iterations β”‚    β”‚
β”‚        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     TOOL MANAGEMENT LAYER                      β”‚
β”‚                                                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚               BigTool Registry                          β”‚    β”‚
β”‚  β”‚          (Semantic Tool Selection)                      β”‚    β”‚
β”‚  β”‚                                                         β”‚    β”‚
β”‚  β”‚  "I need to calculate an integral" β†’ IntegralTool      β”‚    β”‚
β”‚  β”‚  "Show me a plot" β†’ PlotTool                           β”‚    β”‚
β”‚  β”‚  "Analyze the result" β†’ AnalysisTool                   β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                    β”‚                                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚    β”‚
β”‚  β”‚  β”‚IntegralTool β”‚   β”‚  PlotTool   β”‚   β”‚AnalysisTool β”‚    β”‚    β”‚
β”‚  β”‚  β”‚             β”‚   β”‚             β”‚   β”‚             β”‚    β”‚    β”‚
β”‚  β”‚  β”‚β€’ SymPy      β”‚   β”‚β€’ Matplotlib β”‚   β”‚β€’ Statistics β”‚    β”‚    β”‚
β”‚  β”‚  β”‚β€’ SciPy      β”‚   β”‚β€’ Plotly     β”‚   β”‚β€’ Validation β”‚    β”‚    β”‚
β”‚  β”‚  β”‚β€’ Numerical  β”‚   β”‚β€’ Interactiveβ”‚   β”‚β€’ Error Checkβ”‚    β”‚    β”‚
β”‚  β”‚  β”‚  Integrationβ”‚   β”‚  Plots      β”‚   β”‚β€’ Result Evalβ”‚    β”‚    β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     PERSISTENCE LAYER                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   PostgreSQL    β”‚    β”‚   Vector Store  β”‚    β”‚   Cache     β”‚  β”‚
β”‚  β”‚                 β”‚    β”‚   (Embeddings)  β”‚    β”‚   (Memory)  β”‚  β”‚
β”‚  β”‚β€’ Conversations  β”‚    β”‚β€’ Tool Vectors   β”‚    β”‚β€’ Results    β”‚  β”‚
β”‚  β”‚β€’ Agent State    β”‚    β”‚β€’ Semantic Index β”‚    β”‚β€’ Sessions   β”‚  β”‚
β”‚  β”‚β€’ Workflow Steps β”‚    β”‚β€’ Search History β”‚    β”‚β€’ Temp Data  β”‚  β”‚
β”‚  β”‚β€’ Results Cache  β”‚    β”‚β€’ User Patterns  β”‚    β”‚β€’ Tool Cache β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The system follows a layered architecture with clear separation of concerns:

  • Presentation Layer: Streamlit web interface for user interaction
  • Agent Layer: ReAct agent powered by LangGraph for reasoning and decision-making
  • Tool Layer: Specialized mathematical tools managed by BigTool for intelligent selection
  • Data Layer: PostgreSQL database for persistence and conversation history
  • LLM Integration: Google Gemini 2.5 Flash for natural language processing and mathematical reasoning

The agent uses BigTool's semantic search to automatically select the most appropriate tools (IntegralTool, PlotTool, AnalysisTool) based on user requests, ensuring efficient and accurate responses.

Technology Stack

  • AI Framework: LangChain Core 0.3.72 + LangGraph 0.6.2
  • LLM Provider: Google Gemini 2.5 Flash (AI Studio)
  • Tool Management: BigTool 0.0.3 for intelligent tool selection
  • Database: PostgreSQL with vector extensions
  • Frontend: Streamlit for web interface
  • Mathematical Libraries: SciPy, NumPy, SymPy, Matplotlib
  • Deployment: Docker + Docker Compose
  • Development: Poetry for dependency management

Quick Start

Prerequisites

  • Docker and Docker Compose
  • Google AI Studio API Key (Get it here)

Installation

  1. Clone the repository

    git clone <repository-url>
    cd Agent_1
  2. Configure environment

    cp .env.example .env
    # Edit .env and add your Google API key
  3. Start services

    make setup
    make up
  4. Verify installation

    make test
    make health

Access Points

Example Usage

Open the Streamlit interface and try:

Calculate the integral of x^2 from 0 to 3 and visualize the area under the curve

Development

Docker-Based Development (Recommended)

# View all available commands
make help

# Start development environment
make dev

# Monitor services
make status
make logs

# Quality checks
make test
make lint
make format

# Database operations
make db-shell
make db-reset

Local Development

If you prefer local development without Docker:

  1. Install Poetry

    curl -sSL https://install.python-poetry.org | python3 -
  2. Install dependencies

    poetry install
  3. Start PostgreSQL

    docker-compose up postgres -d
  4. Run the application

    poetry shell
    poetry run streamlit run app/main.py

Jupyter Notebooks

Interactive notebooks for testing and development:

# Open Jupyter Lab
make jupyter

# Test all notebooks
make notebook-test

# Test specific notebooks
make notebook-env     # Environment tests
make notebook-math    # Mathematical tools
make notebook-agent   # ReAct agent tests
make notebook-db      # Database tests

Testing

Running Tests

# All tests
make test

# Unit tests only
poetry run pytest tests/unit/ -v

# Integration tests
poetry run pytest tests/integration/ -v

# Coverage report
poetry run pytest --cov=app --cov-report=html

# Specific test suites
poetry run pytest tests/unit/test_persistence.py -v
poetry run pytest tests/unit/test_bigtool_integration.py -v

Configuration

Environment Variables

Create a .env file with the following configuration:

# Required
GOOGLE_API_KEY=your_google_api_key_here
DATABASE_URL=postgresql://agent_user:agent_pass@localhost:5432/react_agent_db

# Optional
DEBUG=true
ENVIRONMENT=development
LOG_LEVEL=INFO

# BigTool Configuration
BIGTOOL_INDEX_BATCH_SIZE=100
BIGTOOL_SEARCH_LIMIT=5

# Cache Settings
CALCULATION_CACHE_TTL_HOURS=24
CONVERSATION_HISTORY_LIMIT=100

Database Setup

PostgreSQL extensions required:

CREATE EXTENSION IF NOT EXISTS vector;      -- For embeddings
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -- For UUIDs

Usage Examples

Simple Integral Calculation

User: "Calculate the integral of x^2 from 0 to 2"

Agent: 
Think: I need to compute the definite integral of x^2 over [0,2]
Act: Using IntegralTool with function="x**2", lower=0, upper=2
Observe: Result = 8/3 β‰ˆ 2.667

The integral of xΒ² from 0 to 2 is 8/3 β‰ˆ 2.667

Visualization with Area

User: "Show the plot of sin(x) and the area under the curve from 0 to Ο€"

Agent:
Think: I need to compute the integral and create a visualization
Act: IntegralTool β†’ PlotTool with shaded area
Observe: Integral = 2.0, plot generated

[Interactive plot with shaded area showing sin(x) from 0 to Ο€]
The area under the curve of sin(x) from 0 to Ο€ is 2.0

Persistence and Monitoring

Persistent Data

The system maintains persistent state across sessions:

  • Conversations: Full history across sessions
  • Calculation Cache: Avoids recalculating integrals
  • Usage Metrics: Tool usage analysis
  • Agent State: Recovery after restarts

Database Schema

-- Main tables
conversations        -- Conversation sessions
messages            -- User/agent messages  
calculation_results -- Integral cache
tool_usage          -- Tool usage logs

Monitoring

Available metrics include:

  • Tool usage frequency and performance
  • Cache hit rate for calculations
  • Agent response time
  • Conversation history and analytics

Access metrics through the Streamlit sidebar:

tool_stats = st.session_state.tool_tracker.get_tool_statistics()
st.sidebar.json(tool_stats)

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published