-
Notifications
You must be signed in to change notification settings - Fork 106
feat(backend): add authentication support to OpenAIHTTPBackend #491
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
didlawowo
wants to merge
3
commits into
vllm-project:main
Choose a base branch
from
didlawowo:fix/add-auth-headers-to-backend-validation
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.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,9 @@ def __init__( | |
| self, | ||
| target: str, | ||
| model: str = "", | ||
| api_key: str | None = None, | ||
| bearer_token: str | None = None, | ||
| headers: dict[str, str] | None = None, | ||
| api_routes: dict[str, str] | None = None, | ||
| response_handlers: dict[str, Any] | None = None, | ||
| timeout: float = 60.0, | ||
|
|
@@ -65,6 +68,9 @@ def __init__( | |
|
|
||
| :param target: Base URL of the OpenAI-compatible server | ||
| :param model: Model identifier for generation requests | ||
| :param api_key: API key for authentication (used as Bearer token) | ||
| :param bearer_token: Bearer token for authentication (alternative to api_key) | ||
| :param headers: Additional headers to include in all requests | ||
| :param api_routes: Custom API endpoint routes mapping | ||
| :param response_handlers: Custom response handlers for different request types | ||
| :param timeout: Request timeout in seconds | ||
|
|
@@ -79,6 +85,29 @@ def __init__( | |
| self.target = target.rstrip("/").removesuffix("/v1") | ||
| self.model = model | ||
|
|
||
| # Build default headers with authentication | ||
| from guidellm.settings import settings | ||
|
|
||
| self._default_headers: dict[str, str] = {} | ||
|
|
||
| # Merge headers from settings first (lowest priority) | ||
| if settings.openai.headers: | ||
| self._default_headers.update(settings.openai.headers) | ||
|
|
||
| # Add explicit headers parameter (medium priority) | ||
| if headers: | ||
| self._default_headers.update(headers) | ||
|
|
||
| # Resolve API key (highest priority): explicit param > settings | ||
| resolved_api_key = api_key or settings.openai.api_key | ||
| resolved_bearer_token = bearer_token or settings.openai.bearer_token | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you actually remove all references to bearer_token. I don't think we need both. |
||
|
|
||
| # Set Authorization header if we have credentials | ||
| if resolved_api_key: | ||
| self._default_headers["Authorization"] = f"Bearer {resolved_api_key}" | ||
| elif resolved_bearer_token: | ||
| self._default_headers["Authorization"] = f"Bearer {resolved_bearer_token}" | ||
|
|
||
| # Store configuration | ||
| self.api_routes = api_routes or { | ||
| "health": "health", | ||
|
|
@@ -184,7 +213,7 @@ async def available_models(self) -> list[str]: | |
| raise RuntimeError("Backend not started up for process.") | ||
|
|
||
| target = f"{self.target}/{self.api_routes['models']}" | ||
| response = await self._async_client.get(target) | ||
| response = await self._async_client.get(target, headers=self._default_headers) | ||
| response.raise_for_status() | ||
|
|
||
| return [item["id"] for item in response.json()["data"]] | ||
|
|
@@ -245,13 +274,19 @@ async def resolve( # type: ignore[override] | |
| request.request_type, handler_overrides=self.response_handlers | ||
| ) | ||
|
|
||
| # Merge default headers with request-specific headers | ||
| merged_headers = { | ||
| **self._default_headers, | ||
| **(request.arguments.headers or {}), | ||
| } | ||
|
|
||
| if not request.arguments.stream: | ||
| request_info.timings.request_start = time.time() | ||
| response = await self._async_client.request( | ||
| request.arguments.method or "POST", | ||
| request_url, | ||
| params=request.arguments.params, | ||
| headers=request.arguments.headers, | ||
| headers=merged_headers, | ||
| json=request_json, | ||
| data=request_data, | ||
| files=request_files, | ||
|
|
@@ -269,7 +304,7 @@ async def resolve( # type: ignore[override] | |
| request.arguments.method or "POST", | ||
| request_url, | ||
| params=request.arguments.params, | ||
| headers=request.arguments.headers, | ||
| headers=merged_headers, | ||
| json=request_json, | ||
| data=request_data, | ||
| files=request_files, | ||
|
|
@@ -331,4 +366,9 @@ def _resolve_validate_kwargs( | |
| if "method" not in validate_kwargs: | ||
| validate_kwargs["method"] = "GET" | ||
|
|
||
| # Include default headers (with auth) in validation request | ||
| if self._default_headers: | ||
| existing_headers = validate_kwargs.get("headers", {}) | ||
| validate_kwargs["headers"] = {**self._default_headers, **existing_headers} | ||
|
|
||
| return validate_kwargs | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't late import. This should be in import section at top.