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-
91import copy
10- import inspect
11- from pprint import pformat
12- import traceback
13- from types import GenericAlias
14- from typing import get_origin , Annotated
152import json , sys , re
163
17- import binding
184from binding import PATH_BINDS
195
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
728
739def get_tools () -> dict :
74- return copy .deepcopy (_TOOL_DESCRIPTIONS )
7510
11+ def convert (tool : dict ):
12+ r = copy .deepcopy (tool )
7613
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
10217
103- return str ( ret )
18+ return [ convert ( t ) for t in tool_definition . _TOOL_DESCRIPTIONS ]
10419
10520def build_sys_prompt ():
10621 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:
12439 code = extract_code ('\n ' .join (call_args_text ))
12540 args = eval (code , {'tool_call' : tool_call }, {})
12641 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 } " )
13045 return "failed to call the function"
13146
13247class ToolChatLLM (ChatLLM ):
0 commit comments