OpenAI-style models serving
| 35 | |
| 36 | |
| 37 | class OpenAIServingModels: |
| 38 | """ |
| 39 | OpenAI-style models serving |
| 40 | """ |
| 41 | |
| 42 | def __init__( |
| 43 | self, |
| 44 | model_paths: list[ModelPath], |
| 45 | max_model_len: int, |
| 46 | ips: Union[List[str], str], |
| 47 | ): |
| 48 | self.model_paths = model_paths |
| 49 | self.max_model_len = max_model_len |
| 50 | self.master_ip = ips |
| 51 | self.host_ip = get_host_ip() |
| 52 | if self.master_ip is not None: |
| 53 | if isinstance(self.master_ip, list): |
| 54 | self.master_ip = self.master_ip[0] |
| 55 | else: |
| 56 | self.master_ip = self.master_ip.split(",")[0] |
| 57 | |
| 58 | def _check_master(self): |
| 59 | if self.master_ip is None: |
| 60 | return True |
| 61 | if self.host_ip == self.master_ip: |
| 62 | return True |
| 63 | return False |
| 64 | |
| 65 | def is_supported_model(self, model_name) -> tuple[bool, str]: |
| 66 | """ |
| 67 | Check whether the specified model is supported. |
| 68 | """ |
| 69 | if self.model_paths[0].verification is False: |
| 70 | return True, self.model_name() |
| 71 | if model_name == "default": |
| 72 | return True, self.model_name() |
| 73 | return any(model.name == model_name for model in self.model_paths), model_name |
| 74 | |
| 75 | def model_name(self) -> str: |
| 76 | """ |
| 77 | Returns the current model name. |
| 78 | """ |
| 79 | return self.model_paths[0].name |
| 80 | |
| 81 | async def list_models(self) -> ModelList: |
| 82 | """ |
| 83 | Show available models. |
| 84 | """ |
| 85 | if not self._check_master(): |
| 86 | err_msg = ( |
| 87 | f"Only master node can accept models request, please send request to master node: {self.master_ip}" |
| 88 | ) |
| 89 | api_server_logger.error(err_msg) |
| 90 | return ErrorResponse(error=ErrorInfo(message=err_msg, type=ErrorType.INTERNAL_ERROR)) |
| 91 | model_infos = [ |
| 92 | ModelInfo( |
| 93 | id=model.name, max_model_len=self.max_model_len, root=model.model_path, permission=[ModelPermission()] |
| 94 | ) |
no outgoing calls