1
- """
2
- Copied from: https://github.com/THUDM/ChatGLM3/blob/main/composite_demo/tool_registry.py
3
-
4
- This code is the tool registration part. By registering the tool, the model can call the tool.
5
- This code provides extended functionality to the model, enabling it to call and interact with a variety of utilities
6
- through defined interfaces.
7
- """
8
-
9
1
import copy
10
- import inspect
11
- from pprint import pformat
12
- import traceback
13
- from types import GenericAlias
14
- from typing import get_origin , Annotated
15
2
import json , sys , re
16
3
17
- import binding
18
4
from binding import PATH_BINDS
19
5
20
- _TOOL_HOOKS = {}
21
- _TOOL_DESCRIPTIONS = {}
22
-
23
-
24
- def register_tool (func : callable ):
25
- tool_name = func .__name__
26
- tool_description = inspect .getdoc (func ).strip ()
27
- python_params = inspect .signature (func ).parameters
28
- tool_params = []
29
- for name , param in python_params .items ():
30
- annotation = param .annotation
31
- if annotation is inspect .Parameter .empty :
32
- raise TypeError (f"Parameter `{ name } ` missing type annotation" )
33
- if get_origin (annotation ) != Annotated :
34
- raise TypeError (f"Annotation type for `{ name } ` must be typing.Annotated" )
35
-
36
- typ , (description , required ) = annotation .__origin__ , annotation .__metadata__
37
- typ : str = str (typ ) if isinstance (typ , GenericAlias ) else typ .__name__
38
- if not isinstance (description , str ):
39
- raise TypeError (f"Description for `{ name } ` must be a string" )
40
- if not isinstance (required , bool ):
41
- raise TypeError (f"Required for `{ name } ` must be a bool" )
42
-
43
- tool_params .append ({
44
- "name" : name ,
45
- "description" : description ,
46
- "type" : typ ,
47
- "required" : required
48
- })
49
- tool_def = {
50
- "name" : tool_name ,
51
- "description" : tool_description ,
52
- "params" : tool_params
53
- }
54
-
55
- # print("[registered tool] " + pformat(tool_def))
56
- _TOOL_HOOKS [tool_name ] = func
57
- _TOOL_DESCRIPTIONS [tool_name ] = tool_def
58
-
59
- return func
60
-
61
-
62
- def dispatch_tool (tool_name : str , tool_params : dict ) -> str :
63
- if tool_name not in _TOOL_HOOKS :
64
- return f"Tool `{ tool_name } ` not found. Please use a provided tool."
65
- tool_call = _TOOL_HOOKS [tool_name ]
66
- try :
67
- ret = tool_call (** tool_params )
68
- except :
69
- ret = traceback .format_exc ()
70
- return str (ret )
71
-
6
+ import tool_definition
7
+ from tool_definition import dispatch_tool
72
8
73
9
def get_tools () -> dict :
74
- return copy .deepcopy (_TOOL_DESCRIPTIONS )
75
10
11
+ def convert (tool : dict ):
12
+ r = copy .deepcopy (tool )
76
13
77
- # Tool Definitions
78
-
79
- @register_tool
80
- def get_weather (
81
- city_name : Annotated [str , 'The name of the city to be queried' , True ],
82
- ) -> str :
83
- """
84
- Get the current weather for `city_name`
85
- """
86
-
87
- if not isinstance (city_name , str ):
88
- raise TypeError ("City name must be a string" )
89
-
90
- key_selection = {
91
- "current_condition" : ["temp_C" , "FeelsLikeC" , "humidity" , "weatherDesc" , "observation_time" ],
92
- }
93
- import requests
94
- try :
95
- resp = requests .get (f"https://wttr.in/{ city_name } ?format=j1" )
96
- resp .raise_for_status ()
97
- resp = resp .json ()
98
- ret = {k : {_v : resp [k ][0 ][_v ] for _v in v } for k , v in key_selection .items ()}
99
- except :
100
- import traceback
101
- ret = "Error encountered while fetching weather data!\n " + traceback .format_exc ()
14
+ r ['params' ] = r ['parameters' ]
15
+ del r ['parameters' ]
16
+ return r
102
17
103
- return str ( ret )
18
+ return [ convert ( t ) for t in tool_definition . _TOOL_DESCRIPTIONS ]
104
19
105
20
def build_sys_prompt ():
106
21
return "Answer the following questions as best as you can. You have access to the following tools: \n \n " + \
@@ -124,9 +39,9 @@ def tool_call(*args, **kwargs) -> dict:
124
39
code = extract_code ('\n ' .join (call_args_text ))
125
40
args = eval (code , {'tool_call' : tool_call }, {})
126
41
observation = dispatch_tool (tool_name , args )
127
- return observation
128
- except :
129
- print ("error occurs" )
42
+ return observation . text
43
+ except Exception as e :
44
+ print (f "error occurs: { e } " )
130
45
return "failed to call the function"
131
46
132
47
class ToolChatLLM (ChatLLM ):
0 commit comments