[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]):"""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,):iffunctionsisnotNone:iflen(functions)!=len(runnables):raiseValueError("The number of functions does not match the number of runnables.")ifnotall(func["name"]inrunnablesforfuncinfunctions):raiseValueError("One or more function names are not found in runnables.")router=(JsonOutputFunctionsParser(args_only=False)|{"key":itemgetter("name"),"input":itemgetter("arguments")}|RouterRunnable(runnables))super().__init__(bound=router,kwargs={},functions=functions)