MATIH Platform is in active MVP development. Documentation reflects current implementation status.
2. Architecture
AI Architecture

AI Service Architecture

Production - Python/FastAPI - Port 8000 - 65+ modules, multi-agent orchestrator

The AI Service is the conversational analytics engine and the largest single service in the platform. It implements a multi-agent orchestrator using a LangGraph-style state machine that coordinates specialized agents for intent classification, SQL generation, result analysis, and visualization recommendation. The service spans 65+ Python modules organized across agents, BI analytics, context graphs, LLM management, and storage layers.


2.4.B.1Module Architecture

API Layer (FastAPI Routes)
chat_routessession_routesfeedback_routesmodel_routesdashboard_routesanalytics_routesthinking_routes
Agent Orchestrator
RouterAgentSQLAgentAnalysisAgentVisualizationAgentDocumentationAgentApprovalAgentQualityMetrics
BI Analytics
DashboardServiceDashboardRepositoryWidgetEngineChartRecommender
Context & Intelligence
ContextGraphStoreThinkingEmbeddingsSemanticFeatureFlagsPatternStoreFeedbackStore
LLM Management
LLMRouterPromptABTestingResponseCacheTokenCounterContextIntelligenceValidationLayer
Storage Layer
PostgreSQL (sessions, configs)Redis (cache, sessions)Qdrant (embeddings)Dgraph (context graph)Kafka (events)

Key Module Areas

Module AreaFilesPurpose
agents/Orchestrator, router, SQL, analysis, viz, docs, approval, drift detection, hallucinationMulti-agent state machine and specialized agents
bi/Dashboard routes, service, repository, orchestratorBI analytics with AI-powered insights
context_graph/Dgraph storage, semantic features, thinking service, pattern/feedback/trace storesKnowledge graph for contextual intelligence
llm/Router, cache, context intelligence, infrastructure, performance, validationLLM provider abstraction and optimization
session/PostgreSQL-backed session storeConversation state management
config/Settings, tenant configsService configuration
storage/Database pool, connection managementPostgreSQL connection management
prompt_ab_testing/Repository, experiment frameworkA/B testing for prompt optimization

2.4.B.2Agent Orchestrator

The orchestrator coordinates five specialized agents through a state machine:

# From orchestrator.py - Agent state machine
class AgentState(Enum):
    START = "start"
    ROUTE = "route"
    GENERATE_SQL = "generate_sql"
    EXECUTE_QUERY = "execute_query"
    ANALYZE = "analyze"
    VISUALIZE = "visualize"
    DOCUMENTATION = "documentation"
    APPROVAL = "approval"
    RESPOND = "respond"
    ERROR = "error"
    END = "end"

State Machine Flow

START
  |
  v
ROUTE (RouterAgent classifies intent)
  |
  +---> SQL_QUERY ------> GENERATE_SQL (SQLAgent)
  |                           |
  |                      [approval needed?]
  |                      yes --> APPROVAL (ApprovalAgent)
  |                      no  --> EXECUTE_QUERY (via query-engine)
  |                                  |
  |                                  v
  |                             ANALYZE (AnalysisAgent)
  |                                  |
  |                                  v
  |                             VISUALIZE (VizAgent)
  |                                  |
  |                                  v
  |                             RESPOND
  |
  +---> ANALYSIS ---------> ANALYZE (AnalysisAgent)
  |                              |
  |                              v
  |                         RESPOND
  |
  +---> DOCUMENTATION ----> DOCS (DocumentationAgent)
  |                              |
  |                              v
  |                         RESPOND
  |
  v
END (session updated, events published)

Agent Responsibilities

AgentInputOutputLLM Usage
RouterAgentUser message + conversation historyIntent classification (SQL, analysis, docs)Classification prompt
SQLAgentUser question + schema context + semantic modelSQL query + explanationText-to-SQL prompt with RAG
AnalysisAgentQuery results + user questionStatistical insights, trends, anomaliesAnalytical reasoning prompt
VisualizationAgentData shape + analysis resultsChart type + configuration JSONVisualization recommendation
DocumentationAgentUser question + schema metadataDocumentation responseSchema explanation prompt
ApprovalAgentGenerated SQL + governance policiesApproval/rejection decisionPolicy evaluation

2.4.B.3Session Management

Conversation sessions are stored in PostgreSQL (with Redis caching for active sessions):

@dataclass
class ConversationSession:
    session_id: str
    tenant_id: str
    user_id: str | None
    state: AgentState          # Current state machine position
    messages: list[Message]    # Conversation history
    context: dict              # Accumulated context (schemas, results)
    created_at: datetime
    last_active: datetime
    message_count: int
    metadata: dict             # LLM tokens consumed, latencies

Session storage hierarchy:

  1. Redis -- Active session cache (TTL: 2 hours from last activity)
  2. PostgreSQL -- Persistent session storage (retained for analytics)
  3. In-memory fallback -- Used when both Redis and PostgreSQL are unavailable

2.4.B.4LLM Provider Abstraction

The LLMRouter abstracts multiple LLM providers behind a unified interface:

ProviderModelsUse Case
OpenAIGPT-4o, GPT-4-turbo, GPT-3.5-turboPrimary provider
Azure OpenAISame models via Azure endpointsEnterprise compliance
vLLMSelf-hosted open-source modelsOn-premises deployment
AnthropicClaude 3.5 SonnetAlternative provider

The router selects the provider based on:

  1. Tenant configuration (preferred provider)
  2. Task type (SQL generation may use a different model than analysis)
  3. Cost optimization (route simple tasks to cheaper models)
  4. Fallback chain (if primary is unavailable, try secondary)

Prompt A/B Testing

The prompt_ab_testing module enables controlled experiments on prompt variations:

# Experiment configuration
experiment = PromptExperiment(
    name="sql_generation_v3",
    variants=[
        PromptVariant("control", weight=0.5, template="prompts/sql_v2.txt"),
        PromptVariant("treatment", weight=0.5, template="prompts/sql_v3.txt"),
    ],
    metrics=["sql_accuracy", "execution_success", "user_satisfaction"],
    tenant_filter=None,  # All tenants
)

2.4.B.5Context Graph Integration

The AI service integrates with Dgraph for contextual intelligence:

FeatureStoragePurpose
Schema contextDgraphGraph of tables, columns, relationships for RAG
Query patternsPostgreSQLSuccessful query patterns for few-shot learning
User feedbackPostgreSQLThumbs up/down on AI responses
Thinking tracesPostgreSQLAgent reasoning chains for debugging
Semantic embeddingsQdrantVector embeddings of schema and queries

The context graph enables the AI service to understand not just the schema structure, but the semantic relationships between data entities, improving text-to-SQL accuracy.


2.4.B.6Key APIs

EndpointMethodDescription
/api/v1/ai/chatPOSTSend a conversational message
/api/v1/ai/chat/streamPOSTStreaming chat response (SSE)
/api/v1/ai/sessionsGETList active sessions
/api/v1/ai/sessions/{id}GET/DELETESession management
/api/v1/ai/feedbackPOSTSubmit response feedback
/api/v1/ai/modelsGETAvailable LLM models
/api/v1/ai/quality/metricsGETQuality metrics dashboard
/api/v1/bi/dashboardsGET/POSTAI-powered dashboard management

Related Sections