fix(router.py): fix setting httpx mounts

This commit is contained in:
Krrish Dholakia 2024-06-26 17:22:04 -07:00
parent 151d19960e
commit 98daedaf60
4 changed files with 93 additions and 26 deletions

View file

@ -87,6 +87,7 @@ from litellm.utils import (
ModelResponse,
_is_region_eu,
calculate_max_parallel_requests,
create_proxy_transport_and_mounts,
get_utc_datetime,
)
@ -3316,34 +3317,32 @@ class Router:
import httpx
# Check if the HTTP_PROXY and HTTPS_PROXY environment variables are set and use them accordingly.
http_proxy = os.getenv("HTTP_PROXY", None)
https_proxy = os.getenv("HTTPS_PROXY", None)
no_proxy = os.getenv("NO_PROXY", None)
# http_proxy = os.getenv("HTTP_PROXY", None)
# https_proxy = os.getenv("HTTPS_PROXY", None)
# no_proxy = os.getenv("NO_PROXY", None)
# Create the proxies dictionary only if the environment variables are set.
sync_proxy_mounts = None
async_proxy_mounts = None
if http_proxy is not None and https_proxy is not None:
sync_proxy_mounts = {
"http://": httpx.HTTPTransport(proxy=httpx.Proxy(url=http_proxy)),
"https://": httpx.HTTPTransport(proxy=httpx.Proxy(url=https_proxy)),
}
async_proxy_mounts = {
"http://": httpx.AsyncHTTPTransport(
proxy=httpx.Proxy(url=http_proxy)
),
"https://": httpx.AsyncHTTPTransport(
proxy=httpx.Proxy(url=https_proxy)
),
}
sync_proxy_mounts, async_proxy_mounts = create_proxy_transport_and_mounts()
# if http_proxy is not None and https_proxy is not None:
# sync_proxy_mounts = {
# "http://": httpx.HTTPTransport(proxy=httpx.Proxy(url=http_proxy)),
# "https://": httpx.HTTPTransport(proxy=httpx.Proxy(url=https_proxy)),
# }
# async_proxy_mounts = {
# "http://": httpx.AsyncHTTPTransport(
# proxy=httpx.Proxy(url=http_proxy)
# ),
# "https://": httpx.AsyncHTTPTransport(
# proxy=httpx.Proxy(url=https_proxy)
# ),
# }
# assume no_proxy is a list of comma separated urls
if no_proxy is not None and isinstance(no_proxy, str):
no_proxy_urls = no_proxy.split(",")
# # assume no_proxy is a list of comma separated urls
# if no_proxy is not None and isinstance(no_proxy, str):
# no_proxy_urls = no_proxy.split(",")
for url in no_proxy_urls: # set no-proxy support for specific urls
sync_proxy_mounts[url] = None # type: ignore
async_proxy_mounts[url] = None # type: ignore
# for url in no_proxy_urls: # set no-proxy support for specific urls
# sync_proxy_mounts[url] = None # type: ignore
# async_proxy_mounts[url] = None # type: ignore
organization = litellm_params.get("organization", None)
if isinstance(organization, str) and organization.startswith("os.environ/"):