检索增强生成#
本指南展示了如何在 RAG 场景中应用 guardrails 配置。本指南基于上一指南构建,进一步开发了演示 ABC Bot。
先决条件#
安装
openai
包
pip install openai
设置
OPENAI_API_KEY
环境变量
export OPENAI_API_KEY=$OPENAI_API_KEY # Replace with your own key
如果您在 notebook 内部运行此程序,请修补 AsyncIO 循环。
import nest_asyncio
nest_asyncio.apply()
用法#
您可以通过两种模式将 guardrails 配置与 RAG 结合使用
相关块:自行执行检索,并将相关块直接传递给
generate
方法。知识库:直接在 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 配置中配置知识库
使用 kb 文件夹。
使用自定义
retrieve_relevant_chunks
action。使用自定义
EmbeddingSearchProvider
。
对于选项 1,您可以通过在 config 文件夹内创建一个 kb 文件夹并在其中添加文档,将知识库直接添加到您的 guardrails 配置中。目前,仅支持 Markdown 格式。有关快速示例,请查看ABC Bot的完整实现。
选项 2 和 3 代表超出本主题范围的高级用例。
总结#
本指南介绍了如何在 RAG 设置的上下文中使用 guardrails 配置。
下一步#
要继续了解 NeMo Guardrails,请查看