MATIH Platform is in active MVP development. Documentation reflects current implementation status.
12. AI Service
Overview
Part IV: Data Plane Services

Chapter 12: AI Service and Conversational Analytics

The AI Service is the largest and most complex microservice in the MATIH platform, implementing the conversational analytics engine that transforms natural language questions into data insights. Built on Python/FastAPI with 984 source files, it orchestrates 80+ agent types, manages multi-turn conversations, generates SQL from natural language, and delivers results through real-time streaming.

Learning Objectives

  • Understand the modular architecture with 7 feature-flagged modules
  • Master the AgentOrchestrator and MultiAgentOrchestrator patterns
  • Learn the Text-to-SQL pipeline from NL parsing through SQL validation
  • Explore the BI Analytics orchestrator state machine
  • Configure the multi-provider LLM Router with tenant-level routing strategies
  • Integrate the Context Graph for agent thinking traces and semantic search

Details

Estimated Read Time: 4-6 hours (full chapter)
Prerequisites:
  • Chapter 2: Platform Architecture
  • Chapter 8: Data Plane Overview
  • Familiarity with Python, FastAPI, and LLM concepts
Related Chapters:
  • Ch. 8: Data Plane Services
  • Ch. 10: Query Engine
  • Ch. 14: Ontology Service

What You Will Learn

This chapter is divided into eight major sections, each covering a distinct subsystem of the AI Service. The sections are organized from foundational infrastructure through to API reference documentation.

SectionDescriptionAudience
Service ArchitectureModule organization, FastAPI application lifecycle, middleware stack, configuration, and deployment topologyArchitects, Platform Engineers
Agent SystemAgent orchestration, multi-agent teams, memory, guardrails, approval workflows, studio, drift detection, evaluation, and runtime lifecycleAI/ML Engineers, Backend Developers
Text-to-SQL PipelineNL parsing, schema extraction, SQL generation, validation, auto-correction, query decomposition, and benchmarkingData Engineers, AI/ML Engineers
BI Analytics PlatformChat sessions, dashboards, alerts, export, filters, scheduling, sharing, visualization, and streamingData Analysts, Frontend Developers
Context GraphSemantic search, graph traversal, agent thinking traces, ontology management, causal inference, and semantic SQLKnowledge Engineers, AI/ML Engineers
LLM InfrastructureMulti-provider router, response cache, context management, validation, MCP integration, and performance monitoringPlatform Engineers, AI/ML Engineers
ML IntegrationModel training, hyperparameter tuning, feature engineering, model serving, registry, and experiment trackingML Engineers
IntegrationsKafka event streaming, WebSocket communication, feedback/RLHF, data lineage, dbt, personalization, and copilot featuresBackend Developers, Platform Engineers
API ReferenceComplete endpoint documentation for all REST, WebSocket, and GraphQL APIsAll Developers

12.1AI Service at a Glance

The AI Service is a Python 3.11+ / FastAPI application that serves as the conversational analytics brain of the MATIH platform. It receives natural language questions from frontend clients (BI Workbench, Agentic Workbench), routes them through specialized agents, generates SQL, retrieves relevant context via RAG, and returns structured responses with data, visualizations, and explanations.

Architecture Diagram

                    +---------------------------+
                    |     Frontend Clients       |
                    | (BI Workbench, Agentic UI) |
                    +------------+--------------+
                                 |
                    +------------v--------------+
                    |      AI Service           |
                    |     (Port 8000)           |
                    |   7 Feature-Flagged       |
                    |       Modules             |
                    +------------+--------------+
                                 |
     +--------+--------+--------+--------+--------+--------+
     |        |        |        |        |        |        |
  +--v---+ +--v---+ +--v---+ +--v----+ +-v-----+ +-v----+ +-v-------+
  | Core | | BI   | | ML   | | Data  | |Context| |Enter-| |Supple-  |
  |Module| |Platf.| |Platf.| |Platf. | |Graph  | |prise | |mentary  |
  +--+---+ +--+---+ +--+---+ +--+----+ +--+----+ +--+---+ +--+------+
     |        |        |        |          |         |        |
  +--v--------v--------v--------v----------v---------v--------v------+
  |                  Shared Infrastructure                            |
  |  LLM Router | RAG | Vector Store | Sessions | Cache | Kafka     |
  +--+-------+--------+---------+----------+----------+-------------+
     |       |        |         |          |          |
  +--v--+ +-v------+ +-v-----+ +-v------+ +-v------+ +-v------+
  |LLM  | |Qdrant  | |Query  | |Redis / | |Kafka   | |Dgraph  |
  |APIs  | |Vectors | |Engine | |Postgres| |Broker  | |Graph   |
  +------+ +--------+ +-------+ +--------+ +--------+ +--------+

Key Numbers

MetricValue
TechnologyPython 3.11+, FastAPI 0.100+, uvicorn
Service Port8000
Source Files984 Python files
Feature Modules7 (Core, BI Platform, ML Platform, Data Platform, Context Graph, Enterprise, Supplementary)
LLM ProvidersOpenAI, Anthropic, Azure OpenAI, Vertex AI, Bedrock, vLLM
Agent Types80+ (Conversational, Specialist, Supervisor, Persona, Tool-specific)
Routing StrategiesCost-optimized, Latency-optimized, Quality-optimized, Round-robin, Fallback
Streaming ProtocolsWebSocket, Server-Sent Events (SSE)
Vector StoreQdrant (gRPC/HTTP)
Session BackendsRedis (primary), PostgreSQL (persistent), Memory (fallback)
Kafka Topics9 topics (feedback, learning signals, insights, agent thinking, decisions, traces, metrics)

12.2Module System

The AI Service uses a modular architecture where each module is a self-contained unit that registers its own routers and initializes its own services. Modules can be individually enabled or disabled via environment variables.

# Module feature flags from src/main.py
MODULE_FLAGS = {
    "core": os.environ.get("MODULE_CORE_ENABLED", "true").lower() == "true",
    "bi_platform": os.environ.get("MODULE_BI_ENABLED", "true").lower() == "true",
    "ml_platform": os.environ.get("MODULE_ML_ENABLED", "true").lower() == "true",
    "data_platform": os.environ.get("MODULE_DATA_ENABLED", "true").lower() == "true",
    "context_graph": os.environ.get("MODULE_CONTEXT_GRAPH_ENABLED", "true").lower() == "true",
    "enterprise": os.environ.get("MODULE_ENTERPRISE_ENABLED", "true").lower() == "true",
    "supplementary": os.environ.get("MODULE_SUPPLEMENTARY_ENABLED", "true").lower() == "true",
}
ModuleFlagKey Capabilities
CoreMODULE_CORE_ENABLEDAgents, LLM Router, Cache, MCP, Studio, Evaluation, Quality Metrics, Guardrails, Tools, Runtime Lifecycle
BI PlatformMODULE_BI_ENABLEDAnalytics Orchestrator, Dashboards, Alerts, Export, Filters, Scheduling, Sharing, Semantic Models
ML PlatformMODULE_ML_ENABLEDFeature Store, Model Registry, Training, Serving, Tracking, Governance
Data PlatformMODULE_DATA_ENABLEDCatalog Integration, Pipeline Management, Data Quality
Context GraphMODULE_CONTEXT_GRAPH_ENABLEDKnowledge Graph, Ontology, Semantic SQL, SHACL, Agent Thinking, Kafka Streaming
EnterpriseMODULE_ENTERPRISE_ENABLEDBilling, Compliance, Audit
SupplementaryMODULE_SUPPLEMENTARY_ENABLEDSearch, Recommendations, FDME, Workflows, Feedback, Lineage, Personalization

12.3Key Source Files

The AI Service implementation is organized under data-plane/ai-service/src/:

PathPurpose
main.pyFastAPI application entry point with module system
config/settings.pyPydantic Settings with 60+ configuration parameters
agents/orchestrator.pyAgentOrchestrator and MultiAgentOrchestrator
agents/studio/Agent creation, templates, preview, deployment
agents/runtime/Agent platform feature flags, lifecycle, beta program, production rollout, GA readiness
sql_generation/generator.pySQLGenerator pipeline
sql_generation/validator.pySQLValidator with sqlglot/sqlparse
bi/agents/orchestrator.pyAnalyticsOrchestrator state machine
bi/api/dashboard_routes.pyDashboard CRUD endpoints
llm/providers/router.pyLLMRouter with multi-provider routing
llm/cache/cache_service.pyResponseCacheService with tenant isolation
context_graph/Knowledge graph, ontology, semantic SQL, agent thinking
feedback/Feedback collection, RLHF, Kafka integration
session/Redis-backed session management
storage/pool.pyasyncpg connection pool management

12.4Integration Points

The AI Service integrates with nearly every other component in the MATIH platform:

IntegrationProtocolPurpose
Query EngineHTTP RESTSQL execution against Trino/ClickHouse
Catalog ServiceHTTP RESTSchema metadata discovery
Semantic LayerHTTP RESTBusiness metric definitions
Ontology ServiceHTTP RESTEntity relationships and semantic queries
QdrantgRPC/HTTPVector storage for schema embeddings and RAG
RedisRedis protocolSession storage, caching, pub/sub
PostgreSQLasyncpgPersistent session, feedback, and metrics storage
KafkaaiokafkaEvent streaming for feedback, context graph, agent metrics
DgraphHTTP/GraphQLAgent thinking trace persistence
OpenAIHTTPSGPT-4o inference
AnthropicHTTPSClaude inference (with extended thinking)
Azure OpenAIHTTPSAzure-hosted OpenAI models
Vertex AIgRPC/HTTPSGemini model inference
AWS BedrockHTTPSBedrock model inference
vLLMHTTPSelf-hosted model inference

12.5Getting Started

To work with the AI Service locally:

# Navigate to service directory
cd data-plane/ai-service
 
# Install dependencies
pip install -r requirements.txt
 
# Set required environment variables
export JWT_SECRET_KEY=dev-secret-key-minimum-32-characters
export DATABASE_URL=postgresql://matih:password@localhost:5432/matih_ai
export REDIS_HOST=localhost
export REDIS_PORT=6379
export OPENAI_API_KEY=sk-xxx
export QDRANT_URL=http://localhost:6333
export KAFKA_BOOTSTRAP_SERVERS=localhost:9092
 
# Start the service
uvicorn src.main:app --host 0.0.0.0 --port 8000 --reload

The service exposes interactive API documentation at:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

Health Endpoints

# General health
curl http://localhost:8000/health
 
# Semantic Platform health
curl http://localhost:8000/health/semantic-platform
 
# Agent Platform health
curl http://localhost:8000/health/agent-platform
 
# Database Pool health
curl http://localhost:8000/health/database-pool

Disabling Modules for Development

To run a minimal configuration during local development:

# Disable non-essential modules
export MODULE_ML_ENABLED=false
export MODULE_DATA_ENABLED=false
export MODULE_ENTERPRISE_ENABLED=false
export MODULE_SUPPLEMENTARY_ENABLED=false
 
# Run with only Core, BI Platform, and Context Graph
uvicorn src.main:app --host 0.0.0.0 --port 8000 --reload