aws-samples/sample-stop-ai-agent-hallucinations-workshop

GitHub: aws-samples/sample-stop-ai-agent-hallucinations-workshop

提供 6 个渐进式实操演示,通过 Graph-RAG、语义工具选择、多 Agent 验证、神经符号护栏和 Agent 引导五种技术,系统性解决 AI Agent 在生产环境中的幻觉问题并部署到 Amazon Bedrock。

Stars: 3 | Forks: 0

# 如何阻止 AI Agent 幻觉:5 种技术 + 在 Amazon Bedrock AgentCore 上的生产部署 [![Python](https://img.shields.io/badge/Python-3.9+-3776AB.svg?style=flat&logo=python&logoColor=white)](https://python.org) [![Strands Agents](https://img.shields.io/badge/Strands_Agents-1.27+-00B4D8.svg?style=flat)](https://strandsagents.com) [![Amazon Bedrock](https://img.shields.io/badge/Amazon-Bedrock-FF9900.svg?style=flat&logo=amazon-aws)](https://aws.amazon.com/bedrock/) [![Neo4j](https://img.shields.io/badge/Neo4j-Graph--RAG-4581C3.svg?style=flat&logo=neo4j)](https://neo4j.com) [![License: MIT-0](https://img.shields.io/badge/License-MIT--0-green.svg)](LICENSE) [![Last Updated](https://img.shields.io/badge/Updated-March_2026-brightgreen.svg?style=flat)]() **AI agent 幻觉**是指 agent 伪造统计数据、选择错误的工具、无视业务规则,或者在操作失败时谎报成功。本研讨会提供了 5 种实操技术 —— Graph-RAG、语义工具选择、多 Agent 验证、神经符号护栏和 Agent 引导 —— 以及在 Amazon Bedrock AgentCore 上的生产部署演示。 本项目基于 [Strands Agents](https://strandsagents.com) 和 Amazon Bedrock 构建。相同的模式同样适用于 LangGraph、AutoGen、CrewAI 或任何其他 agent 框架。 ![Why AI Agents Fail — 6 progressive demos from hallucination research to production deployment on AWS](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/7091926d98195759.png) ## Graph-RAG 与标准 RAG:为什么它对解决幻觉问题很重要 | 方法 | 幻觉风险 | 检索方法 | 最适用场景 | |---|---|---|---| | 标准 RAG (向量) | 高 — 即使在不相关时也会返回相似内容 | 余弦相似度 | 通用问答 | | Graph-RAG (Neo4j) | 降低 73% — 基于实体关系 | 图遍历 + Cypher | 结构化领域(酒店、产品、金融) | ## 每个演示解决了什么问题? | # | 演示 | 解决的问题 | 关键结果 | 技术栈 | |:-:|------|----------------|------------|-------| | 01 | [Graph-RAG 与 RAG](./01-graphrag-demo/) | 伪造统计数据、不完整检索、域外幻觉 | 使用知识图谱减少 73% 的幻觉 | ![Neo4j](https://img.shields.io/badge/Neo4j-4581C3?style=flat&logo=neo4j&logoColor=white) ![FAISS](https://img.shields.io/badge/FAISS-blue?style=flat) | | 02 | [语义工具选择](./02-semantic-tools-demo/) | 工具选择错误、大规模 token 浪费(29 个工具) | 减少 89% 的 token,准确率更高 | ![FAISS](https://img.shields.io/badge/FAISS-blue?style=flat) ![Embeddings](https://img.shields.io/badge/Embeddings-teal?style=flat) | | 03 | [多 Agent 验证](./03-multiagent-demo/) | 未被检测到的幻觉、捏造的回复 | Executor-Validator-Critic 交叉检查流水线 | ![Swarm](https://img.shields.io/badge/Swarm-green?style=flat) | | 04 | [神经符号护栏](./04-neurosymbolic-demo/) | Agent 无视提示词中的业务规则 | 通过生命周期钩子强制执行符号规则 | ![Hooks](https://img.shields.io/badge/Hooks-purple?style=flat) | | 05 | [Agent 控制引导](./05-steering-demo/) | 硬性阻断导致任务停止而非修复问题 | Agent 自我纠正而非失败 | ![Agent Control](https://img.shields.io/badge/Agent_Control-orange?style=flat) | | 06 | [Amazon Bedrock AgentCore](./06-agentcore-boto3-demo/) | 将所有 5 种技术应用于 AWS 生产环境 | 部署在 Amazon Bedrock AgentCore 上并使用 Lambda + DynamoDB 的酒店预订 agent | ![AgentCore](https://img.shields.io/badge/Bedrock-AgentCore-FF9900?style=flat&logo=amazon-aws) ![DynamoDB](https://img.shields.io/badge/DynamoDB-4053D6?style=flat&logo=amazon-dynamodb) ![Lambda](https://img.shields.io/badge/Lambda-FF9900?style=flat&logo=aws-lambda) | ## 这 6 个演示是如何循序渐进的? 每个演示都建立在上一个的基础之上。您可以独立运行任何一个演示,但学习路径是循序渐进设计的: **阶段 1 — 理解失败原因:** 演示 01-02 展示了 agent *为什么*会失败 —— 基于学术研究支持的 RAG 幻觉和工具选择错误。 **阶段 2 — 检测与预防:** 演示 03-04 展示了*如何捕获*失败 —— 多 Agent 验证和符号规则强制执行。 **阶段 3 — 自我纠正:** 演示 05 展示了*如何引导* agent 进行自我纠正,而不是阻断它(Agent Control)。 **阶段 4 — 部署到生产环境:** 演示 06 展示了*如何将*所有 5 种技术交付到 AWS 生产环境,使用 Amazon Bedrock AgentCore、DynamoDB 和 Lambda。 ## 如何运行这些演示? ### 前置条件 - Python 3.9+ - [uv](https://docs.astral.sh/uv/) 包管理器 - 具备 [Amazon Bedrock](https://aws.amazon.com/bedrock/) 访问权限的 AWS 账户(在您所在的区域已启用 Claude Sonnet 4) ### 运行任意演示 ``` cd 01-graphrag-demo # or any demo folder uv venv && uv pip install -r requirements.txt # 运行 demo uv run .py # 或者在您的 IDE(VS Code、Kiro 或任何支持 notebook 的编辑器)中打开 .ipynb notebook ``` 每个演示的 README 中都有具体的设置说明和前置条件。 ### Neo4j 设置(演示 01) 演示 01 需要一个 Neo4j 实例来构建和查询知识图谱。 **作为研讨会的一部分运行:** 将提供 Neo4j Aura 实例。您将在会议开始时收到连接凭据(`NEO4J_URI`、`NEO4J_USERNAME`、`NEO4J_PASSWORD`)—— 请将它们添加到 `01-graphrag-demo/` 下的 `.env` 文件中。 **独立运行:** 创建您自己的免费 Neo4j Aura 实例: 1. 前往 [console.neo4j.io](https://console.neo4j.io) 创建一个免费的 **AuraDB** 实例 2. 在提示时下载凭据文件 —— 它包含您的 URI、用户名和密码 3. 在 `01-graphrag-demo/` 中创建一个 `.env` 文件: NEO4J_URI=neo4j+s://.databases.neo4j.io NEO4J_USERNAME=neo4j NEO4J_PASSWORD= 4. 在您的 Aura 实例设置中启用 **APOC** 插件 5. 运行 `build_graph_lite.py`(30 个文档,约 15 分钟)或 `build_graph.py`(300 个文档,约 2 小时)来填充图谱 ## 常见问题解答 ### 这个代码库解决了哪些类型的 AI agent 幻觉? 这个代码库解决了四大类问题:**(1)** 伪造统计数据 —— 当 RAG agent 猜测数字而不是计算它们时;**(2)** 错误的工具选择 —— 当 agent 从大型工具集中选择不合适的工具时;**(3)** 违反业务规则 —— 当 agent 无视仅在提示词中表达的约束时;**(4)** 未被检测到的失败 —— 当单个 agent 未经验证就声称成功时。 ### 我可以将这些模式用于 Strands Agents 以外的框架吗? 可以。这些模式(Graph-RAG、语义工具过滤、多 Agent 验证、神经符号护栏、引导控制)是框架无关的概念。这些演示使用了 Strands Agents,但相同的方法同样适用于 LangGraph、AutoGen、CrewAI、Haystack 或自定义实现。关键的洞察在于架构,而不是特定于某个框架。 ### 我需要 AWS 账户才能运行这些演示吗? 是的。所有演示都默认使用 Amazon Bedrock (Claude Sonnet 4) 作为 LLM 提供商。您需要拥有一个在所在区域启用了 Bedrock 访问权限的 AWS 账户。 ### 运行每个演示需要多长时间? 演示 02-05 可在 5 分钟内运行完毕。演示 01 提供了精简模式(30 个文档,约 15 分钟)和完整模式(300 个文档,约 2 小时)来构建知识图谱。 ### 支持哪些 LLM 提供商? 所有演示默认使用 Amazon Bedrock (Claude Sonnet 4),但也支持 Strands Agents 支持的任何提供商:Anthropic API、OpenAI、Ollama(本地模型)或任何兼容 OpenAI 的端点。有关配置,请参阅 [Strands Model Providers](https://strandsagents.com/docs/user-guide/concepts/model-providers/amazon-bedrock/)。 ## 常见问题及其解决方法 **Bedrock 访问被拒绝:** 请确保模型(`us.anthropic.claude-sonnet-4-5` 或类似模型)已通过 [Bedrock Model Access console](https://console.aws.amazon.com/bedrock/home#/modelaccess) 在您的区域中启用。 **Neo4j 连接失败(演示 01):** 验证 `.env` 文件中是否设置了 `NEO4J_URI`、`NEO4J_USERNAME` 和 `NEO4J_PASSWORD`,并确保您的 Aura 实例已启用 APOC。 **OpenTelemetry 警告:** 演示 03-05 中出现的 "Failed to detach context" 警告是无害的,不会影响功能。 **模型替代方案:** 通过修改 `BedrockModel(model_id=...)` 调用,可以更改任何演示中的模型。有关所有支持的选项,请参阅 [Strands Model Providers](https://strandsagents.com/docs/user-guide/concepts/model-providers/amazon-bedrock/)。 有关特定演示的问题,请查看每个演示 README 中的故障排除部分。 ## 参考文献 1. Fuentes, E. (2025). *Stop AI Agent Hallucinations: 4 Essential Techniques.* Dev.to / AWS. [dev.to/aws/stop-ai-agent-hallucinations-4-essential-techniques-2i94](https://dev.to/aws/stop-ai-agent-hallucinations-4-essential-techniques-2i94) 2. Fuentes, E. (2025). *5 Techniques to Stop AI Agent Hallucinations in Production.* Dev.to / AWS. [dev.to/aws/5-techniques-to-stop-ai-agent-hallucinations-in-production-oik](https://dev.to/aws/5-techniques-to-stop-ai-agent-hallucinations-in-production-oik) 3. Edge et al. (2024). *From Local to Global: A Graph RAG Approach to Query-Focused Summarization.* Microsoft Research. [arXiv:2404.16130](https://arxiv.org/abs/2404.16130) 4. AWS. *Reducing hallucinations in large language models with custom intervention using Amazon Bedrock Agents.* [AWS Blog](https://aws.amazon.com/blogs/machine-learning/reducing-hallucinations-in-large-language-models/) ## 安全 如果您在本项目中发现潜在的安全问题,请通过[漏洞报告页面](https://aws.amazon.com/security/vulnerability-reporting/)通知 AWS/Amazon Security。请**不要**创建公开的 GitHub Issue。 ## 许可证 本库基于 MIT-0 许可证授权。有关详细信息,请参阅 [LICENSE](LICENSE) 文件。
标签:AgentCore, AI代理, AI可靠性, AI工程, AI纠错, Amazon Bedrock, AutoGen, AWS, CrewAI, Cypher查询, DPI, Graph-RAG, LangGraph, LLM, LLM框架, Neo4j, Python, RAG, Strands Agents, Unmanaged PE, 人工智能, 代理引导, 可信赖AI, 向量检索, 图遍历, 多代理验证, 多智能体, 大模型, 大模型安全, 无后门, 生产部署, 用户模式Hook绕过, 神经符号防护, 语义工具选择, 逆向工具, 防止幻觉