-
Notifications
You must be signed in to change notification settings - Fork 12
Feat/litellm-proxy-support #124
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
Closed
Closed
Changes from all commits
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 |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ tmp | |
| .qodo | ||
|
|
||
| traefik/ | ||
| redis_data/ | ||
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,132 @@ | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| Shared OpenAI/Azure/LiteLLM client for vcon-server. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| When LITELLM_PROXY_URL and LITELLM_MASTER_KEY are set in opts, | ||||||||||||||||||||||||||
| returns an OpenAI client configured to use the LiteLLM proxy. Otherwise uses | ||||||||||||||||||||||||||
| direct OpenAI or Azure OpenAI credentials from opts. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| All links and storage that call OpenAI should use get_openai_client(opts) so | ||||||||||||||||||||||||||
| LLM provider and proxy can be switched in one place. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| from openai import OpenAI, AzureOpenAI, AsyncOpenAI, AsyncAzureOpenAI | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| from lib.logging_utils import init_logger | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| logger = init_logger(__name__) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Default Azure API version when not specified | ||||||||||||||||||||||||||
| DEFAULT_AZURE_OPENAI_API_VERSION = "2024-10-21" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def get_openai_client(opts=None): | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| Return an OpenAI-compatible client (OpenAI or AzureOpenAI). | ||||||||||||||||||||||||||
| Same client is used for chat and embeddings; LiteLLM proxy supports both. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| opts: dict of options. All values are read from opts only. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Supported keys in opts: | ||||||||||||||||||||||||||
| - LITELLM_PROXY_URL, LITELLM_MASTER_KEY -> use LiteLLM proxy (chat + embeddings) | ||||||||||||||||||||||||||
| - AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY, AZURE_OPENAI_API_VERSION -> Azure | ||||||||||||||||||||||||||
| - OPENAI_API_KEY or openai_api_key or api_key -> OpenAI | ||||||||||||||||||||||||||
| - organization / organization_key, project / project_key (optional) | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| opts = opts or {} | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| litellm_url = (opts.get("LITELLM_PROXY_URL") or "").strip().rstrip("/") | ||||||||||||||||||||||||||
| litellm_key = (opts.get("LITELLM_MASTER_KEY") or "").strip() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if litellm_url and litellm_key: | ||||||||||||||||||||||||||
| logger.info("Using LiteLLM proxy at %s", litellm_url) | ||||||||||||||||||||||||||
| organization = opts.get("organization") or opts.get("organization_key") | ||||||||||||||||||||||||||
| project = opts.get("project") or opts.get("project_key") | ||||||||||||||||||||||||||
| return OpenAI( | ||||||||||||||||||||||||||
| api_key=litellm_key, | ||||||||||||||||||||||||||
| base_url=litellm_url, | ||||||||||||||||||||||||||
| organization=organization if organization else None, | ||||||||||||||||||||||||||
| project=project if project else None, | ||||||||||||||||||||||||||
| timeout=120.0, | ||||||||||||||||||||||||||
| max_retries=0, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| azure_endpoint = (opts.get("AZURE_OPENAI_ENDPOINT") or "").strip() | ||||||||||||||||||||||||||
| azure_api_key = (opts.get("AZURE_OPENAI_API_KEY") or "").strip() | ||||||||||||||||||||||||||
| azure_api_version = opts.get("AZURE_OPENAI_API_VERSION") or DEFAULT_AZURE_OPENAI_API_VERSION | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if azure_endpoint and azure_api_key: | ||||||||||||||||||||||||||
| logger.info("Using Azure OpenAI client at endpoint: %s", azure_endpoint) | ||||||||||||||||||||||||||
| return AzureOpenAI( | ||||||||||||||||||||||||||
| api_key=azure_api_key, | ||||||||||||||||||||||||||
| azure_endpoint=azure_endpoint, | ||||||||||||||||||||||||||
| api_version=azure_api_version, | ||||||||||||||||||||||||||
| timeout=120.0, | ||||||||||||||||||||||||||
| max_retries=0, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| openai_api_key = ( | ||||||||||||||||||||||||||
| opts.get("OPENAI_API_KEY") | ||||||||||||||||||||||||||
| or opts.get("openai_api_key") | ||||||||||||||||||||||||||
| or opts.get("api_key") | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| if openai_api_key: | ||||||||||||||||||||||||||
| logger.info("Using public OpenAI client") | ||||||||||||||||||||||||||
| organization = opts.get("organization") or opts.get("organization_key") | ||||||||||||||||||||||||||
| project = opts.get("project") or opts.get("project_key") | ||||||||||||||||||||||||||
| return OpenAI( | ||||||||||||||||||||||||||
| api_key=openai_api_key, | ||||||||||||||||||||||||||
| organization=organization if organization else None, | ||||||||||||||||||||||||||
| project=project if project else None, | ||||||||||||||||||||||||||
| timeout=120.0, | ||||||||||||||||||||||||||
| max_retries=0, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||
| "Set LITELLM_PROXY_URL + LITELLM_MASTER_KEY, or " | ||||||||||||||||||||||||||
| "AZURE_OPENAI_ENDPOINT + AZURE_OPENAI_API_KEY, or OPENAI_API_KEY (or api_key)" | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def get_async_openai_client(opts=None): | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| Return an async OpenAI-compatible client. Same opts semantics as get_openai_client. | ||||||||||||||||||||||||||
| LiteLLM proxy is used for both chat and embeddings when configured. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| opts = opts or {} | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| litellm_url = (opts.get("LITELLM_PROXY_URL") or "").strip().rstrip("/") | ||||||||||||||||||||||||||
| litellm_key = (opts.get("LITELLM_MASTER_KEY") or "").strip() | ||||||||||||||||||||||||||
| if litellm_url and litellm_key: | ||||||||||||||||||||||||||
| logger.info("Using LiteLLM proxy at %s (async)", litellm_url) | ||||||||||||||||||||||||||
| return AsyncOpenAI( | ||||||||||||||||||||||||||
| api_key=litellm_key, | ||||||||||||||||||||||||||
| base_url=litellm_url + "/v1", | ||||||||||||||||||||||||||
|
Comment on lines
+99
to
+102
|
||||||||||||||||||||||||||
| logger.info("Using LiteLLM proxy at %s (async)", litellm_url) | |
| return AsyncOpenAI( | |
| api_key=litellm_key, | |
| base_url=litellm_url + "/v1", | |
| # Normalize base URL: append "/v1" only if it's not already present | |
| litellm_base_url = ( | |
| litellm_url if litellm_url.endswith("/v1") else litellm_url + "/v1" | |
| ) | |
| logger.info("Using LiteLLM proxy at %s (async)", litellm_url) | |
| return AsyncOpenAI( | |
| api_key=litellm_key, | |
| base_url=litellm_base_url, |
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
Oops, something went wrong.
Oops, something went wrong.
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.
LiteLLM proxy base_url handling is inconsistent: sync client uses base_url=litellm_url (no "/v1"), but OpenAI clients typically expect the base URL to include the version prefix. This will likely send requests to the wrong paths unless callers include "/v1" themselves. Consider normalizing the proxy URL (append "/v1" exactly once) so both sync and async clients behave the same and configuration is unambiguous.