forked from NVIDIA/NeMo-Guardrails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
153 lines (120 loc) · 4.78 KB
/
actions.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
# 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 logging
from typing import Dict, Optional
from langchain.llms.base import BaseLLM
from nemoguardrails.actions.actions import action
from nemoguardrails.actions.llm.utils import llm_call
from nemoguardrails.context import llm_call_info_var
from nemoguardrails.llm.params import llm_params
from nemoguardrails.llm.taskmanager import LLMTaskManager
from nemoguardrails.logging.explain import LLMCallInfo
log = logging.getLogger(__name__)
@action()
async def content_safety_check_input(
llms: Dict[str, BaseLLM],
llm_task_manager: LLMTaskManager,
model_name: Optional[str] = None,
context: Optional[dict] = None,
) -> dict:
_MAX_TOKENS = 3
user_input: str = ""
if context is not None:
user_input = context.get("user_message", "")
model_name = model_name or context.get("model", None)
if model_name is None:
error_msg = (
"Model name is required for content safety check, "
"please provide it as an argument in the config.yml. "
"e.g. content safety check input $model=llama_guard"
)
raise ValueError(error_msg)
llm = llms.get(model_name, None)
if llm is None:
error_msg = (
f"Model {model_name} not found in the list of available models for content safety check. "
"Please provide a valid model name."
)
raise ValueError(error_msg)
task = f"content_safety_check_input $model={model_name}"
check_input_prompt = llm_task_manager.render_task_prompt(
task=task,
context={
"user_input": user_input,
},
)
stop = llm_task_manager.get_stop_tokens(task=task)
max_tokens = llm_task_manager.get_max_tokens(task=task)
llm_call_info_var.set(LLMCallInfo(task=task))
max_tokens = max_tokens or _MAX_TOKENS
with llm_params(llm, temperature=1e-20, max_tokens=max_tokens):
result = await llm_call(llm, check_input_prompt, stop=stop)
result = llm_task_manager.parse_task_output(task, output=result)
try:
is_safe, violated_policies = result
# in case the result is single value
except TypeError:
is_safe = result
violated_policies = []
return {"allowed": is_safe, "policy_violations": violated_policies}
@action()
async def content_safety_check_output(
llms: Dict[str, BaseLLM],
llm_task_manager: LLMTaskManager,
model_name: Optional[str] = None,
context: Optional[dict] = None,
) -> dict:
_MAX_TOKENS = 3
user_input: str = ""
bot_response: str = ""
if context is not None:
user_input = context.get("user_message", "")
bot_response = context.get("bot_message", "")
model_name = model_name or context.get("model", None)
if model_name is None:
error_msg = (
"Model name is required for content safety check, "
"please provide it as an argument in the config.yml. "
"e.g. flow content safety (model_name='llama_guard')"
)
raise ValueError(error_msg)
llm = llms.get(model_name, None)
if llm is None:
error_msg = (
f"Model {model_name} not found in the list of available models for content safety check. "
"Please provide a valid model name."
)
raise ValueError(error_msg)
task = f"content_safety_check_output $model={model_name}"
check_output_prompt = llm_task_manager.render_task_prompt(
task=task,
context={
"user_input": user_input,
"bot_response": bot_response,
},
)
stop = llm_task_manager.get_stop_tokens(task=task)
max_tokens = llm_task_manager.get_max_tokens(task=task)
max_tokens = max_tokens or _MAX_TOKENS
llm_call_info_var.set(LLMCallInfo(task=task))
with llm_params(llm, temperature=1e-20, max_tokens=max_tokens):
result = await llm_call(llm, check_output_prompt, stop=stop)
result = llm_task_manager.parse_task_output(task, output=result)
try:
is_safe, violated_policies = result
except TypeError:
is_safe = result
violated_policies = []
return {"allowed": is_safe, "policy_violations": violated_policies}