本文档在“初学者生成式AI”课程中,将介绍如何使用大型语言模型(LLM)构建基于聊天的应用程序。聊天应用程序与文本生成应用程序(在文本生成应用程序中介绍)的区别在于,它们可以维护对话上下文,并实现交互式的、持续的对话,而不是一次性的响应。
聊天应用程序已融入我们的日常生活,提供的功能远不止于闲聊。它们被用于客户服务、技术支持和复杂的咨询系统。生成式AI通过提供更自然、更具上下文感知的交互来增强这些应用程序。
与遵循预设脚本的传统聊天机器人不同,生成式AI驱动的聊天应用程序能够:
来源:[07-building-chat-applications/README.md]
理解基于规则的传统聊天机器人与生成式AI驱动的聊天应用程序之间的区别至关重要。
| 传统聊天机器人 | 生成式AI驱动的聊天应用程序 |
|---|---|
| 面向任务且基于规则 | 上下文感知且适应性强 |
| 通常集成到更大的系统中 | 可以托管一个或多个聊天机器人 |
| 仅限于编程功能 | 集成生成式AI模型 |
| 专业的、结构化的交互 | 能够进行开放域讨论 |
来源:[07-building-chat-applications/README.md], [07-building-chat-applications/translations/cn/README.md]
生成式AI驱动的聊天应用程序通常包含以下组件:
来源:[07-building-chat-applications/README.md], [11-integrating-with-function-calling/README.md]
构建聊天应用程序最常见的方式是使用OpenAI API或Azure OpenAI服务。两者都提供类似的功能,只是实现略有不同。
对于Azure OpenAI服务
来源:[07-building-chat-applications/README.md], [06-text-generation-apps/README.md]
聊天应用程序使用 chat.completions.create() 而不是文本生成中使用的 completions.create()。主要区别在于消息的结构方式。
来源:[06-text-generation-apps/README.md], [07-building-chat-applications/README.md]
聊天补全API需要一个结构化的消息数组,每条消息都有一个角色(role)和内容(content)。
不同的消息角色包括:
来源:[07-building-chat-applications/README.md], [11-integrating-with-function-calling/README.md]
聊天应用程序的关键方面之一是随时间维护对话上下文。这通过保存和更新消息历史来实现。
典型的实现模式
来源:[07-building-chat-applications/README.md]
系统消息是定制聊天应用程序行为的强大方法。微软推荐了一个有效的系统消息框架:
有效的系统消息示例
You are an educational assistant specializing in computer science concepts.
Your responses should be clear, accurate, and suitable for students.
Always provide examples when explaining coding concepts.
Format complex code in code blocks.
If you're unsure about something, acknowledge the limitations and suggest reliable sources for further reading.
Do not provide answers to specific homework problems or exams.
来源:[07-building-chat-applications/README.md], [07-building-chat-applications/translations/cn/README.md]
构建有效的聊天应用程序需要关注用户体验。
来源:[07-building-chat-applications/README.md], [12-designing-ux-for-ai-applications/README.md]
高级聊天应用程序通常需要与外部系统集成。函数调用使模型能够:
这可以通过在API调用中定义函数来实现。
来源:[11-integrating-with-function-calling/README.md]
维护高质量的聊天应用程序需要监控关键指标并遵守负责任的AI原则。
| 指标/原则 | 描述 | 开发人员考量 |
|---|---|---|
| 正常运行时间 | 衡量应用程序运行和可访问用户的时间。 | 您将如何最大限度地减少停机时间? |
| 响应时间 | 应用程序回复用户查询所需的时间。 | 如何优化查询处理? |
| 精确率 | 真实阳性预测数与总阳性预测数之比。 | 您将如何验证模型的准确性? |
| 公平性 | AI系统应对所有人都公平。 | 确保聊天应用程序不基于用户数据进行歧视。 |
| 可靠性与安全性 | AI系统应可靠且安全地运行。 | 实施测试和保护措施以尽量减少错误。 |
| 隐私与安全 | AI系统应安全并尊重隐私。 | 实施强大的加密和数据保护。 |
| 包容性 | AI系统应赋能所有人并吸引人们。 | 设计适用于不同受众的UI/UX。 |
| 透明度 | AI系统应易于理解。 | 为AI响应提供清晰的文档。 |
| 问责制 | 人们应对AI系统负责。 | 建立清晰的审计和改进AI决策的流程。 |
来源:[07-building-chat-applications/README.md], [07-building-chat-applications/translations/cn/README.md]
将API密钥与代码分开。
高效地管理令牌。
优化温度设置。
实施错误处理。
确保负责任的AI集成。
来源:[06-text-generation-apps/README.md], [07-building-chat-applications/README.md], [12-designing-ux-for-ai-applications/README.md]
有关构建聊天应用程序的更多信息,请参阅: