检索增强生成

本指南介绍如何在 RAG 场景中应用 guardrails 配置。本指南基于之前的指南,进一步开发了演示 ABC Bot。

前提条件

  1. 安装 openai

pip install openai
  1. 设置 OPENAI_API_KEY 环境变量

export OPENAI_API_KEY=$OPENAI_API_KEY    # Replace with your own key
  1. 如果你在 notebook 中运行,请修补 AsyncIO 循环。

import nest_asyncio

nest_asyncio.apply()

用法

有两种模式可以将 guardrails 配置与 RAG 结合使用

  1. 相关片段:自行执行检索,并将相关片段直接传递给 generate 方法。

  2. 知识库:直接在 guardrails 配置中配置知识库,并让 NeMo Guardrails 管理检索部分。

相关片段

在之前的指南中,“我每年有多少天免费休假”的消息会产生一般性回应

from nemoguardrails import RailsConfig, LLMRails

config = RailsConfig.from_path("./config")
rails = LLMRails(config)

response = rails.generate(messages=[{
    "role": "user",
    "content": "How many vacation days do I have per year?"
}])
print(response["content"])
Full-time employees are eligible for up to two weeks of paid vacation time per year. Part-time employees receive a prorated amount based on their hours worked. Please refer to the employee handbook for more information.

ABC 公司的员工手册包含以下信息

Employees are eligible for the following time off:

* Vacation: 20 days per year, accrued monthly.
* Sick leave: 15 days per year, accrued monthly.
* Personal days: 5 days per year, accrued monthly.
* Paid holidays: New Year's Day, Memorial Day, Independence Day, Thanksgiving Day, Christmas Day.
* Bereavement leave: 3 days paid leave for immediate family members, 1 day for non-immediate family members.

在进行 generate 调用时,您可以将此信息直接传递给 guardrails

response = rails.generate(messages=[{
    "role": "context",
    "content": {
        "relevant_chunks": """
            Employees are eligible for the following time off:
              * Vacation: 20 days per year, accrued monthly.
              * Sick leave: 15 days per year, accrued monthly.
              * Personal days: 5 days per year, accrued monthly.
              * Paid holidays: New Year's Day, Memorial Day, Independence Day, Thanksgiving Day, Christmas Day.
              * Bereavement leave: 3 days paid leave for immediate family members, 1 day for non-immediate family members. """
    }
},{
    "role": "user",
    "content": "How many vacation days do I have per year?"
}])
print(response["content"])
Eligible employees receive 20 days of paid vacation time per year, which accrues monthly. You can find more information about this in the employee handbook.

正如预期的那样,响应包含正确的答案。

知识库

有三种方法可以将知识库直接配置到 guardrails 配置中

  1. 使用 kb 文件夹。

  2. 使用自定义 retrieve_relevant_chunks 操作。

  3. 使用自定义 EmbeddingSearchProvider

对于选项 1,您可以通过在 config 文件夹内创建 kb 文件夹并在其中添加文档,将知识库直接添加到您的 guardrails 配置中。目前,仅支持 Markdown 格式。有关快速示例,请查看 ABC Bot 的完整实现。

选项 2 和 3 代表超出本主题范围的高级用例。

总结

本指南介绍了如何在 RAG 设置的上下文中使用 guardrails 配置。

下一步

要继续了解 NeMo Guardrails,请查看

  1. Guardrails 库.

  2. 配置指南.