MCPcopy Create free account
hub / github.com/Open-Bee/DataStudio / MPOpenAIAPI

Class MPOpenAIAPI

datastudio/models/mp_openai_api.py:84–330  ·  view source on GitHub ↗

Multiprocessing OpenAI-compatible API using fork + COW shared memory. Distributes requests across multiple worker processes, each running an independent :class:`OpenAIAPI` with its own async event loop. Designed for workloads where single-process async saturates CPU on image encodi

Source from the content-addressed store, hash-verified

82
83@MODELS.register_module()
84class MPOpenAIAPI(BaseAPI):
85 """Multiprocessing OpenAI-compatible API using fork + COW shared memory.
86
87 Distributes requests across multiple worker processes, each running an
88 independent :class:`OpenAIAPI` with its own async event loop.
89
90 Designed for workloads where single-process async saturates CPU on
91 image encoding / JSON serialization, total request count is large,
92 and payload size makes pickle-based IPC infeasible.
93
94 Memory model:
95 Pre-encoded messages are stored in a module-level global before
96 ``fork()``. Workers inherit the parent's address space via
97 Copy-on-Write and only read shared data, so N workers ≈ 1x memory.
98 """
99
100 def __init__(
101 self,
102 model: str,
103 key: str = None,
104 temperature: float = 0.6,
105 top_p: float = 0.95,
106 top_k: int = 20,
107 min_p: float = 0.0,
108 presence_penalty: float = 1.0,
109 repetition_penalty: float = 1.0,
110 api_base: str = None,
111 port: int = None,
112 retry: int = 10,
113 wait: int = 3,
114 timeout: tuple = (30, 1800),
115 max_tokens: int = 16384,
116 thread_num: int = 8192,
117 return_dict: bool = False,
118 logger=None,
119 max_connections: int = None,
120 num_workers: int = None,
121 worker_concurrency: int = None,
122 enable_thinking: bool = False,
123 **kwargs,
124 ):
125 """Initialize multiprocessing OpenAI API wrapper.
126
127 Args:
128 model: Model name, e.g., gpt-4, Qwen2-VL-72B.
129 key: API key (uses OPENAI_API_KEY env var if not provided).
130 temperature: Generation temperature.
131 top_p: Nucleus sampling threshold (0~1).
132 top_k: Top-K sampling (0 to disable). vLLM/SGLang extra param.
133 min_p: Minimum probability threshold. vLLM/SGLang extra param.
134 presence_penalty: Penalize tokens already present in the output.
135 repetition_penalty: Penalize repeated tokens. vLLM/SGLang extra param.
136 api_base: API base URL (full URL to chat/completions endpoint).
137 port: Port number for local deployments.
138 retry: Number of retry attempts on API failure.
139 wait: Max wait time between retries (seconds).
140 timeout: Request timeout as (connect_timeout, read_timeout) tuple.
141 max_tokens: Maximum tokens in response.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected