Elffuss BraidHub

kikocis/llama-stack public

174d429e7af6 · 1.3 KiBRawPermalink
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the terms described in the LICENSE file in
5# the root directory of this source tree.
6
7from typing import List
8
9from llama_stack.apis.inference import Message
10from llama_stack.apis.safety import * # noqa: F403
11
12from llama_stack.providers.impls.meta_reference.agents.safety import ShieldRunnerMixin
13
14from .builtin import BaseTool
15
16
17class SafeTool(BaseTool, ShieldRunnerMixin):
18 """A tool that makes other tools safety enabled"""
19
20 def __init__(
21 self,
22 tool: BaseTool,
23 safety_api: Safety,
24 input_shields: List[str] = None,
25 output_shields: List[str] = None,
26 ):
27 self._tool = tool
28 ShieldRunnerMixin.__init__(
29 self, safety_api, input_shields=input_shields, output_shields=output_shields
30 )
31
32 def get_name(self) -> str:
33 return self._tool.get_name()
34
35 async def run(self, messages: List[Message]) -> List[Message]:
36 if self.input_shields:
37 await self.run_multiple_shields(messages, self.input_shields)
38 # run the underlying tool
39 res = await self._tool.run(messages)
40 if self.output_shields:
41 await self.run_multiple_shields(messages, self.output_shields)
42
43 return res