mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
* fix(proxy_server.py): fix azure key vault logic to not require client id/secret * feat(cost_calculator.py): support fireworks ai cost tracking * build(docker-compose.yml): add lines for mounting config.yaml to docker compose Closes https://github.com/BerriAI/litellm/issues/5739 * fix(input.md): update docs to clarify litellm supports content as a list of dictionaries Fixes https://github.com/BerriAI/litellm/issues/5755 * fix(input.md): update input.md to include all message values * fix(image_handling.py): follow image url redirects Fixes https://github.com/BerriAI/litellm/issues/5763 * fix(router.py): Fix model key/base leak in error message Fixes https://github.com/BerriAI/litellm/issues/5762 * fix(http_handler.py): fix linting error * fix(azure.py): fix logging to show azure_ad_token being used Fixes https://github.com/BerriAI/litellm/issues/5767 * fix(_redis.py): add redis sentinel support Closes https://github.com/BerriAI/litellm/issues/4381 * feat(_redis.py): add redis sentinel support Closes https://github.com/BerriAI/litellm/issues/4381 * test(test_completion_cost.py): fix test * Databricks Integration: Integrate Databricks SDK as optional mechanism for fetching API base and token, if unspecified (#5746) * LiteLLM Minor Fixes & Improvements (09/16/2024) (#5723) * coverage (#5713) Signed-off-by: dbczumar <corey.zumar@databricks.com> * Move (#5714) Signed-off-by: dbczumar <corey.zumar@databricks.com> * fix(litellm_logging.py): fix logging client re-init (#5710) Fixes https://github.com/BerriAI/litellm/issues/5695 * fix(presidio.py): Fix logging_hook response and add support for additional presidio variables in guardrails config Fixes https://github.com/BerriAI/litellm/issues/5682 * feat(o1_handler.py): fake streaming for openai o1 models Fixes https://github.com/BerriAI/litellm/issues/5694 * docs: deprecated traceloop integration in favor of native otel (#5249) * fix: fix linting errors * fix: fix linting errors * fix(main.py): fix o1 import --------- Signed-off-by: dbczumar <corey.zumar@databricks.com> Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com> Co-authored-by: Nir Gazit <nirga@users.noreply.github.com> * feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view (#5730) * feat(spend_management_endpoints.py): expose `/global/spend/refresh` endpoint for updating material view Supports having `MonthlyGlobalSpend` view be a material view, and exposes an endpoint to refresh it * fix(custom_logger.py): reset calltype * fix: fix linting errors * fix: fix linting error * fix Signed-off-by: dbczumar <corey.zumar@databricks.com> * fix: fix import * Fix Signed-off-by: dbczumar <corey.zumar@databricks.com> * fix Signed-off-by: dbczumar <corey.zumar@databricks.com> * DB test Signed-off-by: dbczumar <corey.zumar@databricks.com> * Coverage Signed-off-by: dbczumar <corey.zumar@databricks.com> * progress Signed-off-by: dbczumar <corey.zumar@databricks.com> * fix Signed-off-by: dbczumar <corey.zumar@databricks.com> * fix Signed-off-by: dbczumar <corey.zumar@databricks.com> * fix Signed-off-by: dbczumar <corey.zumar@databricks.com> * fix test name Signed-off-by: dbczumar <corey.zumar@databricks.com> --------- Signed-off-by: dbczumar <corey.zumar@databricks.com> Co-authored-by: Krish Dholakia <krrishdholakia@gmail.com> Co-authored-by: Nir Gazit <nirga@users.noreply.github.com> * test: fix test * test(test_databricks.py): fix test * fix(databricks/chat.py): handle custom endpoint (e.g. sagemaker) * Apply code scanning fix for clear-text logging of sensitive information Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix(__init__.py): fix known fireworks ai models --------- Signed-off-by: dbczumar <corey.zumar@databricks.com> Co-authored-by: Corey Zumar <39497902+dbczumar@users.noreply.github.com> Co-authored-by: Nir Gazit <nirga@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
470 lines
16 KiB
Python
470 lines
16 KiB
Python
import asyncio
|
|
import os
|
|
import traceback
|
|
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union
|
|
|
|
import httpx
|
|
from httpx import USE_CLIENT_DEFAULT
|
|
|
|
import litellm
|
|
|
|
from .types import httpxSpecialProvider
|
|
|
|
if TYPE_CHECKING:
|
|
from litellm import LlmProviders
|
|
else:
|
|
LlmProviders = Any
|
|
|
|
try:
|
|
from litellm._version import version
|
|
except:
|
|
version = "0.0.0"
|
|
|
|
headers = {
|
|
"User-Agent": f"litellm/{version}",
|
|
}
|
|
|
|
# https://www.python-httpx.org/advanced/timeouts
|
|
_DEFAULT_TIMEOUT = httpx.Timeout(timeout=5.0, connect=5.0)
|
|
|
|
|
|
class AsyncHTTPHandler:
|
|
def __init__(
|
|
self,
|
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
concurrent_limit=1000,
|
|
):
|
|
self.timeout = timeout
|
|
self.client = self.create_client(
|
|
timeout=timeout, concurrent_limit=concurrent_limit
|
|
)
|
|
|
|
def create_client(
|
|
self, timeout: Optional[Union[float, httpx.Timeout]], concurrent_limit: int
|
|
) -> httpx.AsyncClient:
|
|
|
|
# SSL certificates (a.k.a CA bundle) used to verify the identity of requested hosts.
|
|
# /path/to/certificate.pem
|
|
ssl_verify = os.getenv("SSL_VERIFY", litellm.ssl_verify)
|
|
# An SSL certificate used by the requested host to authenticate the client.
|
|
# /path/to/client.pem
|
|
cert = os.getenv("SSL_CERTIFICATE", litellm.ssl_certificate)
|
|
|
|
if timeout is None:
|
|
timeout = _DEFAULT_TIMEOUT
|
|
# Create a client with a connection pool
|
|
|
|
return httpx.AsyncClient(
|
|
timeout=timeout,
|
|
limits=httpx.Limits(
|
|
max_connections=concurrent_limit,
|
|
max_keepalive_connections=concurrent_limit,
|
|
),
|
|
verify=ssl_verify,
|
|
cert=cert,
|
|
headers=headers,
|
|
)
|
|
|
|
async def close(self):
|
|
# Close the client when you're done with it
|
|
await self.client.aclose()
|
|
|
|
async def __aenter__(self):
|
|
return self.client
|
|
|
|
async def __aexit__(self):
|
|
# close the client when exiting
|
|
await self.client.aclose()
|
|
|
|
async def get(
|
|
self,
|
|
url: str,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
follow_redirects: Optional[bool] = None,
|
|
):
|
|
# Set follow_redirects to UseClientDefault if None
|
|
_follow_redirects = (
|
|
follow_redirects if follow_redirects is not None else USE_CLIENT_DEFAULT
|
|
)
|
|
|
|
response = await self.client.get(
|
|
url, params=params, headers=headers, follow_redirects=_follow_redirects # type: ignore
|
|
)
|
|
return response
|
|
|
|
async def post(
|
|
self,
|
|
url: str,
|
|
data: Optional[Union[dict, str]] = None, # type: ignore
|
|
json: Optional[dict] = None,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
stream: bool = False,
|
|
):
|
|
try:
|
|
if timeout is None:
|
|
timeout = self.timeout
|
|
req = self.client.build_request(
|
|
"POST", url, data=data, json=json, params=params, headers=headers, timeout=timeout # type: ignore
|
|
)
|
|
response = await self.client.send(req, stream=stream)
|
|
response.raise_for_status()
|
|
return response
|
|
except (httpx.RemoteProtocolError, httpx.ConnectError):
|
|
# Retry the request with a new session if there is a connection error
|
|
new_client = self.create_client(timeout=timeout, concurrent_limit=1)
|
|
try:
|
|
return await self.single_connection_post_request(
|
|
url=url,
|
|
client=new_client,
|
|
data=data,
|
|
json=json,
|
|
params=params,
|
|
headers=headers,
|
|
stream=stream,
|
|
)
|
|
finally:
|
|
await new_client.aclose()
|
|
except httpx.TimeoutException as e:
|
|
headers = {}
|
|
error_response = getattr(e, "response", None)
|
|
if error_response is not None:
|
|
for key, value in error_response.headers.items():
|
|
headers["response_headers-{}".format(key)] = value
|
|
|
|
raise litellm.Timeout(
|
|
message=f"Connection timed out after {timeout} seconds.",
|
|
model="default-model-name",
|
|
llm_provider="litellm-httpx-handler",
|
|
headers=headers,
|
|
)
|
|
except httpx.HTTPStatusError as e:
|
|
setattr(e, "status_code", e.response.status_code)
|
|
if stream is True:
|
|
setattr(e, "message", await e.response.aread())
|
|
else:
|
|
setattr(e, "message", e.response.text)
|
|
raise e
|
|
except Exception as e:
|
|
raise e
|
|
|
|
async def put(
|
|
self,
|
|
url: str,
|
|
data: Optional[Union[dict, str]] = None, # type: ignore
|
|
json: Optional[dict] = None,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
stream: bool = False,
|
|
):
|
|
try:
|
|
if timeout is None:
|
|
timeout = self.timeout
|
|
req = self.client.build_request(
|
|
"PUT", url, data=data, json=json, params=params, headers=headers, timeout=timeout # type: ignore
|
|
)
|
|
response = await self.client.send(req, stream=stream)
|
|
response.raise_for_status()
|
|
return response
|
|
except (httpx.RemoteProtocolError, httpx.ConnectError):
|
|
# Retry the request with a new session if there is a connection error
|
|
new_client = self.create_client(timeout=timeout, concurrent_limit=1)
|
|
try:
|
|
return await self.single_connection_post_request(
|
|
url=url,
|
|
client=new_client,
|
|
data=data,
|
|
json=json,
|
|
params=params,
|
|
headers=headers,
|
|
stream=stream,
|
|
)
|
|
finally:
|
|
await new_client.aclose()
|
|
except httpx.TimeoutException as e:
|
|
headers = {}
|
|
error_response = getattr(e, "response", None)
|
|
if error_response is not None:
|
|
for key, value in error_response.headers.items():
|
|
headers["response_headers-{}".format(key)] = value
|
|
|
|
raise litellm.Timeout(
|
|
message=f"Connection timed out after {timeout} seconds.",
|
|
model="default-model-name",
|
|
llm_provider="litellm-httpx-handler",
|
|
headers=headers,
|
|
)
|
|
except httpx.HTTPStatusError as e:
|
|
setattr(e, "status_code", e.response.status_code)
|
|
if stream is True:
|
|
setattr(e, "message", await e.response.aread())
|
|
else:
|
|
setattr(e, "message", e.response.text)
|
|
raise e
|
|
except Exception as e:
|
|
raise e
|
|
|
|
async def delete(
|
|
self,
|
|
url: str,
|
|
data: Optional[Union[dict, str]] = None, # type: ignore
|
|
json: Optional[dict] = None,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
stream: bool = False,
|
|
):
|
|
try:
|
|
if timeout is None:
|
|
timeout = self.timeout
|
|
req = self.client.build_request(
|
|
"DELETE", url, data=data, json=json, params=params, headers=headers, timeout=timeout # type: ignore
|
|
)
|
|
response = await self.client.send(req, stream=stream)
|
|
response.raise_for_status()
|
|
return response
|
|
except (httpx.RemoteProtocolError, httpx.ConnectError):
|
|
# Retry the request with a new session if there is a connection error
|
|
new_client = self.create_client(timeout=timeout, concurrent_limit=1)
|
|
try:
|
|
return await self.single_connection_post_request(
|
|
url=url,
|
|
client=new_client,
|
|
data=data,
|
|
json=json,
|
|
params=params,
|
|
headers=headers,
|
|
stream=stream,
|
|
)
|
|
finally:
|
|
await new_client.aclose()
|
|
except httpx.HTTPStatusError as e:
|
|
setattr(e, "status_code", e.response.status_code)
|
|
if stream is True:
|
|
setattr(e, "message", await e.response.aread())
|
|
else:
|
|
setattr(e, "message", e.response.text)
|
|
raise e
|
|
except Exception as e:
|
|
raise e
|
|
|
|
async def single_connection_post_request(
|
|
self,
|
|
url: str,
|
|
client: httpx.AsyncClient,
|
|
data: Optional[Union[dict, str]] = None, # type: ignore
|
|
json: Optional[dict] = None,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
stream: bool = False,
|
|
):
|
|
"""
|
|
Making POST request for a single connection client.
|
|
|
|
Used for retrying connection client errors.
|
|
"""
|
|
req = client.build_request(
|
|
"POST", url, data=data, json=json, params=params, headers=headers # type: ignore
|
|
)
|
|
response = await client.send(req, stream=stream)
|
|
response.raise_for_status()
|
|
return response
|
|
|
|
def __del__(self) -> None:
|
|
try:
|
|
asyncio.get_running_loop().create_task(self.close())
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
class HTTPHandler:
|
|
def __init__(
|
|
self,
|
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
concurrent_limit=1000,
|
|
client: Optional[httpx.Client] = None,
|
|
):
|
|
if timeout is None:
|
|
timeout = _DEFAULT_TIMEOUT
|
|
|
|
# SSL certificates (a.k.a CA bundle) used to verify the identity of requested hosts.
|
|
# /path/to/certificate.pem
|
|
ssl_verify = os.getenv("SSL_VERIFY", litellm.ssl_verify)
|
|
# An SSL certificate used by the requested host to authenticate the client.
|
|
# /path/to/client.pem
|
|
cert = os.getenv("SSL_CERTIFICATE", litellm.ssl_certificate)
|
|
|
|
if client is None:
|
|
# Create a client with a connection pool
|
|
self.client = httpx.Client(
|
|
timeout=timeout,
|
|
limits=httpx.Limits(
|
|
max_connections=concurrent_limit,
|
|
max_keepalive_connections=concurrent_limit,
|
|
),
|
|
verify=ssl_verify,
|
|
cert=cert,
|
|
headers=headers,
|
|
)
|
|
else:
|
|
self.client = client
|
|
|
|
def close(self):
|
|
# Close the client when you're done with it
|
|
self.client.close()
|
|
|
|
def get(
|
|
self,
|
|
url: str,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
follow_redirects: Optional[bool] = None,
|
|
):
|
|
# Set follow_redirects to UseClientDefault if None
|
|
_follow_redirects = (
|
|
follow_redirects if follow_redirects is not None else USE_CLIENT_DEFAULT
|
|
)
|
|
|
|
response = self.client.get(
|
|
url, params=params, headers=headers, follow_redirects=_follow_redirects # type: ignore
|
|
)
|
|
return response
|
|
|
|
def post(
|
|
self,
|
|
url: str,
|
|
data: Optional[Union[dict, str]] = None,
|
|
json: Optional[Union[dict, str]] = None,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
stream: bool = False,
|
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
):
|
|
try:
|
|
|
|
if timeout is not None:
|
|
req = self.client.build_request(
|
|
"POST", url, data=data, json=json, params=params, headers=headers, timeout=timeout # type: ignore
|
|
)
|
|
else:
|
|
req = self.client.build_request(
|
|
"POST", url, data=data, json=json, params=params, headers=headers # type: ignore
|
|
)
|
|
response = self.client.send(req, stream=stream)
|
|
response.raise_for_status()
|
|
return response
|
|
except httpx.TimeoutException:
|
|
raise litellm.Timeout(
|
|
message=f"Connection timed out after {timeout} seconds.",
|
|
model="default-model-name",
|
|
llm_provider="litellm-httpx-handler",
|
|
)
|
|
except httpx.HTTPStatusError as e:
|
|
setattr(e, "status_code", e.response.status_code)
|
|
if stream is True:
|
|
setattr(e, "message", e.response.read())
|
|
else:
|
|
setattr(e, "message", e.response.text)
|
|
raise e
|
|
except Exception as e:
|
|
raise e
|
|
|
|
def put(
|
|
self,
|
|
url: str,
|
|
data: Optional[Union[dict, str]] = None,
|
|
json: Optional[Union[dict, str]] = None,
|
|
params: Optional[dict] = None,
|
|
headers: Optional[dict] = None,
|
|
stream: bool = False,
|
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
):
|
|
try:
|
|
|
|
if timeout is not None:
|
|
req = self.client.build_request(
|
|
"PUT", url, data=data, json=json, params=params, headers=headers, timeout=timeout # type: ignore
|
|
)
|
|
else:
|
|
req = self.client.build_request(
|
|
"PUT", url, data=data, json=json, params=params, headers=headers # type: ignore
|
|
)
|
|
response = self.client.send(req, stream=stream)
|
|
return response
|
|
except httpx.TimeoutException:
|
|
raise litellm.Timeout(
|
|
message=f"Connection timed out after {timeout} seconds.",
|
|
model="default-model-name",
|
|
llm_provider="litellm-httpx-handler",
|
|
)
|
|
except Exception as e:
|
|
raise e
|
|
|
|
def __del__(self) -> None:
|
|
try:
|
|
self.close()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def get_async_httpx_client(
|
|
llm_provider: Union[LlmProviders, httpxSpecialProvider],
|
|
params: Optional[dict] = None,
|
|
) -> AsyncHTTPHandler:
|
|
"""
|
|
Retrieves the async HTTP client from the cache
|
|
If not present, creates a new client
|
|
|
|
Caches the new client and returns it.
|
|
"""
|
|
_params_key_name = ""
|
|
if params is not None:
|
|
for key, value in params.items():
|
|
try:
|
|
_params_key_name += f"{key}_{value}"
|
|
except Exception:
|
|
pass
|
|
|
|
_cache_key_name = "async_httpx_client" + _params_key_name + llm_provider
|
|
if _cache_key_name in litellm.in_memory_llm_clients_cache:
|
|
return litellm.in_memory_llm_clients_cache[_cache_key_name]
|
|
|
|
if params is not None:
|
|
_new_client = AsyncHTTPHandler(**params)
|
|
else:
|
|
_new_client = AsyncHTTPHandler(
|
|
timeout=httpx.Timeout(timeout=600.0, connect=5.0)
|
|
)
|
|
litellm.in_memory_llm_clients_cache[_cache_key_name] = _new_client
|
|
return _new_client
|
|
|
|
|
|
def _get_httpx_client(params: Optional[dict] = None) -> HTTPHandler:
|
|
"""
|
|
Retrieves the HTTP client from the cache
|
|
If not present, creates a new client
|
|
|
|
Caches the new client and returns it.
|
|
"""
|
|
_params_key_name = ""
|
|
if params is not None:
|
|
for key, value in params.items():
|
|
try:
|
|
_params_key_name += f"{key}_{value}"
|
|
except Exception:
|
|
pass
|
|
|
|
_cache_key_name = "httpx_client" + _params_key_name
|
|
if _cache_key_name in litellm.in_memory_llm_clients_cache:
|
|
return litellm.in_memory_llm_clients_cache[_cache_key_name]
|
|
|
|
if params is not None:
|
|
_new_client = HTTPHandler(**params)
|
|
else:
|
|
_new_client = HTTPHandler(timeout=httpx.Timeout(timeout=600.0, connect=5.0))
|
|
|
|
litellm.in_memory_llm_clients_cache[_cache_key_name] = _new_client
|
|
return _new_client
|