目录
一句话简介
🎯 核心功能
📝 个性化配置维度
💻 实现 PersonalizationProvider
数据模型
核心实现
💻 动态学习偏好
在 InvokedAsync 中学习
🧪 测试效果对比
🏢 最佳实践
✅ DO
❌ DON'T
🎯 总结
上一篇
一句话简介
实现电商智能客服的个性化配置,根据会员等级和用户偏好动态调整服务策略和语言风格。
🎯 核心功能
✅会员等级配置:VIP/普通/新用户差异化服务
✅语言风格调整:正式/随和/简洁风格切换
✅动态学习偏好:根据用户行为自动调整
✅序列化与恢复:持久化个性化配置
📝 个性化配置维度
配置维度 | 说明 | 示例值 |
|---|---|---|
| 会员等级 | VIP/普通/新用户 | 影响称呼和服务优先级 |
| 语言风格 | 正式/标准/随和/简洁 | 影响回复语气 |
| 响应长度 | 简洁/标准/详细 | 影响回复详细程度 |
| 专业度 | 技术型/非技术型 | 影响术语使用 |
💻 实现 PersonalizationProvider
数据模型
public enum MemberLevel { New, Regular, VIP } publicenum CommunicationStyle { Formal, Standard, Casual, Concise } publicenum DetailLevel { Brief, Standard, Detailed } publicclassUserProfile { publicstring? Name { get; set; } public MemberLevel MemberLevel { get; set; } = MemberLevel.Regular; public CommunicationStyle CommunicationStyle { get; set; } = CommunicationStyle.Standard; public DetailLevel ResponseDetailLevel { get; set; } = DetailLevel.Standard; publicbool IsTechnicalUser { get; set; } = false; publicint InteractionCount { get; set; } = 0; }核心实现
public sealedclassPersonalizationProvider : AIContextProvider { privatereadonly IChatClient _chatClient; public UserProfile UserProfile { get; set; } // 调用前:注入个性化 Instructions public override ValueTask<AIContext> InvokingAsync(...) { var instructions = BuildPersonalizedInstructions(); returnnew ValueTask<AIContext>(new AIContext { Instructions = instructions }); } private string BuildPersonalizedInstructions() { var sb = new StringBuilder(); // 会员等级配置 switch (UserProfile.MemberLevel) { case MemberLevel.VIP: sb.AppendLine($"User is VIP. Use respectful title: '尊敬的{UserProfile.Name}'"); sb.AppendLine("Prioritize their requests and provide premium service"); break; case MemberLevel.New: sb.AppendLine("User is new. Provide warm welcome and detailed guidance"); break; } // 语言风格配置 switch (UserProfile.CommunicationStyle) { case CommunicationStyle.Concise: sb.AppendLine("Keep responses brief. Maximum 2-3 sentences."); break; case CommunicationStyle.Formal: sb.AppendLine("Use formal and professional language. Avoid emojis."); break; } return sb.ToString(); } }💻 动态学习偏好
在 InvokedAsync 中学习
public override async ValueTask InvokedAsync(InvokedContext context, ...) { UserProfile.InteractionCount++; var userMessage = context.RequestMessages.Last().Text ?? ""; // 1️⃣ 分析消息长度,判断简洁偏好 if (userMessage.Length < 50) { UserProfile.CommunicationStyle = CommunicationStyle.Concise; } // 2️⃣ 检测用户反馈,调整详细程度 if (userMessage.Contains("太详细") || userMessage.Contains("简短")) { UserProfile.ResponseDetailLevel = DetailLevel.Brief; } // 3️⃣ 根据交互次数升级会员等级 if (UserProfile.InteractionCount >= 15 && UserProfile.MemberLevel == MemberLevel.Regular) { UserProfile.MemberLevel = MemberLevel.VIP; } // 4️⃣ 检测技术术语,标记技术用户 if (userMessage.Contains("api") || userMessage.Contains("代码")) { UserProfile.IsTechnicalUser = true; } }🧪 测试效果对比
用户类型 | 称呼 | 语气 | Emoji |
|---|---|---|---|
👑 VIP | 尊敬的王总 | 正式专业 | ❌ |
👤 普通 | 小李好 | 友好随和 | ✅ 😊 |
🆕 新用户 | 您好 | 耐心引导 | ✅ |
⚡ 简洁 | 张工 | 简洁直接 | ❌ |
🏢 最佳实践
✅ DO
分离数据(UserProfile)和逻辑(Provider)
两个构造函数支持首次创建和反序列化
异常保护,InvokedAsync 中的异常不影响主流程
引用共享,跨 Thread 时传入同一个对象引用
❌ DON'T
不要在 InvokingAsync 中执行耗时操作
不要直接修改 Agent.Instructions,通过 AIContext 注入
不要序列化服务依赖(如 IChatClient)
🎯 总结
✅多维度配置:会员等级、语言风格、响应长度、专业度
✅动态学习:根据用户行为自动调整偏好
✅职责分离:数据模型 + Provider 逻辑
✅企业应用:智能客服、学习助手、医疗问诊
下一篇
引入地址