[docs]classOpenAIFunction(TypedDict):"""A function description for ChatOpenAI."""name:str"""The name of the function."""description:str"""The description of the function."""parameters:dict"""The parameters to the function."""
[docs]classOpenAIFunctionsRouter(RunnableBindingBase[BaseMessage,Any]):# type: ignore[no-redef]"""A runnable that routes to the selected function."""functions:Optional[list[OpenAIFunction]]def__init__(self,runnables:Mapping[str,Union[Runnable[dict,Any],Callable[[dict],Any],],],functions:Optional[list[OpenAIFunction]]=None,):"""Initialize the OpenAIFunctionsRouter. Args: runnables: A mapping of function names to runnables. functions: Optional list of functions to check against the runnables. """iffunctionsisnotNone:iflen(functions)!=len(runnables):msg="The number of functions does not match the number of runnables."raiseValueError(msg)ifnotall(func["name"]inrunnablesforfuncinfunctions):msg="One or more function names are not found in runnables."raiseValueError(msg)router=(JsonOutputFunctionsParser(args_only=False)|{"key":itemgetter("name"),"input":itemgetter("arguments")}|RouterRunnable(runnables))super().__init__(bound=router,kwargs={},functions=functions)