Skip to content

Commit

Permalink
[ENH] use httpx over requests (#2336)
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jun 18, 2024
1 parent 545fdaf commit 8be43ae
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 301 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ repos:
]
additional_dependencies:
[
"types-requests",
"pydantic",
"overrides",
"hypothesis",
Expand Down
4 changes: 2 additions & 2 deletions bin/windows_upgrade_sqlite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import requests
import httpx
import zipfile
import io
import os
Expand All @@ -10,7 +10,7 @@

if __name__ == "__main__":
# Download and extract the DLL
r = requests.get(DLL_URL)
r = httpx.get(DLL_URL)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall(".")
# Print current Python path
Expand Down
45 changes: 4 additions & 41 deletions chromadb/api/async_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from uuid import UUID
import urllib.parse
import orjson as json
from typing import Any, Optional, TypeVar, cast, Tuple, Sequence, Dict
from typing import Any, Optional, cast, Tuple, Sequence, Dict
import logging
import httpx
from overrides import override
from chromadb import errors
from chromadb.api import AsyncServerAPI
from chromadb.api.base_http_client import BaseHTTPClient
from chromadb.config import DEFAULT_DATABASE, DEFAULT_TENANT, System, Settings
Expand Down Expand Up @@ -43,15 +42,6 @@
)


# requests removes None values from the built query string, but httpx includes it as an empty value
T = TypeVar("T", bound=Dict[Any, Any])


def clean_params(params: T) -> T:
"""Remove None values from kwargs."""
return {k: v for k, v in params.items() if v is not None} # type: ignore


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -130,8 +120,8 @@ async def _make_request(
escaped_path = urllib.parse.quote(path, safe="/", encoding=None, errors=None)
url = self._api_url + escaped_path

response = await self._get_client().request(method, url, **kwargs)
await raise_chroma_error(response)
response = await self._get_client().request(method, url, **cast(Any, kwargs))
BaseHTTPClient._raise_chroma_error(response)
return json.loads(response.text)

@trace_method("AsyncFastAPI.heartbeat", OpenTelemetryGranularity.OPERATION)
Expand Down Expand Up @@ -202,7 +192,7 @@ async def list_collections(
resp_json = await self._make_request(
"get",
"/collections",
params=clean_params(
params=BaseHTTPClient._clean_params(
{
"tenant": tenant,
"database": database,
Expand Down Expand Up @@ -605,30 +595,3 @@ async def get_max_batch_size(self) -> int:
resp_json = await self._make_request("get", "/pre-flight-checks")
self._max_batch_size = cast(int, resp_json["max_batch_size"])
return self._max_batch_size


async def raise_chroma_error(resp: httpx.Response) -> Any:
"""Raises an error if the response is not ok, using a ChromaError if possible."""
try:
resp.raise_for_status()
return
except httpx.HTTPStatusError:
pass

chroma_error = None
try:
body = json.loads(resp.text)
if "error" in body:
if body["error"] in errors.error_types:
chroma_error = errors.error_types[body["error"]](body["message"])

except BaseException:
pass

if chroma_error:
raise chroma_error

try:
resp.raise_for_status()
except httpx.HTTPStatusError:
raise (Exception(resp.text))
40 changes: 39 additions & 1 deletion chromadb/api/base_http_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Optional
from typing import Any, Dict, Optional, TypeVar
from urllib.parse import quote, urlparse, urlunparse
import logging
import orjson as json
import httpx

import chromadb.errors as errors
from chromadb.config import Settings

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -57,3 +60,38 @@ def resolve_url(
)

return full_url

# requests removes None values from the built query string, but httpx includes it as an empty value
T = TypeVar("T", bound=Dict[Any, Any])

@staticmethod
def _clean_params(params: T) -> T:
"""Remove None values from provided dict."""
return {k: v for k, v in params.items() if v is not None} # type: ignore

@staticmethod
def _raise_chroma_error(resp: httpx.Response) -> None:
"""Raises an error if the response is not ok, using a ChromaError if possible."""
try:
resp.raise_for_status()
return
except httpx.HTTPStatusError:
pass

chroma_error = None
try:
body = json.loads(resp.text)
if "error" in body:
if body["error"] in errors.error_types:
chroma_error = errors.error_types[body["error"]](body["message"])

except BaseException:
pass

if chroma_error:
raise chroma_error

try:
resp.raise_for_status()
except httpx.HTTPStatusError:
raise (Exception(resp.text))
6 changes: 3 additions & 3 deletions chromadb/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from uuid import UUID

from overrides import override
import requests
import httpx
from chromadb.api import AdminAPI, ClientAPI, ServerAPI
from chromadb.api.shared_system_client import SharedSystemClient
from chromadb.api.types import (
Expand Down Expand Up @@ -349,7 +349,7 @@ def set_database(self, database: str) -> None:
def _validate_tenant_database(self, tenant: str, database: str) -> None:
try:
self._admin_client.get_tenant(name=tenant)
except requests.exceptions.ConnectionError:
except httpx.ConnectError:
raise ValueError(
"Could not connect to a Chroma server. Are you sure it is running?"
)
Expand All @@ -363,7 +363,7 @@ def _validate_tenant_database(self, tenant: str, database: str) -> None:

try:
self._admin_client.get_database(name=database, tenant=tenant)
except requests.exceptions.ConnectionError:
except httpx.ConnectError:
raise ValueError(
"Could not connect to a Chroma server. Are you sure it is running?"
)
Expand Down
Loading

0 comments on commit 8be43ae

Please sign in to comment.