Skip to content

Commit 8acd35d

Browse files
authored
Merge pull request #107 from chatchat-space/fix/_new_to_args_and_kwargs
Fix/ new to args and kwargs
2 parents 52e40c1 + b5a1605 commit 8acd35d

File tree

3 files changed

+1538
-1386
lines changed

3 files changed

+1538
-1386
lines changed

chatchat-server/chatchat/server/agent/tools_factory/tools_registry.py

+2-47
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import json
44
import re
5-
from typing import Any, Callable, Dict, Optional, Tuple, Type, Union
5+
from typing import Any, Callable, Optional, Type, Union
66

77
from langchain.agents import tool
88
from langchain_core.tools import BaseTool
@@ -14,51 +14,6 @@
1414
_TOOLS_REGISTRY = {}
1515

1616

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-
6217
def regist_tool(
6318
*args: Any,
6419
title: str = "",
@@ -140,4 +95,4 @@ def __str__(self) -> str:
14095
elif callable(self.format):
14196
return self.format(self)
14297
else:
143-
return str(self.data)
98+
return str(self.data)

0 commit comments

Comments
 (0)