r"""Add a function or list of functions to the runtime. Args: funcs (FunctionTool or List[FunctionTool]): The function or list of functions to add. threshold (int): The risk threshold for functions. (default: :obj:`2`) Returns
( # type: ignore[override]
self,
funcs: Union[FunctionTool, List[FunctionTool]],
threshold: int = 2,
)
| 104 | ) |
| 105 | |
| 106 | def add( # type: ignore[override] |
| 107 | self, |
| 108 | funcs: Union[FunctionTool, List[FunctionTool]], |
| 109 | threshold: int = 2, |
| 110 | ) -> "LLMGuardRuntime": |
| 111 | r"""Add a function or list of functions to the runtime. |
| 112 | |
| 113 | Args: |
| 114 | funcs (FunctionTool or List[FunctionTool]): The function or |
| 115 | list of functions to add. |
| 116 | threshold (int): The risk threshold for functions. |
| 117 | (default: :obj:`2`) |
| 118 | |
| 119 | Returns: |
| 120 | LLMGuardRuntime: The current runtime. |
| 121 | """ |
| 122 | |
| 123 | if not isinstance(funcs, list): |
| 124 | funcs = [funcs] |
| 125 | |
| 126 | for func in funcs: |
| 127 | inner_func = func.func |
| 128 | |
| 129 | # Create a wrapper that explicitly binds `func` |
| 130 | @wraps(inner_func) |
| 131 | def wrapper( |
| 132 | *args, |
| 133 | func=func, |
| 134 | inner_func=inner_func, |
| 135 | threshold=threshold, |
| 136 | **kwargs, |
| 137 | ): |
| 138 | function_name = func.get_function_name() |
| 139 | if function_name in self.ignore_toolkit.ignored_risks: |
| 140 | reason = self.ignore_toolkit.ignored_risks.pop( |
| 141 | function_name |
| 142 | ) |
| 143 | logger.info( |
| 144 | f"Ignored risk for function {function_name}: {reason}" |
| 145 | ) |
| 146 | return inner_func(*args, **kwargs) |
| 147 | self.agent.init_messages() |
| 148 | resp = self.agent.step( |
| 149 | f""" |
| 150 | Function is: {function_name} |
| 151 | Function description: {func.get_function_description()} |
| 152 | Args: {args} |
| 153 | Kwargs: {kwargs} |
| 154 | """ |
| 155 | ) |
| 156 | tool_call = resp.info.get("external_tool_request", None) |
| 157 | if not tool_call: |
| 158 | logger.error("No tool call found in response.") |
| 159 | return { |
| 160 | "error": "Risk assessment failed. Disabling function." |
| 161 | } |
| 162 | data = tool_call.function.arguments |
| 163 | data = json.loads(data) |
nothing calls this directly
no test coverage detected