A class that wraps a given model
| 37 | |
| 38 | @PROVIDER.register_module(force=True) |
| 39 | class OpenAIProvider(LLMProvider, EmbeddingProvider): |
| 40 | """A class that wraps a given model""" |
| 41 | |
| 42 | client: Any = None |
| 43 | llm_model: str = "" |
| 44 | embedding_model: str = "" |
| 45 | |
| 46 | allowed_special: Union[Literal["all"], Set[str]] = set() |
| 47 | disallowed_special: Union[Literal["all"], Set[str], Sequence[str]] = "all" |
| 48 | chunk_size: int = 1000 |
| 49 | embedding_ctx_length: int = 8191 |
| 50 | request_timeout: Optional[Union[float, Tuple[float, float]]] = None |
| 51 | tiktoken_model_name: Optional[str] = None |
| 52 | |
| 53 | """Whether to skip empty strings when embedding or raise an error.""" |
| 54 | skip_empty: bool = False |
| 55 | |
| 56 | |
| 57 | def __init__(self, provider_cfg_path) -> None: |
| 58 | """Initialize a class instance |
| 59 | |
| 60 | Args: |
| 61 | cfg: Config object |
| 62 | |
| 63 | Returns: |
| 64 | None |
| 65 | """ |
| 66 | self.retries = 5 |
| 67 | provider_cfg_path = assemble_project_path(provider_cfg_path) |
| 68 | provider_cfg = load_json(provider_cfg_path) |
| 69 | self.init_provider(provider_cfg) |
| 70 | |
| 71 | |
| 72 | def init_provider(self, provider_cfg ) -> None: |
| 73 | self.provider_cfg = self._parse_config(provider_cfg) |
| 74 | |
| 75 | def _parse_config(self, provider_cfg) -> dict: |
| 76 | """Parse the config object""" |
| 77 | |
| 78 | conf_dict = dict() |
| 79 | |
| 80 | if isinstance(provider_cfg, dict): |
| 81 | conf_dict = provider_cfg |
| 82 | |
| 83 | key_var_name = conf_dict[PROVIDER_SETTING_KEY_VAR] |
| 84 | |
| 85 | if conf_dict[PROVIDER_SETTING_IS_AZURE]: |
| 86 | |
| 87 | key = os.getenv(key_var_name) |
| 88 | endpoint_var_name = conf_dict[PROVIDER_SETTING_BASE_VAR] |
| 89 | endpoint = os.getenv(endpoint_var_name) |
| 90 | |
| 91 | self.client = AzureOpenAI( |
| 92 | api_key = key, |
| 93 | api_version = conf_dict[PROVIDER_SETTING_API_VERSION], |
| 94 | azure_endpoint = endpoint |
| 95 | ) |
| 96 | else: |
nothing calls this directly
no outgoing calls
no test coverage detected