forked from NVIDIA/NeMo-Guardrails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_llama_guard.py
198 lines (167 loc) · 6 KB
/
test_llama_guard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from nemoguardrails import LLMRails, RailsConfig
from nemoguardrails.actions.actions import ActionResult
from tests.utils import FakeLLM, TestChat
COLANG_CONFIG = """
define user express greeting
"hi"
define bot refuse to respond
"I'm sorry, I can't respond to that."
"""
YAML_CONFIG = """
models:
- type: main
engine: openai
model: gpt-3.5-turbo-instruct
- type: llama_guard
engine: vllm_openai
parameters:
openai_api_base: "http://localhost:5000/v1"
model_name: "meta-llama/LlamaGuard-7b"
rails:
input:
flows:
- llama guard check input
output:
flows:
- llama guard check output
prompts:
- task: llama_guard_check_input
content: |
<s>[INST] Sample content. Only the entry needs to exist for this test. [/INST]
- task: llama_guard_check_output
content: |
<s>[INST] Sample content. Only the entry needs to exist for this test. [/INST]
"""
def test_llama_guard_check_all_safe():
"""
Test the chat flow when both llama_guard_check_input and llama_guard_check_output actions return "safe"
"""
config = RailsConfig.from_content(
colang_content=COLANG_CONFIG, yaml_content=YAML_CONFIG
)
chat = TestChat(
config,
llm_completions=[
"Mock generated user intent", # mock response for the generate_user_intent action
"Mock generated next step", # mock response for the generate_next_step action
" Hi there! How are you doing?", # mock response for the generate_bot_message action
],
)
llama_guard_llm = FakeLLM(
responses=[
"safe", # llama_guard_check_input
"safe", # llama_guard_check_output
]
)
chat.app.register_action_param("llama_guard_llm", llama_guard_llm)
chat >> "Hi"
chat << "Hi there! How are you doing?"
def test_llama_guard_check_input_unsafe():
"""
Test the chat flow when the llama_guard_check_input action returns "unsafe"
"""
config = RailsConfig.from_content(
colang_content=COLANG_CONFIG, yaml_content=YAML_CONFIG
)
chat = TestChat(
config,
llm_completions=[
# Since input is unsafe, the main llm doesn't need to perform any of
# generate_user_intent, generate_next_step, or generate_bot_message
# Dev note: iff the input was safe, this empty llm_completions list would result in a test failure.
],
)
llama_guard_llm = FakeLLM(
responses=[
"unsafe", # llama_guard_check_input
]
)
chat.app.register_action_param("llama_guard_llm", llama_guard_llm)
chat >> "Unsafe input"
chat << "I'm sorry, I can't respond to that."
def test_llama_guard_check_input_error():
"""
Test the chat flow when the llama_guard_check_input action raises an error
"""
config = RailsConfig.from_content(
colang_content=COLANG_CONFIG, yaml_content=YAML_CONFIG
)
chat = TestChat(
config,
llm_completions=[
# Since input is unsafe, the main llm doesn't need to perform any of
# generate_user_intent, generate_next_step, or generate_bot_message
# Dev note: iff the input was safe, this empty llm_completions list would result in a test failure.
],
)
llama_guard_llm = FakeLLM(
responses=[
"error", # llama_guard_check_input
]
)
chat.app.register_action_param("llama_guard_llm", llama_guard_llm)
chat >> "Unsafe input"
chat << "I'm sorry, I can't respond to that."
def test_llama_guard_check_output_unsafe():
"""
Test the chat flow when the llama_guard_check_input action raises an error
"""
config = RailsConfig.from_content(
colang_content=COLANG_CONFIG, yaml_content=YAML_CONFIG
)
chat = TestChat(
config,
llm_completions=[
"Mock generated user intent", # mock response for the generate_user_intent action
"Mock generated next step", # mock response for the generate_next_step action
" Hi there! How are you doing?", # mock response for the generate_bot_message action
],
)
llama_guard_llm = FakeLLM(
responses=[
"safe", # llama_guard_check_input
"unsafe", # llama_guard_check_output
]
)
chat.app.register_action_param("llama_guard_llm", llama_guard_llm)
chat >> "Unsafe input"
chat << "I'm sorry, I can't respond to that."
def test_llama_guard_check_output_error():
"""
Test the chat flow when the llama_guard_check_input action raises an error
"""
config = RailsConfig.from_content(
colang_content=COLANG_CONFIG, yaml_content=YAML_CONFIG
)
chat = TestChat(
config,
llm_completions=[
"Mock generated user intent", # mock response for the generate_user_intent action
"Mock generated next step", # mock response for the generate_next_step action
" Hi there! How are you doing?", # mock response for the generate_bot_message action
],
)
llama_guard_llm = FakeLLM(
responses=[
"safe", # llama_guard_check_input
"error", # llama_guard_check_output
]
)
chat.app.register_action_param("llama_guard_llm", llama_guard_llm)
chat >> "Unsafe input"
chat << "I'm sorry, I can't respond to that."