检索增强生成#
本指南演示如何在 RAG 场景中应用防护栏配置。本指南以上一篇指南为基础,进一步开发演示用的 ABC 机器人。
先决条件#
安装
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()
用法#
您可以结合 RAG 使用防护栏配置,有两种模式:
相关块 (Relevant Chunks):自行执行检索,并将相关块直接传递给
generate
方法。知识库 (Knowledge Base):直接在防护栏配置中配置知识库,并让 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
时,您可以将此信息直接传递给防护栏
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.
正如所料,回复包含了正确答案。
知识库#
您可以通过三种方式直接在防护栏配置中配置知识库
使用 kb 文件夹。
使用自定义的
retrieve_relevant_chunks
动作。使用自定义的
EmbeddingSearchProvider
。
对于选项 1,您可以通过在 config 文件夹中创建一个 kb 文件夹并将文档添加到其中,从而将知识库直接添加到您的防护栏配置中。目前仅支持 Markdown 格式。有关快速示例,请查看 ABC 机器人的完整实现。
选项 2 和 3 表示超出本主题范围的高级用例。
总结#
本指南介绍了如何在 RAG 设置的上下文中应用防护栏配置。
下一步#
要继续学习 NeMo Guardrails,请查看