2
2
3
3
import json
4
4
import re
5
- from typing import Any , Callable , Dict , Optional , Tuple , Type , Union
5
+ from typing import Any , Callable , Optional , Type , Union
6
6
7
7
from langchain .agents import tool
8
8
from langchain_core .tools import BaseTool
14
14
_TOOLS_REGISTRY = {}
15
15
16
16
17
- def _new_parse_input (
18
- self ,
19
- tool_input : Union [str , Dict ],
20
- ) -> Union [str , Dict [str , Any ]]:
21
- """Convert tool input to pydantic model."""
22
- input_args = self .args_schema
23
- if isinstance (tool_input , str ):
24
- if input_args is not None :
25
- key_ = next (iter (input_args .__fields__ .keys ()))
26
- input_args .validate ({key_ : tool_input })
27
- return tool_input
28
- else :
29
- if input_args is not None :
30
- result = input_args .parse_obj (tool_input )
31
- return result .dict ()
32
-
33
-
34
- def _new_to_args_and_kwargs (self , tool_input : Union [str , Dict ]) -> Tuple [Tuple , Dict ]:
35
- # For backwards compatibility, if run_input is a string,
36
- # pass as a positional argument.
37
- if isinstance (tool_input , str ):
38
- return (tool_input ,), {}
39
- else :
40
- # for tool defined with `*args` parameters
41
- # the args_schema has a field named `args`
42
- # it should be expanded to actual *args
43
- # e.g.: test_tools
44
- # .test_named_tool_decorator_return_direct
45
- # .search_api
46
- if "args" in tool_input :
47
- args = tool_input ["args" ]
48
- if args is None :
49
- tool_input .pop ("args" )
50
- return (), tool_input
51
- elif isinstance (args , tuple ):
52
- tool_input .pop ("args" )
53
- return args , tool_input
54
- return (), tool_input
55
-
56
-
57
- BaseTool ._parse_input = _new_parse_input
58
- BaseTool ._to_args_and_kwargs = _new_to_args_and_kwargs
59
- ###############################
60
-
61
-
62
17
def regist_tool (
63
18
* args : Any ,
64
19
title : str = "" ,
@@ -140,4 +95,4 @@ def __str__(self) -> str:
140
95
elif callable (self .format ):
141
96
return self .format (self )
142
97
else :
143
- return str (self .data )
98
+ return str (self .data )
0 commit comments