Hello World
本指南向您展示如何创建控制问候行为的 “Hello World” Guardrails 配置。在开始之前,请确保您已安装 NeMo Guardrails。
准备工作
此 “Hello World” Guardrails 配置使用 OpenAI gpt-3.5-turbo-instruct
模型。
安装
openai
包
pip install openai
设置
OPENAI_API_KEY
环境变量
export OPENAI_API_KEY=$OPENAI_API_KEY # Replace with your own key
如果您在笔记本中运行此代码,请修补 AsyncIO 循环。
import nest_asyncio
nest_asyncio.apply()
步骤 1:创建新的 Guardrails 配置
每个 Guardrails 配置都必须存储在一个文件夹中。标准文件夹结构如下
.
├── config
│ ├── actions.py
│ ├── config.py
│ ├── config.yml
│ ├── rails.co
│ ├── ...
有关这些文件内容的信息,请参阅配置指南。
为您的配置创建一个文件夹,例如 config
mkdir config
创建一个 config.yml 文件,内容如下
models:
- type: main
engine: openai
model: gpt-3.5-turbo-instruct
config.yml 文件中的 models
键配置 LLM 模型。有关受支持的 LLM 模型的完整列表,请参阅受支持的 LLM 模型。
步骤 2:加载 Guardrails 配置
要从路径加载 Guardrails 配置,您必须在 Python 代码中使用 from_path
方法创建 RailsConfig
实例
from nemoguardrails import RailsConfig
config = RailsConfig.from_path("./config")
步骤 3:使用 Guardrails 配置
通过创建 LLMRails
实例并在 Python 代码中使用 generate_async
方法来使用此空配置
from nemoguardrails import LLMRails
rails = LLMRails(config)
response = rails.generate(messages=[{
"role": "user",
"content": "Hello!"
}])
print(response)
{'role': 'assistant', 'content': "Hello! It's nice to meet you. My name is Assistant. How can I help you today?"}
输入 messages
数组以及响应的格式遵循 OpenAI API 格式。
步骤 4:添加您的第一个 Guardrail
要控制问候响应,请定义用户和 bot 消息,以及连接两者的流程。有关消息和流程的定义,请参阅核心 Colang 概念。
通过创建包含以下内容的 config/rails.co 文件来定义
greeting
用户消息
define user express greeting
"Hello"
"Hi"
"Wassup?"
添加一个问候流程,指示 bot 回复 “Hello World!” 并询问他们的情况,方法是将以下内容添加到 rails.co 文件中
define flow greeting
user express greeting
bot express greeting
bot ask how are you
通过将以下内容添加到 rails.co 文件中来定义响应的消息
define bot express greeting
"Hello World!"
define bot ask how are you
"How are you doing?"
重新加载配置并进行测试
config = RailsConfig.from_path("./config")
rails = LLMRails(config)
response = rails.generate(messages=[{
"role": "user",
"content": "Hello!"
}])
print(response["content"])
Hello World!
How are you doing?
恭喜!您刚刚创建了您的第一个 Guardrails 配置!
其他查询
如果您提出另一个问题,例如 “法国的首都是哪里?”,会发生什么?
response = rails.generate(messages=[{
"role": "user",
"content": "What is the capital of France?"
}])
print(response["content"])
The capital of France is Paris.
对于任何其他不是问候的输入,LLM 会像往常一样生成响应。这是因为我们定义的 rail 只关注如何响应问候。
CLI 聊天
您还可以使用 NeMo Guardrails CLI Chat 命令在交互模式下测试此配置
$ nemoguardrails chat
在没有任何其他参数的情况下,CLI 聊天从当前目录的 config 文件夹中的 config.yml 文件加载配置。
示例会话
$ nemoguardrails chat
Starting the chat (Press Ctrl+C to quit) ...
> Hello there!
Hello World!
How are you doing?
> What is the capital of France?
The capital of france is Paris.
> And how many people live there?
According to the latest estimates, the population of Paris is around 2.2 million people.
服务器和聊天 UI
您还可以使用 NeMo Guardrails 服务器和聊天 UI 测试 Guardrails 配置。
启动服务器
$ nemoguardrails server --config=.
INFO: Started server process [27509]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
聊天 UI 界面现在可以在 http://127.0.0.1:8000
上使用
下一步
下一个指南核心 Colang 概念解释了 Colang 概念消息和流程。