Skip to content

Commit

Permalink
feat: reusing httpx client to improve performance (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik authored Jul 1, 2024
1 parent ce50ce3 commit d17c93c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion aidial_adapter_dial/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
HTTPException,
dial_exception_decorator,
)
from aidial_adapter_dial.utils.http_client import get_http_client
from aidial_adapter_dial.utils.log_config import configure_loggers
from aidial_adapter_dial.utils.reflection import call_with_extra_body
from aidial_adapter_dial.utils.sse_stream import to_openai_sse_stream
Expand Down Expand Up @@ -103,7 +104,7 @@ async def parse(
base_url=upstream_endpoint,
api_key=remote_dial_api_key,
api_version=query_params.get("api-version"),
timeout=DEFAULT_TIMEOUT,
http_client=get_http_client(),
)

attachment_transformer = await AttachmentTransformer.create(
Expand Down
20 changes: 20 additions & 0 deletions aidial_adapter_dial/utils/http_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import functools

import httpx

# connect timeout and total timeout
DEFAULT_TIMEOUT = httpx.Timeout(600, connect=10)

# Borrowed from openai._constants.DEFAULT_CONNECTION_LIMITS
DEFAULT_CONNECTION_LIMITS = httpx.Limits(
max_connections=1000, max_keepalive_connections=100
)


@functools.cache
def get_http_client() -> httpx.AsyncClient:
return httpx.AsyncClient(
timeout=DEFAULT_TIMEOUT,
limits=DEFAULT_CONNECTION_LIMITS,
follow_redirects=True,
)
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ aiohttp = "3.9.5"
openai = "1.32.0" # NOTE: used solely for chat completion response types
pydantic = "^1.10.12"
aidial-sdk = {version = "^0.8.0", extras = ["telemetry"]}
respx = "^0.21.1"

[tool.poetry.group.test.dependencies]
pytest = "7.4.0"
pytest-asyncio = "0.21.1"
python-dotenv = "1.0.0"
respx = "^0.21.1"

[tool.poetry.group.lint.dependencies]
pyright = "1.1.324"
Expand Down

0 comments on commit d17c93c

Please sign in to comment.