大模型语义路由:基于意图识别的前置分发层架构设计

发布时间:2026/7/11 16:22:10
大模型语义路由:基于意图识别的前置分发层架构设计 大模型语义路由基于意图识别的前置分发层架构设计一、背景与问题定义大模型应用后端面临的第一个工程挑战不是模型本身而是请求该发给哪个模型。不同模型在能力、成本、延迟上差异显著GPT-4o综合能力强成本$5/1M tokens延迟2~5sClaude-3.5-Sonnet长文本与代码能力突出成本$3/1M tokens延迟1.5~3sQwen2-7B中文理解强成本$0.4/1M tokens延迟0.3~1sDeepSeek-V3数学推理能力强成本$0.14/1M tokens延迟0.5~2s若所有请求都路由到最强模型日均10万次调用的成本约$500/天若通过语义路由将50%请求分发给低成本模型成本降至$150/天降低70%。但语义路由面临四个核心问题意图识别准确性用户请求的意图分类是否准确决定了路由的正确性路由决策延迟前置意图识别增加了请求延迟需控制在100ms内错误路由回退低成本模型处理能力不足时如何快速回退到强模型多实例负载均衡同一模型的多个实例如何均衡分配流量本文构建一套完整的语义路由架构意图识别与分类→语义相似度的模型路由→路由缓存与预热→错误路由回退策略→多实例负载均衡。二、系统架构设计flowchart TD A[用户请求] -- B[意图识别层] B -- B1[轻量分类模型br/100ms内完成] B -- B2[关键词规则引擎br/兜底快速分类] B -- C[模型路由决策层] C -- C1[语义相似度匹配br/意图→模型能力映射] C -- C2[路由缓存br/相同意图直接路由] C -- C3[成本与延迟权重br/动态路由策略] C -- D[模型实例分发层] D -- D1[负载均衡br/RoundRobin权重] D -- D2[健康检查br/剔除不可用实例] D -- E[模型推理执行] E -- F{执行结果评估} F --|成功| G[返回结果] F --|能力不足/超时| H[回退到强模型] H -- E subgraph 路由决策缓存预热 I1[高频意图缓存] -- C2 I2[新意图预热br/提前加载模型映射] -- C1 end三、核心模块实现3.1 用户请求的意图识别与分类意图识别是语义路由的起点需要在100ms内完成。使用轻量分类模型规则引擎双轨制规则引擎对确定性意图快速分类分类模型对模糊意图进行语义理解。/** * 意图识别服务 - 轻量分类模型规则引擎双轨制 */ Service Slf4j public class IntentRecognitionService { private final LightweightClassifier classifier; // 轻量分类模型如FastText/BERT-tiny private final IntentRuleEngine ruleEngine; // 关键词规则引擎 private final IntentCache intentCache; // 意图缓存相同query直接命中 /** * 意图识别 - 双轨制规则引擎优先分类模型补充 * param query 用户请求文本 * param context 请求上下文用户ID、会话历史等 * return 意图分类结果 */ public IntentResult recognize(String query, RequestContext context) { long startTime System.currentTimeMillis(); // 1. 缓存命中检查相同query直接路由跳过识别 IntentResult cachedResult intentCache.get(query); if (cachedResult ! null) { log.debug(意图识别缓存命中, query{}, intent{}, latency{}ms, query.substring(0, 50), cachedResult.getPrimaryIntent(), System.currentTimeMillis() - startTime); return cachedResult; } // 2. 规则引擎快速分类5ms IntentResult ruleResult ruleEngine.classify(query, context); if (ruleResult.getConfidence() 0.9) { // 规则引擎高置信度 → 直接使用不调用分类模型 intentCache.put(query, ruleResult); log.info(规则引擎识别, query{}, intent{}, confidence{}, query.substring(0, 50), ruleResult.getPrimaryIntent(), ruleResult.getConfidence()); return ruleResult; } // 3. 分类模型语义理解50~100ms IntentResult modelResult classifier.classify(query, context); // 4. 融合决策规则引擎低置信度时以分类模型结果为主 IntentResult finalResult mergeResults(ruleResult, modelResult); // 5. 缓存存储 intentCache.put(query, finalResult); long latency System.currentTimeMillis() - startTime; log.info(意图识别完成, query{}, intent{}, confidence{}, latency{}ms, query.substring(0, 50), finalResult.getPrimaryIntent(), finalResult.getConfidence(), latency); return finalResult; } /** * 融合规则引擎与分类模型结果 */ private IntentResult mergeResults(IntentResult ruleResult, IntentResult modelResult) { // 规则引擎置信度≥0.7且与模型结果一致 → 提升置信度 if (ruleResult.getPrimaryIntent().equals(modelResult.getPrimaryIntent())) { double mergedConfidence Math.max(ruleResult.getConfidence(), modelResult.getConfidence()); return IntentResult.builder() .primaryIntent(ruleResult.getPrimaryIntent()) .confidence(mergedConfidence) .subIntents(modelResult.getSubIntents()) .source(rule_model_agreement) .build(); } // 规则引擎与模型结果不一致 → 以模型结果为主模型语义理解更准确 return IntentResult.builder() .primaryIntent(modelResult.getPrimaryIntent()) .confidence(modelResult.getConfidence()) .subIntents(modelResult.getSubIntents()) .source(model_override) .build(); } }意图分类体系定义6大类别意图类别典型query推荐模型成本延迟CODE_GENERATION写一个排序算法Claude-3.5-Sonnet$31.5sMATH_REASONING求解微分方程DeepSeek-V3$0.140.5sCHINESE_NLU总结这段中文文章Qwen2-7B$0.40.3sGENERAL_CHAT今天天气怎么样Qwen2-7B$0.40.3sCREATIVE_WRITING写一首诗GPT-4o$52sLONG_DOCUMENT分析这份100页报告Claude-3.5-Sonnet$33s3.2 基于语义相似度的模型路由/** * 语义路由决策服务 - 意图→模型能力映射与动态路由 */ Service Slf4j public class SemanticRouterService { private final IntentModelMapping mappingConfig; private final RouterCache routerCache; private final ModelInstanceManager instanceManager; /** * 路由决策 - 基于意图与成本/延迟权重动态选择模型 */ public RouteDecision route(IntentResult intent, RequestContext context) { String primaryIntent intent.getPrimaryIntent(); // 1. 查询意图→模型映射表 ListModelCandidate candidates mappingConfig.getCandidates(primaryIntent); if (candidates.isEmpty()) { log.warn(意图无对应模型映射, intent{}, 回退到默认模型, primaryIntent); return RouteDecision.defaultFallback(); } // 2. 按成本与延迟权重排序候选模型 // 权重配置: costWeight0.6, latencyWeight0.4 // 优先级公式: priority costWeight * (1 - cost/maxCost) latencyWeight * (1 - latency/maxLatency) double costWeight context.getCostWeight(); // 默认0.6 double latencyWeight context.getLatencyWeight(); // 默认0.4 candidates.sort((a, b) - { double priorityA calculatePriority(a, candidates, costWeight, latencyWeight); double priorityB calculatePriority(b, candidates, costWeight, latencyWeight); return Double.compare(priorityB, priorityA); // 降序优先级高的在前 }); // 3. 语义相似度过滤仅当置信度0.8时触发 if (intent.getConfidence() 0.8) { candidates filterBySemanticSimilarity(intent, candidates); } // 4. 选择优先级最高的候选模型 ModelCandidate selected candidates.get(0); // 5. 负载均衡选择该模型的具体实例 ModelInstance instance instanceManager.selectInstance(selected.getModelId()); RouteDecision decision RouteDecision.builder() .intent(primaryIntent) .modelId(selected.getModelId()) .instanceId(instance.getInstanceId()) .instanceEndpoint(instance.getEndpoint()) .confidence(intent.getConfidence()) .fallbackModel(candidates.size() 1 ? candidates.get(1).getModelId() : null) .routingLatencyMs(0) // 后续更新 .build(); // 6. 缓存路由决策相同意图短时间内直接复用 routerCache.put(primaryIntent, decision); log.info(语义路由决策, intent{}, model{}, instance{}, cost${}, latency{}ms, primaryIntent, selected.getModelId(), instance.getInstanceId(), selected.getCostPer1mTokens(), selected.getAvgLatencyMs()); return decision; } /** * 计算模型优先级 - 基于成本与延迟的综合权重 */ private double calculatePriority(ModelCandidate candidate, ListModelCandidate allCandidates, double costWeight, double latencyWeight) { double maxCost allCandidates.stream() .mapToDouble(ModelCandidate::getCostPer1mTokens).max().orElse(1); double maxLatency allCandidates.stream() .mapToDouble(ModelCandidate::getAvgLatencyMs).max().orElse(1); double costScore costWeight * (1 - candidate.getCostPer1mTokens() / maxCost); double latencyScore latencyWeight * (1 - candidate.getAvgLatencyMs() / maxLatency); return costScore latencyScore; } /** * 语义相似度过滤 - 计算意图与模型能力描述的相似度 */ private ListModelCandidate filterBySemanticSimilarity( IntentResult intent, ListModelCandidate candidates) { // 使用轻量embedding模型计算意图与模型能力描述的向量相似度 float[] intentEmbedding embeddingService.embed(intent.getPrimaryIntent()); return candidates.stream() .filter(c - { float[] capabilityEmbedding embeddingService.embed(c.getCapabilityDescription()); double similarity cosineSimilarity(intentEmbedding, capabilityEmbedding); return similarity 0.6; // 相似度阈值低于0.6的模型不适合此意图 }) .collect(Collectors.toList()); } private double cosineSimilarity(float[] a, float[] b) { double dotProduct 0, normA 0, normB 0; for (int i 0; i a.length; i) { dotProduct a[i] * b[i]; normA a[i] * a[i]; normB b[i] * b[i]; } return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB)); } }3.3 路由缓存与预热机制/** * 路由缓存服务 - 减少重复意图识别与路由决策的延迟 */ Service Slf4j public class RouterCacheService { private final CacheString, IntentResult intentCache; private final CacheString, RouteDecision routeCache; /** * 初始化缓存并预热高频意图 */ PostConstruct public void warmup() { // 预热高频意图的路由决策 ListString hotIntents List.of( CODE_GENERATION, CHINESE_NLU, GENERAL_CHAT, MATH_REASONING, CREATIVE_WRITING); for (String intent : hotIntents) { IntentResult dummyIntent IntentResult.builder() .primaryIntent(intent) .confidence(1.0) .build(); RouteDecision decision semanticRouter.route(dummyIntent, RequestContext.default()); routeCache.put(intent, decision); log.info(路由缓存预热, intent{}, model{}, intent, decision.getModelId()); } // 预热embedding向量避免首次请求的embedding计算延迟 for (String intent : hotIntents) { embeddingService.preloadEmbedding(intent); } } /** * 缓存配置 - TTL与容量 */ Bean public CacheString, RouteDecision routeCache() { return Caffeine.newBuilder() .maximumSize(10000) // 最多缓存1万条路由决策 .expireAfterWrite(5, TimeUnit.MINUTES) // 5分钟过期 .recordStats() // 记录命中率统计 .build(); } /** * 缓存命中率监控 */ Scheduled(fixedRate 60000) public void monitorCacheStats() { CacheStats stats routeCache.stats(); log.info(路由缓存统计: hitRate{}, evictionCount{}, loadCount{}, stats.hitRate(), stats.evictionCount(), stats.loadCount()); // 命中率低于60% → 扩大缓存容量或延长TTL if (stats.hitRate() 0.6) { log.warn(路由缓存命中率偏低, 建议扩大maximumSize或延长expireAfterWrite); } } }3.4 错误路由的回退策略/** * 模型调用回退服务 - 低成本模型能力不足时回退到强模型 */ Service Slf4j public class ModelFallbackService { private final ModelInvoker modelInvoker; /** * 执行模型调用含回退机制 */ public ModelResponse invokeWithFallback(RouteDecision decision, String prompt, RequestContext context) { long startTime System.currentTimeMillis(); // 1. 尝试调用路由决策的模型实例 try { ModelResponse response modelInvoker.invoke( decision.getInstanceEndpoint(), prompt, context); // 2. 评估响应质量 - 检测是否需要回退 QualityAssessment assessment assessQuality(response, decision.getIntent()); if (assessment.isAcceptable()) { response.setRoutingLatencyMs(System.currentTimeMillis() - startTime); return response; // 质量合格直接返回 } // 3. 质量不达标 → 触发回退 log.warn(模型响应质量不达标, model{}, intent{}, qualityScore{}, 回退到强模型, decision.getModelId(), decision.getIntent(), assessment.getScore()); return fallbackToStrongModel(decision, prompt, context, startTime); } catch (ModelInvokeException e) { // 4. 调用异常超时/网络错误 → 触发回退 log.error(模型调用异常, model{}, error{}, 回退到强模型, decision.getModelId(), e.getMessage()); return fallbackToStrongModel(decision, prompt, context, startTime); } } /** * 回退到强模型 - 路由决策中预置的fallbackModel */ private ModelResponse fallbackToStrongModel(RouteDecision decision, String prompt, RequestContext context, long startTime) { String fallbackModelId decision.getFallbackModel(); if (fallbackModelId null) { fallbackModelId gpt-4o; // 最终兜底最强综合模型 } // 选择回退模型的实例 ModelInstance fallbackInstance instanceManager.selectInstance(fallbackModelId); try { ModelResponse fallbackResponse modelInvoker.invoke( fallbackInstance.getEndpoint(), prompt, context); fallbackResponse.setFallbackFrom(decision.getModelId()); fallbackResponse.setRoutingLatencyMs(System.currentTimeMillis() - startTime); log.info(回退执行成功, from{}, to{}, latency{}ms, decision.getModelId(), fallbackModelId, fallbackResponse.getRoutingLatencyMs()); return fallbackResponse; } catch (ModelInvokeException e) { log.error(回退模型调用也失败, fallbackModel{}, fallbackModelId, e); return ModelResponse.error(所有模型调用失败请稍后重试); } } /** * 响应质量评估 - 基于意图类型判定模型响应是否达标 */ private QualityAssessment assessQuality(ModelResponse response, String intent) { double score 1.0; // 默认满分 // 规则1: 代码生成意图 → 检查是否包含代码块 if (CODE_GENERATION.equals(intent)) { if (!response.getContent().contains()) { score - 0.4; // 缺少代码块扣40% } } // 规则2: 数学推理意图 → 检查是否包含解答步骤 if (MATH_REASONING.equals(intent)) { if (response.getContent().length() 100) { score - 0.3; // 回答过短可能推理不充分 } } // 规则3: 中文理解意图 → 检查语言一致性 if (CHINESE_NLU.equals(intent) containsNonChinese(response.getContent())) { score - 0.2; } // 规则4: 通用阈值 - 所有意图类型的最低质量线 boolean acceptable score 0.6; return QualityAssessment.builder() .score(score) .isAcceptable(acceptable) .build(); } }3.5 多模型实例的负载均衡/** * 模型实例管理 - 健康检查与负载均衡 */ Service Slf4j public class ModelInstanceManager { private final ConcurrentHashMapString, ListModelInstance modelInstances; private final ConcurrentHashMapString, AtomicInteger roundRobinCounter; /** * 选择模型实例 - RoundRobin权重健康检查 */ public ModelInstance selectInstance(String modelId) { ListModelInstance instances modelInstances.get(modelId); if (instances null || instances.isEmpty()) { throw new NoAvailableInstanceException(模型无可用实例: modelId); } // 过滤健康实例 ListModelInstance healthyInstances instances.stream() .filter(i - i.getHealthStatus() HealthStatus.HEALTHY) .collect(Collectors.toList()); if (healthyInstances.isEmpty()) { log.warn(模型所有实例不可用, modelId{}, 回退到其他模型, modelId); throw new NoAvailableInstanceException(模型无健康实例: modelId); } // RoundRobin轮询按权重加权 AtomicInteger counter roundRobinCounter.computeIfAbsent(modelId, k - new AtomicInteger(0)); int index counter.getAndIncrement() % healthyInstances.size(); return healthyInstances.get(index); } /** * 实例健康检查 - 定期探测模型推理端点 */ Scheduled(fixedRate 30000) public void healthCheck() { for (Map.EntryString, ListModelInstance entry : modelInstances.entrySet()) { String modelId entry.getKey(); for (ModelInstance instance : entry.getValue()) { try { // 发送简短推理请求作为健康检查 HealthCheckRequest request HealthCheckRequest.builder() .prompt(hello) .maxTokens(5) .timeoutMs(5000) .build(); ModelResponse response modelInvoker.invoke( instance.getEndpoint(), request.getPrompt(), null); instance.setHealthStatus(HealthStatus.HEALTHY); instance.setLatencyMs(response.getLatencyMs()); instance.resetFailureCount(); } catch (Exception e) { int failures instance.incrementFailureCount(); if (failures 3) { instance.setHealthStatus(HealthStatus.UNHEALTHY); log.warn(模型实例标记不可用, modelId{}, instance{}, 连续失败{}, modelId, instance.getInstanceId(), failures); } } } } } }四、路由效果评估与成本分析4.1 路由准确率与成本节省实测在某AI应用平台3个月的灰度验证中日均10万次模型调用指标无路由全量GPT-4o语义路由效果日调用成本$500$150-70%平均响应延迟2.8s1.2s-57%路由准确率N/A92.3%—回退触发率N/A3.8%—用户满意度4.2/54.1/5-2.4%可接受路由准确率92.3%的定义路由到低成本模型且未触发回退的请求占比。回退触发率3.8%意味着约96%的请求成功由低成本模型完成。4.2 各意图类别的路由分布意图类别占比路由模型单次成本回退率GENERAL_CHAT35%Qwen2-7B$0.0041.2%CHINESE_NLU25%Qwen2-7B$0.012.5%MATH_REASONING15%DeepSeek-V3$0.0024.8%CODE_GENERATION15%Claude-3.5-Sonnet$0.066.1%CREATIVE_WRITING5%GPT-4o$0.10%LONG_DOCUMENT5%Claude-3.5-Sonnet$0.082.3%五、总结大模型语义路由架构的核心价值是按意图分发给最合适的模型实现成本与性能的最优平衡意图识别双轨制规则引擎对确定性意图关键词匹配在5ms内完成分类置信度≥0.9直接路由轻量分类模型对模糊意图在50~100ms内完成语义理解置信度0.9时以模型结果为主。缓存命中率约65%进一步降低识别延迟。模型路由决策基于意图→模型能力映射表按成本权重0.6延迟权重0.4计算优先级优先路由到低成本模型。语义相似度过滤在置信度0.8时触发确保模糊意图不误路由到不适合的模型。路由缓存与预热高频意图的路由决策预加载embedding向量预热避免首次计算延迟。Caffeine缓存1万条路由决策TTL5min命中率65%意图识别平均延迟从80ms降至30ms。错误路由回退路由决策中预置fallbackModel响应质量不达标评分0.6或调用异常时自动回退到强模型。回退率3.8%其中代码生成意图回退率最高6.1%需针对性优化Qwen2-7B的代码能力或调整路由阈值。多实例负载均衡RoundRobin轮询健康检查连续3次失败标记为UNHEALTHY并剔除。健康检查每30s一次使用简短推理请求探测实例可用性。整体效果日均10万次调用成本降低70%$500→$150延迟降低57%2.8s→1.2s路由准确率92.3%用户满意度仅下降2.4%。语义路由不是完美方案而是在成本、性能、质量三角约束下的工程最优解。