brain

package
v0.18.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 7 Imported by: 0

README

Native Brain: Production-Grade LLM Intelligence

The brain package is the core intelligence engine of HotPlex. It provides a highly reliable, observable, and cost-effective interface for Large Language Model (LLM) reasoning.

🏛 Architecture Overview

The system is designed with a layered "Enhanced Brain" architecture. It decorates a base LLM client with multiple production-readiness middleware layers.

graph TD
    User([Application]) --> Inter[Brain Interface]
    
    subgraph Layers [Production Layers]
        Inter --> Priority[Priority Scheduler]
        Priority --> Router[Model Router]
        Router --> Balancer[Load Balancer / Failover]
        Balancer --> Resil[Resiliency: Circuit Breaker / Retry]
        Resil --> Cache[LRU Cache]
        Cache --> RL[Rate Limiter]
        RL --> Obs[Observability: Metrics / Cost]
    end
    
    subgraph Providers [LLM Providers]
        Obs --> OpenAI[OpenAI SDK]
        Obs --> Custom[Custom Providers]
    end
Core Components
  • Brain Interface: Unified API for high-level reasoning (Chat, Analyze) and streaming (ChatStream).
  • Resiliency Engine: Implements exponential backoff retries and the Circuit Breaker pattern to handle transient failures and provider outages.
  • Dynamic Router: Automatically selects the optimal model based on the scenario (e.g., Code vs. Chat) and configured strategies (Cost vs. Latency vs. Quality).
  • High Availability: Multi-provider failover with automatic recovery (failback).
  • Resource Control: Distributed rate limiting and token bucket management per model.
  • Budget Guardrails: Multi-level token budget tracking (Daily/Weekly/Session) with hard and soft limits.

🛠 Developer Guide

Interface Definitions

The package exports several interfaces to allow for granular usage of brain capabilities:

// Base reasoning
type Brain interface {
    Chat(ctx context.Context, prompt string) (string, error)
    Analyze(ctx context.Context, prompt string, target any) error
}

// Specialized capabilities
type StreamingBrain interface { ChatStream(...) }
type RoutableBrain interface { ChatWithModel(...) }
type ObservableBrain interface { GetMetrics(...) }
type ResilientBrain interface { GetCircuitBreaker(...); GetFailoverManager(...) }
Advanced Usage Scenarios & Patterns
1. 🎬 Real-time Streaming (Live UI)

Best for large language outputs where low perceived latency is critical.

func StreamAnswer(ctx context.Context, question string) {
    if sb, ok := brain.Global().(brain.StreamingBrain); ok {
        stream, err := sb.ChatStream(ctx, question)
        if err != nil {
            log.Fatal(err)
        }
        
        for token := range stream {
            fmt.Print(token) // Progressive rendering
        }
    }
}
2. 🚦 Explicit Multi-Model Selection

Forces a specific model for specialized tasks (e.g., GPT-4o for complex reasoning).

func SpecializedTask(ctx context.Context) {
    if rb, ok := brain.Global().(brain.RoutableBrain); ok {
        // High quality model override
        ans, _ := rb.ChatWithModel(ctx, "gpt-4o", "Deep technical analysis...")
        fmt.Println(ans)
    }
}
3. 🎯 Intelligent Scenario Routing

Automatically detects task type (Code, Chat, or Analysis) and routes to the most cost-effective model configured in your strategies.

func AutoRouteAction(ctx context.Context, userPrompt string) {
    // router selected based on StrategyBalanced/StrategyCostPriority
    // System automatically detects 'Write a Python script' as ScenarioCode
    resp, err := brain.Global().Chat(ctx, userPrompt)
    if err != nil {
        log.Printf("Routing error: %v", err)
    }
}
4. 🏥 Resilience Management

Observing provider health and manually controlling the circuit state.

func MonitorResilience() {
    if rb, ok := brain.Global().(brain.ResilientBrain); ok {
        cb := rb.GetCircuitBreaker()
        fm := rb.GetFailoverManager()
        
        fmt.Printf("Circuit State: %s | Failure Count: %d\n", 
            cb.GetState(), cb.GetStats().FailRequests)
            
        // Manual failover if we detect high latency on primary
        if fm.GetCurrentProvider().Name == "openai" {
            _ = fm.ManualFailover("dashscope") 
        }
    }
}
5. 💰 Session Budget Guardrails

Tracking and enforcing financial limits for specific chat sessions.

func ControlledSession(sessionID string) {
    if bb, ok := brain.Global().(brain.BudgetControlledBrain); ok {
        tracker := bb.GetBudgetTracker(sessionID)
        
        // Estimated cost check before heavy processing
        if allowed, _, _ := tracker.CheckBudget(0.05); !allowed {
            fmt.Println("Session budget exceeded. Stopping.")
            return
        }
    }
}

📊 Observability & Metrics

The system tracks enterprise-level metrics using OpenTelemetry:

  • Latency: Detailed histogram of request durations.
  • Token Usage: Granular tracking of input/output tokens.
  • Financials: Real-time USD cost calculation based on model-specific pricing.
  • Reliability: Error rates and circuit breaker state transitions.
Metric Type Description
llm_request_duration Histogram Latency per model/operation
llm_tokens_total Counter Total tokens consumed
llm_cost_usd Gauge Cumulative cost in USD
llm_error_rate Gauge Failure percentage

⚙️ Configuration Reference

Variable Description Default
HOTPLEX_BRAIN_PROVIDER Primary provider (openai/dashscope/etc) openai
HOTPLEX_BRAIN_CIRCUIT_BREAKER_ENABLED Enable circuit breaker protection false
HOTPLEX_BRAIN_ROUTER_ENABLED Enable scenario-based model routing false
HOTPLEX_BRAIN_FAILOVER_ENABLED Enable automatic multi-provider switching false
HOTPLEX_BRAIN_BUDGET_LIMIT Hard USD limit for the period 10.0

🧪 Testing

The package includes high-coverage unit tests and integration tests for providers.

go test -v ./brain/...
  • Unit tests: Fast, mocks provider calls.
  • Integration tests: Requires API keys, tests real connectivity.
  • Scenario tests: Validates routing and budget logic under load.

Package Status: Production Ready (Phase 3)
Maintainer: HotPlex Core Team

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRateLimiter added in v0.18.0

func GetRateLimiter() *llm.RateLimiter

GetRateLimiter returns the global rate limiter if available.

func GetRouter added in v0.18.0

func GetRouter() *llm.Router

GetRouter returns the global router if the brain supports routing.

func Init

func Init(logger *slog.Logger) error

Init initializes the global Brain from environmental variables. It detects the provider and sets the Global Brain instance.

func SetGlobal

func SetGlobal(b Brain)

SetGlobal sets the global Brain instance.

Types

type Brain

type Brain interface {
	// Chat generates a plain text response for a given prompt.
	// Best used for simple questions, greetings, or summarization.
	Chat(ctx context.Context, prompt string) (string, error)

	// Analyze performs structured analysis and returns the result in the target struct.
	// The target must be a pointer to a struct that can be unmarshaled from JSON.
	// Useful for intent routing, safety checks, and complex data extraction.
	Analyze(ctx context.Context, prompt string, target any) error
}

Brain represents the core "System 1" intelligence for HotPlex. It provides fast, structured, and low-cost reasoning capabilities.

func Global

func Global() Brain

Global returns the globally configured Brain instance. If no brain is configured, it returns nil.

type BudgetConfig added in v0.18.0

type BudgetConfig struct {
	Enabled         bool
	Period          string
	Limit           float64
	EnableHardLimit bool
	AlertThresholds []float64
}

type BudgetControlledBrain added in v0.18.0

type BudgetControlledBrain interface {
	Brain

	// GetBudgetTracker returns the budget tracker for a session.
	GetBudgetTracker(sessionID string) *llm.BudgetTracker

	// GetBudgetManager returns the budget manager.
	GetBudgetManager() *llm.BudgetManager
}

BudgetControlledBrain extends Brain with budget control capabilities.

type CacheConfig added in v0.18.0

type CacheConfig struct {
	Enabled bool
	Size    int
}

type CircuitBreakerConfig added in v0.18.0

type CircuitBreakerConfig struct {
	Enabled     bool
	MaxFailures int
	Timeout     time.Duration
	Interval    time.Duration
}

type Config

type Config struct {
	// Enabled is automatically determined based on APIKey presence.
	Enabled bool
	// Model is the model configuration.
	Model ModelConfig
	// Cache is the cache configuration.
	Cache CacheConfig
	// Retry is the retry configuration.
	Retry RetryConfig
	// Metrics is the metrics configuration.
	Metrics MetricsConfig
	// Cost is the cost configuration.
	Cost CostConfig
	// RateLimit is the rate limit configuration.
	RateLimit RateLimitConfig
	// Router is the router configuration.
	Router RouterConfig
	// CircuitBreaker is the circuit breaker configuration.
	CircuitBreaker CircuitBreakerConfig
	// Failover is the failover configuration.
	Failover FailoverConfig
	// Budget is the budget configuration.
	Budget BudgetConfig
	// Priority is the priority configuration.
	Priority PriorityConfig
}

Config holds the configuration for the Global Brain.

func LoadConfigFromEnv

func LoadConfigFromEnv() Config

LoadConfigFromEnv loads the brain configuration from environment variables.

type CostConfig added in v0.18.0

type CostConfig struct {
	Enabled      bool
	EnableBudget bool
}

type FailoverConfig added in v0.18.0

type FailoverConfig struct {
	Enabled        bool
	Providers      []llm.ProviderConfig
	EnableAuto     bool
	EnableFailback bool
	Cooldown       time.Duration
}

type HealthStatus added in v0.18.0

type HealthStatus = llm.HealthStatus

HealthStatus represents the health status of the Brain service. Re-exported from llm package for convenience.

type MetricsConfig added in v0.18.0

type MetricsConfig struct {
	Enabled        bool
	ServiceName    string
	Endpoint       string
	ExportInterval time.Duration
}

type ModelConfig added in v0.18.0

type ModelConfig struct {
	Provider string
	Model    string
	Endpoint string
	TimeoutS int
}

type ObservableBrain added in v0.18.0

type ObservableBrain interface {
	Brain

	// GetMetrics returns current metrics statistics.
	GetMetrics() llm.MetricsStats

	// GetCostCalculator returns the cost calculator.
	GetCostCalculator() *llm.CostCalculator
}

ObservableBrain provides observability and metrics access.

type PriorityBrain added in v0.18.0

type PriorityBrain interface {
	Brain

	// GetPriorityScheduler returns the priority scheduler.
	GetPriorityScheduler() *llm.PriorityScheduler

	// SubmitWithPriority submits a request with specified priority.
	SubmitWithPriority(ctx context.Context, prompt string, priority llm.Priority) (string, error)
}

PriorityBrain extends Brain with priority queue capabilities.

type PriorityConfig added in v0.18.0

type PriorityConfig struct {
	Enabled               bool
	MaxQueueSize          int
	EnableLowPriorityDrop bool
	HighPriorityReserve   int
}

type RateLimitConfig added in v0.18.0

type RateLimitConfig struct {
	Enabled      bool
	RPS          float64
	Burst        int
	QueueSize    int
	QueueTimeout time.Duration
	PerModel     bool
}

type ResilientBrain added in v0.18.0

type ResilientBrain interface {
	Brain

	// GetCircuitBreaker returns the circuit breaker.
	GetCircuitBreaker() *llm.CircuitBreaker

	// GetFailoverManager returns the failover manager.
	GetFailoverManager() *llm.FailoverManager

	// ResetCircuitBreaker manually resets the circuit breaker.
	ResetCircuitBreaker()

	// ManualFailover manually switches to a specific provider.
	ManualFailover(providerName string) error
}

ResilientBrain extends Brain with circuit breaker and failover capabilities.

type RetryConfig added in v0.18.0

type RetryConfig struct {
	Enabled     bool
	MaxAttempts int
	MinWaitMs   int
	MaxWaitMs   int
}

type RoutableBrain added in v0.18.0

type RoutableBrain interface {
	Brain

	// ChatWithModel generates a response using a specific model.
	ChatWithModel(ctx context.Context, model string, prompt string) (string, error)

	// AnalyzeWithModel performs analysis using a specific model.
	AnalyzeWithModel(ctx context.Context, model string, prompt string, target any) error
}

RoutableBrain extends Brain with model routing capability.

type RouterConfig added in v0.18.0

type RouterConfig struct {
	Enabled      bool
	DefaultStage string
	Models       []llm.ModelConfig
}

type StreamingBrain added in v0.18.0

type StreamingBrain interface {
	Brain

	// ChatStream returns a channel that streams tokens as they are generated.
	// The channel is closed when the stream completes or an error occurs.
	// Best used for long responses, real-time UI updates, or progressive rendering.
	ChatStream(ctx context.Context, prompt string) (<-chan string, error)
}

StreamingBrain extends Brain with streaming capabilities. It provides token-by-token streaming for real-time responses.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL