@@ -100,7 +100,7 @@ def any2message(message: str|List[Dict[str,str]]|Dict[str,str], vars: Optional[D
100100
101101
102102
103- def toolnames2funcs (tools ):
103+ def toolnames2funcs (tools , tool_map : dict = None ):
104104 """
105105 Convert a list of tool names to a dictionary of functions.
106106
@@ -113,10 +113,15 @@ def toolnames2funcs(tools):
113113 Raises:
114114 Exception: If a function is not found.
115115 """
116+ if tool_map is None :
117+ tool_map = {}
116118 fmap = {}
117119 for tool in tools :
118120 name = tool ["function" ]["name" ]
119- func = get_func_by_name (name )
121+ if name in tool_map :
122+ func = tool_map [name ]
123+ else :
124+ func = get_func_by_name (name )
120125 if func is None :
121126 raise Exception (f"Function { name } not found" )
122127 fmap [name ] = func
@@ -668,6 +673,7 @@ def query(
668673 llmalias : str ,
669674 messages : List [Dict [str , str ]],
670675 tools : Optional [List [Dict ]] = None ,
676+ tool_map : Optional [Dict [str , Callable ]] = None ,
671677 return_cost : bool = False ,
672678 return_response : bool = False ,
673679 debug = False ,
@@ -685,8 +691,11 @@ def query(
685691 llmalias: the alias/name of the LLM to query
686692 messages: a list of message dictionaries with role and content keys
687693 tools: a list of tool dictionaries, each dictionary describing a tool.
688- See https://docs.litellm.ai/docs/completion/function_call for the format.
689- However, this can be created using the `make_tooling` function.
694+ See https://docs.litellm.ai/docs/completion/function_call for the format. Each entry must contain
695+ the key `python_function` in addition, where the value is the actual python callable to invoke.
696+ This can be created using the `make_tooling` function.
697+ tool_map: a dictionary, mapping the tool names in the "tools" list to actual python callables. If a tool
698+ name is not found in this or this is empy/None, an attempt is made to find the function automatically.
690699 return_cost: whether or not LLM invocation costs should get returned. Gets automatically enabled if
691700 cost logging is enabled.
692701 return_response: whether or not the complete reponse should get returned
@@ -763,8 +772,8 @@ def cleaned_args(args: dict):
763772 completion_kwargs ["tool_choice" ] = "auto"
764773 # Not known/supported by litellm, apparently
765774 # if "parallel_tool_choice" not in completion_kwargs:
766- # completion_kwargs["parallel_tool_choice"] = True
767- fmap = toolnames2funcs (tools )
775+ # completion_kwargs["parallel_tool_choice"] = True
776+ fmap = toolnames2funcs (tools , tool_map = tool_map )
768777 else :
769778 fmap = {}
770779 if via_streaming :
0 commit comments