Skip to main content
Glama
hani-ben-dhaou

Enterprise Big Data Copilot

企业大数据 Copilot

一个 AI 副驾驶,利用 RAG、MCP 工具和本地 LLM 推理,将自然语言问题转化为经过验证、感知模式的 Trino SQL。

概述

企业大数据 Copilot 让用户能够用通俗英语查询大数据平台。LangGraph 流水线检索相关文档(RAG)和实时模式元数据(MCP),使用本地 LLM(Ollama)生成 SQL,根据安全性和模式规则进行验证,并在平台可达时在 Trino 上执行并返回真实数据行。

Related MCP server: Doris MCP Server

功能特性

  • 面向 Trino 的自然语言 Text-to-SQL 生成

  • 使用 Ollama 进行本地 LLM 推理(无需云 API)

  • 基于 Trino / Hive / Iceberg 文档的 RAG 检索(Qdrant)

  • 基于实时目录元数据的模式感知生成

  • SQL 验证(只读 SELECT 强制、解析检查、模式接地)并带有自动重新生成循环

  • 尽力而为的查询执行和 Trino 结果检索

  • 模型上下文协议(MCP)服务器,暴露元数据、查询和分析工具

  • 面向 Open WebUI 的 OpenAI 兼容 API

  • 使用 LangSmith 进行端到端流水线追踪

架构

flowchart LR
    User --> API
    API --> Agent
    Agent --> RAG
    RAG --> Qdrant
    Agent --> LLM
    Agent --> MCP
    MCP --> Trino
    Agent --> Validation
    Agent --> Response
    Response --> User

技术栈

技术

用途

Python + FastAPI

后端和 REST/OpenAI 兼容 API

LangGraph

流水线编排(RAG → 模式 → SQL → 验证 → 执行)

Ollama

本地 LLM(llama3.2)和嵌入模型(mxbai-embed-large)

LangChain + Qdrant

RAG 文档检索

FastMCP

模型上下文协议服务器(工具)

LangSmith

流水线追踪和监控

Trino

SQL 查询引擎(TPCH 演示目录)

Open WebUI

聊天界面(可选)

Docker

容器化基础设施

项目结构

app/
├── agent/            # SQL agent + prompts (Ollama)
├── api/              # REST + OpenAI-compatible endpoints
├── core/             # Config, models, exceptions, logging
├── formatter/        # Response formatting
├── mcp/              # MCP server, client, catalog services
├── orchestrator/     # LangGraph pipeline
├── rag/              # Ingestion and retrieval (Qdrant)
├── services/         # Trino client
└── validator/        # SQL validation
tests/                # pytest suite
docker/               # App image + Trino config
docs/                 # RAG knowledge base
scripts/              # Document ingestion CLI

快速开始

前提条件

  • Python 3.11 或 3.12(不支持 3.13)

  • Docker + Docker Compose

  • GPU 可选(Ollama 可在 CPU 上运行)

安装

git clone https://github.com/hani-ben-dhaou/Enterprise-Big-Data-Copilot.git
cd entreprise-bigdata-copilot

python -m venv .venv
# Windows: .venv\Scripts\activate | macOS/Linux: source .venv/bin/activate

pip install -r requirements.txt
pip install -r requirements-dev.txt   # pytest

配置

cp .env.example .env

关键变量(默认值适用于本地开发):

变量

描述

OLLAMA_BASE_URL / OLLAMA_MODEL

LLM 服务器和模型(llama3.2)

OLLAMA_EMBED_MODEL

嵌入模型(mxbai-embed-large)

QDRANT_HOST / QDRANT_PORT

Qdrant 向量数据库

TRINO_HOST / TRINO_PORT / TRINO_CATALOG / TRINO_SCHEMA

Trino(默认目录 tpch,模式 tiny)

MCP_TRANSPORT

inprocess(默认,Windows 上推荐)或 sse

MCP_METADATA_SOURCE

inmemory(演示目录)或 trino(实时元数据)

ENABLE_SQL_EXECUTION

在 Trino 上运行验证后的 SQL

LANGCHAIN_TRACING_V2

设置为 true 以启用 LangSmith 追踪

LANGCHAIN_API_KEY

您的 LangSmith API 密钥(为空时追踪保持关闭)

LANGCHAIN_PROJECT

LangSmith 项目名称(默认 copilot)

运行

# 1. Start infrastructure (Ollama, Qdrant, Trino)
docker compose up -d

# 2. Pull models and ingest documentation (Qdrant must be up)
docker exec -it copilot-ollama ollama pull llama3.2
docker exec -it copilot-ollama ollama pull mxbai-embed-large
python scripts/ingest_docs.py

# 3. Start the API
uvicorn app.main:app --reload --port 8000

# 4. Optional: standalone MCP server (SSE on :8001)
python -m app.mcp.server

使用方法

用自然语言提问:

curl -X POST http://localhost:8000/api/v1/query \
  -H "Content-Type: application/json" \
  -d '{"question":"Show me the top 10 customers by total revenue last month"}'

响应包括生成的 SQL、解释、置信度分数、警告,以及当执行启用时的结果行:

{
  "question": "Show me the top 10 customers by total revenue last month",
  "sql": "SELECT ...",
  "explanation": "...",
  "confidence": 0.92,
  "warnings": [],
  "dialect": "trino",
  "results": [["42", "Acme", 98765.00]],
  "execution": {"status": "ok", "columns": ["id", "name", "revenue"], "row_count": 1, "truncated": false}
}

其他端点:GET /api/v1/schema(列出目录)、GET /api/v1/health 和 POST /v1/chat/completions(OpenAI 兼容,供 Open WebUI 使用)。

测试

pytest

测试套件是封闭的,无需实时堆栈即可运行(146 个测试)。

追踪与监控

每个流水线查询(RAG 检索、模式查找、SQL 生成、验证循环、执行)都可以使用 LangSmith 进行追踪。创建一个免费账户,获取 API 密钥,并设置:LANGCHAIN_TRACING_V2=true、LANGCHAIN_API_KEY=<您的密钥>,以及可选的 LANGCHAIN_PROJECT=copilot。未配置密钥时追踪保持关闭。

LangSmith 对 copilot 流水线运行的追踪

Docker

docker compose 运行完整堆栈:

服务

容器

端口

Ollama

copilot-ollama

11434

Qdrant

copilot-qdrant

6333

Trino

copilot-trino

8080

Open WebUI

copilot-webui

3000

Copilot API

copilot-api

8000

MCP Server

copilot-mcp

8001

命名卷持久化 Ollama 模型、Qdrant 数据和 Open WebUI 数据。ollama 卷被声明为 external——如果不存在,请创建一次:

docker volume create ollama
docker compose up -d
docker compose ps
docker compose logs -f copilot-api
docker compose down

Windows 注意: 在 Windows 事件循环上,通过真实 SSE 的 MCP 可能不稳定。在 Windows 上进行本地开发时,请保持 MCP_TRANSPORT=inprocess;在 Linux/Docker 上使用 SSE。

Related MCP Connectors

Related MCP Servers

  • A
    license
    B
    quality
    D
    maintenance
    Provides AI models with structured access to Trino's distributed SQL query engine, enabling LLMs to directly query and analyze data stored in Trino databases.
    3
    10
    MIT
  • A
    license
    Not graded
    quality
    F
    maintenance
    Enables natural language querying of Apache Doris databases via LLM-powered SQL generation, execution, and metadata management through the MCP protocol.
    9
    Apache 2.0
  • F
    license
    A
    quality
    A
    maintenance
    Natural language to SQL engine with multi-connector support (PostgreSQL, MySQL, Snowflake, BigQuery, DuckDB), document QA, semantic caching, and self-hosted MCP server.
    9
    31 PyPI
    4
    -