-
Notifications
You must be signed in to change notification settings - Fork 150
Reuse OpenAI client + tqdm-safe logging for vLLM/OpenAI-compatible servers #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VedantMadane
wants to merge
8
commits into
lmarena:main
Choose a base branch
from
VedantMadane:fix/vllm-openai-client-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−26
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9932b91
Reuse OpenAI client for local servers (vLLM) and avoid tqdm corruption
VedantMadane 463b577
docs: add docstrings to satisfy 80% coverage threshold
VedantMadane 3f3f496
fix: add test cache isolation and use consistent client caching
VedantMadane 591d5dd
fix: address CodeRabbit review feedback
VedantMadane e554bf6
docs: add docstrings to register_api and register_engine decorators
VedantMadane b53d353
docs: add module docstring to openai_client.py
VedantMadane 146f322
Update utils/completion.py
VedantMadane fdff334
Address CodeRabbit review: reset thread-local cache in tests, fix doc…
VedantMadane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
|
|
||
| class TestOpenAIClientCache(unittest.TestCase): | ||
| def test_openai_client_is_cached_per_thread_and_endpoint(self): | ||
| # Avoid importing the full completion stack (pulls optional deps). | ||
| # Instead, mock an `openai` module and test the small helper directly. | ||
| from utils import openai_client | ||
|
|
||
| fake_openai = mock.Mock() | ||
| fake_openai.OpenAI.side_effect = [object(), object(), object()] | ||
|
|
||
| with mock.patch.dict("sys.modules", {"openai": fake_openai}): | ||
| c1 = openai_client.get_openai_client( | ||
| {"api_base": "http://localhost:8000/v1", "api_key": "k", "timeout": 123} | ||
| ) | ||
| c2 = openai_client.get_openai_client( | ||
| {"api_base": "http://localhost:8000/v1", "api_key": "k", "timeout": 123} | ||
| ) | ||
| self.assertIs(c1, c2) | ||
| self.assertEqual(fake_openai.OpenAI.call_count, 1) | ||
|
|
||
| # Different timeout should create a new client | ||
| c3 = openai_client.get_openai_client( | ||
| {"api_base": "http://localhost:8000/v1", "api_key": "k", "timeout": 456} | ||
| ) | ||
| self.assertIsNot(c1, c3) | ||
| self.assertEqual(fake_openai.OpenAI.call_count, 2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
| from typing import Optional | ||
|
|
||
| _thread_local = threading.local() | ||
|
|
||
|
|
||
| def get_openai_client(api_dict: Optional[dict] = None): | ||
| """Return a thread-local cached OpenAI client. | ||
|
|
||
| Creating a new OpenAI client per request is expensive (new HTTP client, | ||
| TLS/session setup, etc.) and can significantly slow down local inference | ||
| backends like vLLM that can otherwise serve quickly. | ||
|
|
||
| `api_dict` supports: | ||
| - api_base: str | ||
| - api_key: str | ||
| - timeout: float (seconds) | ||
| """ | ||
| import openai | ||
|
|
||
| api_base = None | ||
| api_key = None | ||
| timeout = None | ||
| if api_dict: | ||
| api_base = api_dict.get("api_base") | ||
| api_key = api_dict.get("api_key") | ||
| timeout = api_dict.get("timeout") | ||
|
|
||
| cache = getattr(_thread_local, "openai_clients", None) | ||
| if cache is None: | ||
| cache = {} | ||
| _thread_local.openai_clients = cache | ||
|
|
||
| key = (api_base, api_key, timeout) | ||
| client = cache.get(key) | ||
| if client is None: | ||
| kwargs = {} | ||
| if api_base: | ||
| kwargs["base_url"] = api_base | ||
| if api_key: | ||
| kwargs["api_key"] = api_key | ||
| if timeout is not None: | ||
| kwargs["timeout"] = timeout | ||
| client = openai.OpenAI(**kwargs) if kwargs else openai.OpenAI() | ||
| cache[key] = client | ||
| return client | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.