MCPcopy
hub / github.com/Forethought-Technologies/AutoChain / Tool

Class Tool

autochain/tools/base.py:15–110  ·  view source on GitHub ↗

Interface AutoChain tools must implement.

Source from the content-addressed store, hash-verified

13
14
15class Tool(ABC, BaseModel):
16 """Interface AutoChain tools must implement."""
17
18 name: Optional[str] = None
19 """The unique name of the tool that clearly communicates its purpose.
20 If not provided, it will be named after the func name.
21 The more descriptive it is, the easier it would be for model to call the right tool
22 """
23
24 description: str
25 """Used to tell the model how/when/why to use the tool.
26 You can provide few-shot examples as a part of the description.
27 """
28
29 arg_description: Optional[Dict[str, Any]] = None
30 """Dictionary of arg name and description when using OpenAIFunctionsAgent to provide
31 additional argument information"""
32
33 args_schema: Optional[Type[BaseModel]] = None
34 """Pydantic model class to validate and parse the tool's input arguments."""
35
36 func: Union[Callable[..., str], None] = None
37
38 @root_validator()
39 def validate_environment(cls, values: Dict) -> Dict:
40 """Validate that api key and python package exists in environment."""
41 func = values.get("func")
42 if func and not values.get("name"):
43 values["name"] = values["func"].__name__
44
45 # check if all args from arg_description exist in func args
46 if values.get("arg_description") and func:
47 inspection = inspect.getfullargspec(func)
48 override_args = set(values["arg_description"].keys())
49 args = set(inspection.args)
50 override_without_args = override_args - args
51 if len(override_without_args) > 0:
52 raise ValueError(
53 f"Provide arg description for not existed args: {override_without_args}"
54 )
55
56 return values
57
58 def _parse_input(
59 self,
60 tool_input: Union[str, Dict],
61 ) -> Union[str, Dict[str, Any]]:
62 """Convert tool input to pydantic model."""
63 input_args = self.args_schema
64 if isinstance(tool_input, str):
65 if input_args is not None:
66 key_ = next(iter(input_args.__fields__.keys()))
67 input_args.validate({key_: tool_input})
68 return tool_input
69 else:
70 if input_args is not None:
71 result = input_args.parse_obj(tool_input)
72 return {k: v for k, v in result.dict().items() if k in tool_input}

Calls

no outgoing calls

Tested by 4

test_run_toolFunction · 0.72
test_tool_name_overrideFunction · 0.72
test_arg_descriptionFunction · 0.72