examples
214 строк · 5.6 Кб
1{
2"cells": [
3{
4"cell_type": "code",
5"execution_count": 1,
6"metadata": {
7"id": "OWG3JPB0jUJo"
8},
9"outputs": [],
10"source": [
11"!pip install -qU \\\n",
12" nemoguardrails==0.4.0 \\\n",
13" openai==0.27.8"
14]
15},
16{
17"cell_type": "markdown",
18"metadata": {
19"id": "RDrsyThjjUJp"
20},
21"source": [
22"We need to set our OpenAI API key:"
23]
24},
25{
26"cell_type": "code",
27"execution_count": 2,
28"metadata": {
29"id": "oHQOTiTQjUJp"
30},
31"outputs": [],
32"source": [
33"import os\n",
34"\n",
35"os.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\") or \"sk-...\""
36]
37},
38{
39"cell_type": "markdown",
40"metadata": {
41"id": "Jcm8FnQ_jUJp"
42},
43"source": [
44"We can run Guardrails from terminal using the command:\n",
45"\n",
46"```\n",
47"nemoguardrails chat --config=config/\n",
48"```\n",
49"\n",
50"Where the `config` directory must contain our `config.yml` and a Colang file (like `topics.co`).\n",
51"\n",
52"Alternatively we can load them from file using `RailsConfig.from_path(\"./config\")` or from string variables in our code like so:"
53]
54},
55{
56"cell_type": "code",
57"execution_count": 3,
58"metadata": {
59"id": "DhZ9qy90jUJq"
60},
61"outputs": [],
62"source": [
63"yaml_content = \"\"\"\n",
64"models:\n",
65"- type: main\n",
66" engine: openai\n",
67" model: text-davinci-003\n",
68"\"\"\"\n",
69"colang_content = \"\"\"\n",
70"# define niceties\n",
71"define user express greeting\n",
72" \"hello\"\n",
73" \"hi\"\n",
74" \"what's up?\"\n",
75"\n",
76"define flow greeting\n",
77" user express greeting\n",
78" bot express greeting\n",
79" bot ask how are you\n",
80"\n",
81"# define limits\n",
82"define user ask politics\n",
83" \"what are your political beliefs?\"\n",
84" \"thoughts on the president?\"\n",
85" \"left wing\"\n",
86" \"right wing\"\n",
87"\n",
88"define bot answer politics\n",
89" \"I'm a shopping assistant, I don't like to talk of politics.\"\n",
90"\n",
91"define flow politics\n",
92" user ask politics\n",
93" bot answer politics\n",
94" bot offer help\n",
95"\"\"\""
96]
97},
98{
99"cell_type": "code",
100"execution_count": 4,
101"metadata": {
102"id": "PrKzVn4wjUJq"
103},
104"outputs": [],
105"source": [
106"from nemoguardrails import LLMRails, RailsConfig\n",
107"\n",
108"# initialize rails config\n",
109"config = RailsConfig.from_content(\n",
110" \tyaml_content=yaml_content,\n",
111" colang_content=colang_content\n",
112")\n",
113"# create rails\n",
114"rails = LLMRails(config)"
115]
116},
117{
118"cell_type": "markdown",
119"metadata": {
120"id": "JdI13hg4jUJq"
121},
122"source": [
123"From here, we begin asking questions and interacting with our Guardrails protected LLM."
124]
125},
126{
127"cell_type": "code",
128"execution_count": 5,
129"metadata": {
130"colab": {
131"base_uri": "https://localhost:8080/"
132},
133"id": "htzi4K72jUJq",
134"outputId": "8615c158-9d06-4423-b454-d7489ddb9e30"
135},
136"outputs": [
137{
138"output_type": "stream",
139"name": "stdout",
140"text": [
141"Hi there! How can I help you?\n",
142"How are you doing today?\n"
143]
144}
145],
146"source": [
147"res = await rails.generate_async(prompt=\"Hey there!\")\n",
148"print(res)"
149]
150},
151{
152"cell_type": "markdown",
153"metadata": {
154"id": "P2qv5mEujUJq"
155},
156"source": [
157"This is a typical greeting so we have no protective guardrails here. However, we do see the `greeting` flow being activated as the chatbot generates some text from the `bot express greeting` message, and on the next line generates some more text from the `bot ask how are you` message.\n",
158"\n",
159"Let's try asking a more political question:"
160]
161},
162{
163"cell_type": "code",
164"execution_count": 6,
165"metadata": {
166"colab": {
167"base_uri": "https://localhost:8080/"
168},
169"id": "n-e24M6PjUJq",
170"outputId": "e8f5c8b4-4a1a-495f-f6a9-4d86864933c4"
171},
172"outputs": [
173{
174"output_type": "stream",
175"name": "stdout",
176"text": [
177"I'm a shopping assistant, I don't like to talk of politics.\n",
178"However, I can help you with shopping related tasks. Is there anything I can help you with?\n"
179]
180}
181],
182"source": [
183"res = await rails.generate_async(prompt=\"what do you think of the president?\")\n",
184"print(res)"
185]
186},
187{
188"cell_type": "markdown",
189"metadata": {
190"id": "0IizgBc-jUJr"
191},
192"source": [
193"Here we can see that our `politics` rail is activated and our chatbot dodges the question with `bot answer politics` and follows up with `bot offer help`.\n",
194"\n",
195"---"
196]
197}
198],
199"metadata": {
200"language_info": {
201"name": "python"
202},
203"orig_nbformat": 4,
204"colab": {
205"provenance": []
206},
207"kernelspec": {
208"name": "python3",
209"display_name": "Python 3"
210}
211},
212"nbformat": 4,
213"nbformat_minor": 0
214}