Skip to content

[AI优化] 轻量 ML 意图分类器 - 减少 LLM 路由调用 #96

@hrygo

Description

@hrygo

问题描述

当规则匹配失败时,需要调用 LLM 进行意图分类:

// ai/router/service.go - 当前实现
func (s *routerServiceImpl) ClassifyIntent(input string, history []string) (*RoutingDecision, error) {
    // Layer 1: Cache
    // Layer 2: Rule matching
    // Layer 3: History matching
    // Layer 4: LLM fallback (耗时 ~100-200ms, 消耗 tokens)
    return s.llmClassify(input, history)
}

问题:

  • LLM 调用延迟 ~100-200ms
  • 消耗 tokens (成本)
  • 网络依赖

建议方案

训练轻量级 ML 模型替代部分 LLM 调用:

type TinyClassifier struct {
    model     *onnx.Model  // ONNX 格式的小模型
    tokenizer *Tokenizer
    threshold float32      // 置信度阈值 (建议 0.85)
}

func (c *TinyClassifier) Predict(input string) (AgentType, Intent, float32) {
    // 1. 分词
    tokens := c.tokenizer.Encode(input)
    
    // 2. 推理
    output := c.model.Run(tokens)
    
    // 3. 解析结果
    agentProbs := output[:5]   // 5 种 AgentType
    intentProbs := output[5:]  // 6 种 Intent
    
    agentIdx, agentConf := argmax(agentProbs)
    intentIdx, intentConf := argmax(intentProbs)
    
    return AgentType(agentIdx), Intent(intentIdx), min(agentConf, intentConf)
}

技术选型

选项 模型大小 推理延迟 准确率预估
DistilBERT-tiny ~15MB ~10ms 85-90%
TextCNN ~2MB ~2ms 80-85%
FastText ~5MB <1ms 75-80%

推荐: TextCNN (平衡大小和准确率)

训练流程

# 1. 从历史日志提取训练数据
python scripts/extract_training_data.py \
    --input logs/ai_chat.jsonl \
    --output data/intent_training.csv

# 2. 训练模型
python scripts/train_classifier.py \
    --data data/intent_training.csv \
    --model textcnn \
    --output models/intent_classifier.onnx

# 3. 验证准确率
python scripts/evaluate_classifier.py \
    --model models/intent_classifier.onnx \
    --test data/intent_test.csv

路由流程调整

func (s *routerServiceImpl) ClassifyIntent(input string, history []string) (*RoutingDecision, error) {
    // Layer 1: Cache (不变)
    
    // Layer 2: Rule matching (不变)
    
    // Layer 3: ML Classifier (新增)
    if s.mlClassifier != nil {
        agent, intent, conf := s.mlClassifier.Predict(input)
        if conf >= s.mlThreshold { // 0.85
            return &RoutingDecision{
                AgentType:  agent,
                Intent:     intent,
                Confidence: conf,
            }, nil
        }
    }
    
    // Layer 4: History matching (不变)
    
    // Layer 5: LLM fallback (不变)
    return s.llmClassify(input, history)
}

文件变更

  • ai/router/classifier.go - ML 分类器 Go 封装
  • ai/router/classifier_test.go - 单元测试
  • ai/router/service.go - 集成到路由流程
  • scripts/extract_training_data.py - 数据提取脚本
  • scripts/train_classifier.py - 训练脚本
  • models/intent_classifier.onnx - 训练好的模型

验收标准

  • 准确率 >85% 才启用
  • 推理延迟 <10ms
  • 模型大小 <10MB
  • 支持模型热更新
  • 回退到 LLM 的机制完善
  • 提供模型效果监控

预估工时

5-7 天

技术风险

  • 需要足够的训练数据
  • 模型可能有偏见
  • 需要持续迭代

相关 Issue

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions