【广告营销Agent实战11】Agent路由优化 – 提升意图识别准确率
🎯 Multi-Agent 系统的核心是路由决策。路由错了,再强的 Agent 也没用。这篇聊聊如何提升路由准确率。
路由问题现象
问题1:意图识别错误
用户:”分析一下分期乐的市场策略” 系统:路由到 AdAnalysisAgent(❌ 应该是 MarketAgent)
问题2:边界模糊
用户:”上周投放效果怎么样?” 既可以是数据分析,也可以是通用对话
问题3:用户表达不清
用户:”帮我看看” 看什么?数据?竞品?完全不知道
路由策略演进
V1:关键词匹配(准确率 60%)
func routeByKeywords(question string) string { if strings.Contains(question, “ROI”) || strings.Contains(question, “转化率”) { return “AdAnalysisAgent” } if strings.Contains(question, “竞品”) || strings.Contains(question, “市场”) { return “MarketAgent” } return “ChatAgent”}
问题:太简单粗暴,容易误判。
V2:LLM 分类(准确率 75%)
const routingPrompt = `你是一个意图分类器。根据用户问题,判断应该使用哪个 Agent:1. AdAnalysisAgent – 数据分析、ROI、转化率等2. MarketAgent – 市场洞察、竞品分析、行业趋势3. ChatAgent – 通用对话、知识问答用户问题:{{question}}只返回 Agent 名称,不要解释。`
问题:边界模糊时容易出错。
V3:Few-shot + 规则(准确率 85%)
const routingPromptV3 = `你是一个意图分类器。Agent 说明:1. AdAnalysisAgent – 查询内部广告数据 关键词:ROI、转化率、消耗、点击量、计划、账户2. MarketAgent – 搜索外部市场信息 关键词:竞品、市场份额、行业趋势、最新动态3. ChatAgent – 通用对话 关键词:你好、谢谢、怎么用、是什么示例:Q: 上周的ROI是多少?A: AdAnalysisAgentQ: 分析一下分期乐的市场策略A: MarketAgentQ: 你好,你能做什么?A: ChatAgent用户问题:{{question}}返回:`
V4:置信度 + 二次确认(准确率 92%)
让 LLM 返回置信度,低于阈值时询问用户:
type RoutingResult struct { Agent string `json:”agent”` Confidence float64 `json:”confidence”` Reason string `json:”reason”`}func routeWithConfidence(question string) (*RoutingResult, error) { prompt := routingPromptV3 + `返回 JSON 格式:{ “agent”: “Agent名称”, “confidence”: 0.0-1.0, “reason”: “选择理由”}` response, _ := llm.Generate(prompt) var result RoutingResult json.Unmarshal([]byte(response), &result) // 置信度低于 0.7,询问用户 if result.Confidence < 0.7 { return askUserForClarification(question) } return &result, nil}
优化技巧
1. 历史上下文
考虑用户的历史对话,判断意图:
func routeWithHistory(question string, history []*Message) string { // 如果上一轮是数据分析,这一轮可能也是 if len(history) > 0 { lastAgent := history[len(history)-1].Agent if lastAgent == “AdAnalysisAgent” && isFollowUpQuestion(question) { return “AdAnalysisAgent” } } return routeByLLM(question)}func isFollowUpQuestion(q string) bool { followUpWords := []string{“那”, “呢”, “还有”, “另外”} for _, word := range followUpWords { if strings.Contains(q, word) { return true } } return false}
2. 用户画像
根据用户角色调整路由策略:
type UserProfile struct { Role string // 运营、分析师、管理层 Preference string // 偏好的 Agent}func routeWithProfile(question string, profile *UserProfile) string { // 分析师用户,优先路由到数据分析 if profile.Role == “analyst” { if containsDataKeywords(question) { return “AdAnalysisAgent” } } return routeByLLM(question)}
3. A/B 测试
对比不同路由策略的效果:
降级策略
当路由失败时,提供降级方案:
func routeWithFallback(question string) string { // 尝试主路由 agent, err := routeByLLM(question) if err != nil { // 降级到关键词匹配 agent = routeByKeywords(question) } // 如果还是不确定,默认 ChatAgent if agent == “” { agent = “ChatAgent” } return agent}
用户反馈机制
让用户纠正错误的路由:
界面提示: “我理解您想查询数据分析,对吗?” [✓ 是的] [✗ 不是,我想…]
func collectFeedback(sessionID string, correctAgent string) { // 记录用户纠正 feedback := &RoutingFeedback{ SessionID: sessionID, Question: getQuestion(sessionID), WrongAgent: getCurrentAgent(sessionID), CorrectAgent: correctAgent, } // 存储用于后续优化 db.Save(feedback) // 重新路由 rerouteToAgent(sessionID, correctAgent)}
总结
核心要点: 1. Few-shot 示例 – 显著提升准确率 2. 置信度判断 – 不确定时询问用户 3. 历史上下文 – 考虑对话连贯性 4. 用户反馈 – 持续优化路由策略
下一篇聊性能优化与监控,如何打造生产级系统。
📖 广告营销 AI Agent 开发全记录 – 第11篇
作者:树林子有理想 | 2026年6月
上一篇:流式响应优化 下一篇:性能优化与监控
树林子有理想
一个有想法,爱思考,爱折腾,爱生活,喜欢探索的技术人。
📮 公众号:老王的AI局