Async OpenAI-compatible API wrapper. Uses a shared :class:`aiohttp.ClientSession` with connection pooling and ``asyncio.Semaphore`` for concurrency control. Each request handles retries independently with exponential backoff.
| 20 | |
| 21 | @MODELS.register_module() |
| 22 | class OpenAIAPI(BaseAPI): |
| 23 | """Async OpenAI-compatible API wrapper. |
| 24 | |
| 25 | Uses a shared :class:`aiohttp.ClientSession` with connection pooling |
| 26 | and ``asyncio.Semaphore`` for concurrency control. Each request handles |
| 27 | retries independently with exponential backoff. |
| 28 | """ |
| 29 | |
| 30 | def __init__( |
| 31 | self, |
| 32 | model: str, |
| 33 | key: str = None, |
| 34 | temperature: float = 0.6, |
| 35 | top_p: float = 0.95, |
| 36 | top_k: int = 20, |
| 37 | min_p: float = 0.0, |
| 38 | presence_penalty: float = 1.0, |
| 39 | repetition_penalty: float = 1.0, |
| 40 | api_base: str = None, |
| 41 | port: int = None, |
| 42 | retry: int = 10, |
| 43 | wait: int = 3, |
| 44 | timeout: tuple = (30, 1800), |
| 45 | max_tokens: int = 16384, |
| 46 | thread_num: int = 8192, |
| 47 | return_dict: bool = False, |
| 48 | logger=None, |
| 49 | max_connections: int = None, |
| 50 | enable_thinking: bool = False, |
| 51 | **kwargs, |
| 52 | ): |
| 53 | """Initialize OpenAI-compatible API wrapper. |
| 54 | |
| 55 | Args: |
| 56 | model: Model name, e.g., gpt-4, Qwen2-VL-72B. |
| 57 | key: API key (uses OPENAI_API_KEY env var if not provided). |
| 58 | temperature: Generation temperature. |
| 59 | top_p: Nucleus sampling threshold (0~1). |
| 60 | top_k: Top-K sampling (0 to disable). vLLM/SGLang extra param. |
| 61 | min_p: Minimum probability threshold. vLLM/SGLang extra param. |
| 62 | presence_penalty: Penalize tokens already present in the output. |
| 63 | repetition_penalty: Penalize repeated tokens. vLLM/SGLang extra param. |
| 64 | api_base: API base URL (full URL to chat/completions endpoint). |
| 65 | port: Port number for local deployments. |
| 66 | retry: Number of retry attempts on API failure. |
| 67 | wait: Max wait time between retries (seconds). |
| 68 | timeout: Request timeout as (connect_timeout, read_timeout) tuple. |
| 69 | max_tokens: Maximum tokens in response. |
| 70 | thread_num: Maximum number of concurrent requests (semaphore limit). |
| 71 | return_dict: Whether to parse response as dict via load_str_to_dict. |
| 72 | logger: Logger instance. |
| 73 | max_connections: Max TCP connections in the pool. Defaults to |
| 74 | min(thread_num, 16384). |
| 75 | enable_thinking: Whether to enable thinking mode for models that support it |
| 76 | (e.g., Qwen3 on vLLM/SGLang). Default False. |
| 77 | **kwargs: Additional API parameters passed to payload. |
| 78 | """ |
| 79 | super().__init__( |
no outgoing calls