Skip to content

ClearXs/chatbi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ChatBI - Multi-Agent Business Intelligence System

A sophisticated multi-agent system for natural language business intelligence queries that generates SQL using Monte Carlo Tree Search (MCTS) algorithm without requiring RAG (Retrieval-Augmented Generation).

πŸš€ Overview

ChatBI is an intelligent BI system that transforms natural language queries into actionable business insights through a coordinated multi-agent workflow. The system uses advanced MCTS algorithms for SQL generation and orchestrates multiple specialized agents to deliver comprehensive business reports.

πŸ—οΈ Architecture

Agent Workflow

Planner Agent β†’ SQL Agent β†’ Coder Agent β†’ Reporter Agent

Core Agents

  • 🎯 Planner Agent: Orchestrates the entire workflow, breaks down tasks, and coordinates agent execution
  • πŸ—„οΈ SQL Agent: Converts natural language to SQL using MCTS-based AlphaSQL algorithm
  • πŸ’» Coder Agent: Implements data processing, analysis, and visualization solutions
  • πŸ“Š Reporter Agent: Generates comprehensive BI reports with insights and recommendations

Supporting Agents

  • πŸ“ File Agent: Handles file operations (create, read, write, exists)
  • 🌐 Browser Agent: Web browsing and content extraction capabilities
  • πŸ”§ MCP Agent: Model Context Protocol integration for external tool access
  • πŸ” Research Agent: Literature review and research synthesis (Research domain)

πŸ› οΈ Key Features

✨ Zero-Shot SQL Generation

  • No RAG required - direct natural language to SQL conversion
  • MCTS algorithm ensures optimal query generation
  • Supports complex business logic and multi-table joins

🎨 Multi-Domain Support

  • Business Intelligence: Data querying, reporting, and visualization
  • Research: Literature review and academic research synthesis
  • Vision: Image analysis and optometry applications

πŸ”„ Intelligent Workflow Management

  • Automatic task decomposition and dependency management
  • Parallel agent execution for efficiency
  • Error handling and recovery mechanisms
  • Snapshot-based state management

πŸ“ˆ Advanced Analytics

  • Automated data visualization generation
  • Statistical analysis and trend identification
  • Business metrics interpretation
  • Professional report generation

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • Docker (for sandboxed execution)
  • OpenAI API key or compatible LLM endpoint

Installation

# Clone the repository
git clone <repository-url>
cd chatbi

# Install dependencies with poetry
poetry install

# Or with pip
pip install -r requirements.txt

Configuration

  1. Copy and configure the settings:
cp config.toml.example config.toml
  1. Edit config.toml with your LLM credentials:
[llm]
model = "gpt-4"
api_key = "your-api-key"
base_url = "https://api.openai.com/v1"

[agent.runtime]
parallel_nums = 3
retry = 3

Basic Usage

# Run the interactive CLI
python main.py --domain BI

# Or programmatically
from chatbi.agent.planner import PlannerAgent
from chatbi.llms.llm import LLM
from chatbi.config.configure import Configure

llm = LLM(Configure.config.llm.model, Configure.config.llm.api_key)
planner = PlannerAgent(llm=llm, domain="BI")

# Execute a BI query
result = await planner.process("Show me the total sales by region for Q4")

πŸ“Š Example Queries

Business Intelligence

"What's the percentage of users with guest role?"
"Show total revenue by product category this quarter"
"Create a visualization of user growth over time"
"Analyze customer churn rate by demographics"

Research Domain

"What do recent papers say about AI in education?"
"Find datasets about urban pollution"
"Research the latest developments in renewable energy"

🧠 Technical Deep Dive

AlphaSQL Algorithm

The SQL generation uses a sophisticated MCTS approach:

  1. Divide: Decompose complex queries into sub-questions
  2. Conquer: Generate pseudo-SQL for each component
  3. Combine: Recursively assemble the complete query
  4. Validate: Execute and verify query correctness

Agent Communication

Agents communicate through:

  • Memory System: Persistent conversation history
  • Dependency Management: Result passing between agents
  • Tool Integration: Shared file system and execution environment

Sandbox Environment

  • Isolated Docker containers for safe code execution
  • Automatic resource cleanup
  • Cross-agent file sharing

πŸ“ Project Structure

chatbi/
β”œβ”€β”€ agent/                  # Core agent implementations
β”‚   β”œβ”€β”€ base.py            # Base agent class and interfaces
β”‚   β”œβ”€β”€ planner.py         # Orchestration and planning
β”‚   β”œβ”€β”€ coder.py           # Code generation and execution
β”‚   β”œβ”€β”€ reporter.py        # Report generation
β”‚   └── BI/                # Domain-specific agents
β”œβ”€β”€ alphasql/              # SQL generation engine
β”‚   β”œβ”€β”€ algorithm/mcts/    # MCTS implementation
β”‚   └── prompt/            # SQL generation prompts
β”œβ”€β”€ llms/                  # LLM integrations
β”œβ”€β”€ memory/                # Conversation persistence
β”œβ”€β”€ tool/                  # Agent tools (file, bash, etc.)
β”œβ”€β”€ sandbox/               # Execution environments
└── prompt/                # Agent system prompts

πŸ”§ Advanced Configuration

Custom Domains

Extend the system with custom domain dialects:

class CustomDomainDialect(DomainDialect):
    async def rewrite_goal(self, agent, goal):
        # Transform goals for your domain
        return transformed_goal
    
    async def get_extra_system_rules(self):
        # Add domain-specific rules
        return additional_rules

Tool Integration

Add custom tools through MCP:

{
    "name": "custom_tool",
    "description": "Your tool description",
    "parameters": {
        "type": "object",
        "properties": {
            "param": {"type": "string"}
        }
    }
}

πŸ§ͺ Testing

# Run all tests
pytest

# Run specific agent tests
pytest tests/test_coder_agent.py
pytest tests/test_reporter_agent.py

# Run with coverage
pytest --cov=chatbi

πŸ“Š Performance Metrics

The system tracks comprehensive metrics:

  • Token usage and costs per agent
  • Execution time per workflow step
  • SQL query success rates
  • Agent error rates and recovery

About

A sophisticated multi-agent system for natural language business intelligence queries that generates SQL using Monte Carlo Tree Search (MCTS) algorithm without requiring RAG (Retrieval-Augmented Generation).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages