3
3
import json
4
4
from anthropic .types import ToolUseBlock
5
5
from openai .types .chat import ChatCompletionMessageToolCall
6
+ from groq .types .chat import ChatCompletionMessageToolCall as GroqChatCompletionMessageToolCall
6
7
from ..models .RunLocalTools import AnthropicToolResponse , OpenAIToolResponse
7
8
9
+ SupportedTools = Union [
10
+ ChatCompletionMessageToolCall ,
11
+ GroqChatCompletionMessageToolCall ,
12
+ ToolUseBlock ]
13
+
8
14
9
15
class LocalTools ():
10
16
"""Local Runner"""
11
17
def __init__ (self ):
12
18
self .local_tools = {}
13
-
19
+
14
20
def get_registered_tools (self ) -> List [str ]:
15
21
"""Get List of registered tools"""
16
22
return self .local_tools .keys ()
17
-
23
+
18
24
def register_local_tool (self , local_tool ):
19
25
"""Register a new local tool runner"""
20
26
def decorator (func ):
@@ -39,7 +45,7 @@ def _run_local_tools(self, local_tool, *args, **kwargs) -> str:
39
45
f"Make sure that you have a function that can handle this tool and decorate it with @register_local_tool(\" { local_tool } \" )."
40
46
)
41
47
42
- def run_tools (self , tool : Union [ ChatCompletionMessageToolCall , ToolUseBlock ] ) -> Union [AnthropicToolResponse , OpenAIToolResponse ]:
48
+ def run_tools (self , tool : SupportedTools ) -> Union [AnthropicToolResponse , OpenAIToolResponse ]:
43
49
"""_summary_
44
50
45
51
Args:
@@ -53,7 +59,7 @@ def run_tools(self, tool: Union[ChatCompletionMessageToolCall, ToolUseBlock]) ->
53
59
tool_input = tool .input if isinstance (tool .input , dict ) else {}
54
60
content = self ._run_local_tools (local_tool = tool .name , ** tool_input )
55
61
return AnthropicToolResponse (type = "tool_result" , content = content , tool_use_id = tool .id )
56
- elif isinstance (tool , ChatCompletionMessageToolCall ):
62
+ elif isinstance (tool , ChatCompletionMessageToolCall ) or isinstance ( tool , GroqChatCompletionMessageToolCall ) :
57
63
content = self ._run_local_tools (tool .function .name , ** json .loads (tool .function .arguments ))
58
64
return OpenAIToolResponse (tool_call_id = tool .id , name = tool .function .name , content = content , role = "tool" )
59
65
else :
0 commit comments