r"""Base class for different model backends. It may be OpenAI API, a local LLM, a stub for unit tests, etc. Args: model_type (Union[ModelType, str]): Model for which a backend is created. model_config_dict (Optional[Dict[str, Any]], optional): A config
| 28 | |
| 29 | |
| 30 | class BaseModelBackend(ABC): |
| 31 | r"""Base class for different model backends. |
| 32 | It may be OpenAI API, a local LLM, a stub for unit tests, etc. |
| 33 | |
| 34 | Args: |
| 35 | model_type (Union[ModelType, str]): Model for which a backend is |
| 36 | created. |
| 37 | model_config_dict (Optional[Dict[str, Any]], optional): A config |
| 38 | dictionary. (default: :obj:`{}`) |
| 39 | api_key (Optional[str], optional): The API key for authenticating |
| 40 | with the model service. (default: :obj:`None`) |
| 41 | url (Optional[str], optional): The url to the model service. |
| 42 | (default: :obj:`None`) |
| 43 | token_counter (Optional[BaseTokenCounter], optional): Token |
| 44 | counter to use for the model. If not provided, |
| 45 | :obj:`OpenAITokenCounter` will be used. (default: :obj:`None`) |
| 46 | """ |
| 47 | |
| 48 | def __init__( |
| 49 | self, |
| 50 | model_type: Union[ModelType, str], |
| 51 | model_config_dict: Optional[Dict[str, Any]] = None, |
| 52 | api_key: Optional[str] = None, |
| 53 | url: Optional[str] = None, |
| 54 | token_counter: Optional[BaseTokenCounter] = None, |
| 55 | ) -> None: |
| 56 | self.model_type: UnifiedModelType = UnifiedModelType(model_type) |
| 57 | if model_config_dict is None: |
| 58 | model_config_dict = {} |
| 59 | self.model_config_dict = model_config_dict |
| 60 | self._api_key = api_key |
| 61 | self._url = url |
| 62 | self._token_counter = token_counter |
| 63 | self.check_model_config() |
| 64 | |
| 65 | @property |
| 66 | @abstractmethod |
| 67 | def token_counter(self) -> BaseTokenCounter: |
| 68 | r"""Initialize the token counter for the model backend. |
| 69 | |
| 70 | Returns: |
| 71 | BaseTokenCounter: The token counter following the model's |
| 72 | tokenization style. |
| 73 | """ |
| 74 | pass |
| 75 | |
| 76 | @abstractmethod |
| 77 | def run( |
| 78 | self, |
| 79 | messages: List[OpenAIMessage], |
| 80 | ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]: |
| 81 | r"""Runs the query to the backend model. |
| 82 | |
| 83 | Args: |
| 84 | messages (List[OpenAIMessage]): Message list with the chat history |
| 85 | in OpenAI API format. |
| 86 | |
| 87 | Returns: |
nothing calls this directly
no outgoing calls
no test coverage detected