Documentation
¶
Overview ¶
Package router provides the LLM routing service.
Package router provides the LLM routing service interface for AI agents. This interface is consumed by Team B (Assistant+Schedule) and Team C (Memo Enhancement).
Package router provides the LLM routing service.
Package router provides the LLM routing service.
Package router provides the LLM routing service.
Index ¶
- Constants
- func WithUserID(ctx context.Context, userID int32) context.Context
- type AgentType
- type Config
- type HistoryMatchResult
- type HistoryMatcher
- type Intent
- type LLMClassifier
- type LLMClassifyResult
- type LLMClient
- type MockRouterService
- type ModelConfig
- type RouterService
- type RuleMatcher
- type Service
- type TaskType
Constants ¶
const ClassificationPrompt = `` /* 656-byte string literal not displayed */
ClassificationPrompt is the prompt template for intent classification.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AgentType ¶
type AgentType string
AgentType represents the agent type for routing.
func IntentToAgentType ¶
IntentToAgentType converts Intent to AgentType. This is the canonical mapping used across the routing system.
type Config ¶
type Config struct {
MemoryService memory.MemoryService
LLMClient LLMClient
}
Config contains the configuration for the router service.
type HistoryMatchResult ¶
type HistoryMatchResult struct {
Intent Intent
Confidence float32
SourceID int64 // ID of the matched episode
Matched bool
}
HistoryMatchResult contains the result of history matching.
type HistoryMatcher ¶
type HistoryMatcher struct {
// contains filtered or unexported fields
}
HistoryMatcher implements Layer 2 history-based intent matching. Layer 2a: Lexical similarity (~1ms) - Jaccard on character bigrams Layer 2b: Semantic similarity (~50ms) - Embedding cosine similarity (optional) Target: Handle 30%+ of requests that pass Layer 1.
func NewHistoryMatcher ¶
func NewHistoryMatcher(ms memory.MemoryService) *HistoryMatcher
NewHistoryMatcher creates a new history matcher.
func (*HistoryMatcher) Match ¶
func (m *HistoryMatcher) Match(ctx context.Context, userID int32, input string) (*HistoryMatchResult, error)
Match attempts to classify intent by finding similar historical patterns. Layer 2a: Lexical matching (~1ms) - high precision Layer 2b: Semantic matching (~50ms) - triggered when lexical is ambiguous Returns matched=true if a similar pattern was found with confidence >= threshold.
func (*HistoryMatcher) SaveDecision ¶
func (m *HistoryMatcher) SaveDecision(ctx context.Context, userID int32, input string, intent Intent, success bool) error
SaveDecision saves a routing decision to memory for future matching.
func (*HistoryMatcher) SetEmbeddingService ¶
func (m *HistoryMatcher) SetEmbeddingService(es ai.EmbeddingService)
SetEmbeddingService sets the embedding service for semantic similarity matching.
type Intent ¶
type Intent string
Intent represents the type of user intent.
const ( IntentMemoSearch Intent = "memo_search" IntentMemoCreate Intent = "memo_create" IntentScheduleQuery Intent = "schedule_query" IntentScheduleCreate Intent = "schedule_create" IntentScheduleUpdate Intent = "schedule_update" IntentBatchSchedule Intent = "batch_schedule" IntentAmazing Intent = "amazing" IntentUnknown Intent = "unknown" )
func AgentTypeToIntent ¶
AgentTypeToIntent converts AgentType to default Intent. Used when a specific intent subtype cannot be determined.
type LLMClassifier ¶
type LLMClassifier struct {
// contains filtered or unexported fields
}
LLMClassifier implements Layer 3 LLM-based intent classification. Target: ~400ms latency, handle only ~20% of requests that pass Layer 1&2.
func NewLLMClassifier ¶
func NewLLMClassifier(client LLMClient) *LLMClassifier
NewLLMClassifier creates a new LLM classifier.
func (*LLMClassifier) Classify ¶
func (c *LLMClassifier) Classify(ctx context.Context, input string) (*LLMClassifyResult, error)
Classify classifies user intent using LLM. This is the fallback layer for truly ambiguous inputs.
type LLMClassifyResult ¶
LLMClassifyResult contains the result of LLM classification.
type LLMClient ¶
type LLMClient interface {
// Complete sends a completion request and returns the response.
Complete(ctx context.Context, prompt string, config ModelConfig) (string, error)
}
LLMClient defines the interface for LLM API calls.
type MockRouterService ¶
type MockRouterService struct {
// IntentOverrides allows tests to override intent classification results
IntentOverrides map[string]Intent
// ModelOverrides allows tests to override model selection results
ModelOverrides map[TaskType]ModelConfig
}
MockRouterService is a mock implementation of RouterService for testing.
func NewMockRouterService ¶
func NewMockRouterService() *MockRouterService
NewMockRouterService creates a new MockRouterService.
func (*MockRouterService) ClassifyIntent ¶
func (m *MockRouterService) ClassifyIntent(ctx context.Context, input string) (Intent, float32, error)
ClassifyIntent classifies user intent using rule-based matching.
func (*MockRouterService) SelectModel ¶
func (m *MockRouterService) SelectModel(ctx context.Context, task TaskType) (ModelConfig, error)
SelectModel selects an appropriate model based on task type.
type ModelConfig ¶
type ModelConfig struct {
Provider string `json:"provider"` // local/cloud
Model string `json:"model"` // model name
MaxTokens int `json:"max_tokens"`
Temperature float32 `json:"temperature"`
}
ModelConfig represents the configuration for a model.
type RouterService ¶
type RouterService interface {
// ClassifyIntent classifies user intent from input text.
// Returns: intent type, confidence (0-1), error
// Implementation: rule-based first (0ms) -> LLM fallback (~400ms)
ClassifyIntent(ctx context.Context, input string) (Intent, float32, error)
// SelectModel selects an appropriate model based on task type.
// Returns: model configuration (local/cloud)
SelectModel(ctx context.Context, task TaskType) (ModelConfig, error)
}
RouterService defines the LLM routing service interface. Consumers: Team B (Assistant+Schedule), Team C (Memo Enhancement)
type RuleMatcher ¶
type RuleMatcher struct {
// contains filtered or unexported fields
}
RuleMatcher implements Layer 1 rule-based intent matching. Target: 0ms latency, handle 60%+ of requests.
func NewRuleMatcher ¶
func NewRuleMatcher() *RuleMatcher
NewRuleMatcher creates a new rule matcher with predefined keyword weights.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements the three-layer RouterService. Layer 1: Rule-based matching (0ms) - handles 60%+ requests Layer 2: History matching (~10ms) - handles 20%+ requests Layer 3: LLM classification (~400ms) - fallback for remaining ~20%
func (*Service) ClassifyIntent ¶
ClassifyIntent classifies user intent from input text. Returns: intent type, confidence (0-1), error Implementation: rule-based first (0ms) -> history match (~10ms) -> LLM fallback (~400ms)
func (*Service) SelectModel ¶
SelectModel selects an appropriate model based on task type. Returns: model configuration (local/cloud)
type TaskType ¶
type TaskType string
TaskType represents the type of task for model selection.
const ( TaskIntentClassification TaskType = "intent_classification" TaskEntityExtraction TaskType = "entity_extraction" TaskSimpleQA TaskType = "simple_qa" TaskComplexReasoning TaskType = "complex_reasoning" TaskSummarization TaskType = "summarization" TaskTagSuggestion TaskType = "tag_suggestion" )