# -*- coding: utf-8 -*-"""The chat model base class."""fromabcimportabstractmethodfromtypingimportAsyncGenerator,Anyfrom._model_responseimportChatResponseTOOL_CHOICE_MODES=["auto","none","any","required"]
[docs]classChatModelBase:"""Base class for chat models."""model_name:str"""The model name"""stream:bool"""Is the model output streaming or not"""
[docs]def__init__(self,model_name:str,stream:bool,)->None:"""Initialize the chat model base class. Args: model_name (`str`): The name of the model stream (`bool`): Whether the model output is streaming or not """self.model_name=model_nameself.stream=stream
def_validate_tool_choice(self,tool_choice:str,tools:list[dict]|None,)->None:""" Validate tool_choice parameter. Args: tool_choice (`str`): Tool choice mode or function name tools (`list[dict] | None`): Available tools list Raises: TypeError: If tool_choice is not string ValueError: If tool_choice is invalid """ifnotisinstance(tool_choice,str):raiseTypeError(f"tool_choice must be str, got {type(tool_choice)}",)iftool_choiceinTOOL_CHOICE_MODES:returnavailable_functions=[tool["function"]["name"]fortoolintools]iftool_choicenotinavailable_functions:all_options=TOOL_CHOICE_MODES+available_functionsraiseValueError(f"Invalid tool_choice '{tool_choice}'. "f"Available options: {', '.join(sorted(all_options))}",)