Skip to content

Commit 971c5cf

Browse files
committed
Adds get_httpx_client and get_async_httpx_client methods to simplyfy user experience
1 parent f31cdcc commit 971c5cf

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

ads/aqua/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
from logging import getLogger
88

99
from ads import logger, set_auth
10-
from ads.aqua.client.client import AsyncClient, Client, HttpxOCIAuth
10+
from ads.aqua.client.client import (
11+
AsyncClient,
12+
Client,
13+
HttpxOCIAuth,
14+
get_async_httpx_client,
15+
get_httpx_client,
16+
)
1117
from ads.aqua.common.utils import fetch_service_compartment
1218
from ads.config import OCI_RESOURCE_PRINCIPAL_VERSION
1319

ads/aqua/client/client.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,3 +794,49 @@ async def embeddings(
794794
logger.debug(f"Generating embeddings with input: {input}, payload: {payload}")
795795
payload = {**(payload or {}), "input": input}
796796
return await self._request(payload=payload, headers=headers)
797+
798+
799+
def get_httpx_client(**kwargs: Any) -> httpx.Client:
800+
"""
801+
Creates and returns a synchronous httpx Client configured with OCI authentication.
802+
803+
This function checks if an 'auth' keyword argument is provided. If not, it instantiates
804+
the default OCI authentication (HttpxOCIAuth) and injects it into the client configuration.
805+
Any additional keyword arguments are passed directly to the httpx.Client constructor.
806+
807+
Parameters
808+
----------
809+
**kwargs : Any
810+
Arbitrary keyword arguments for configuring the httpx.Client. An optional 'auth'
811+
argument can be provided to override the default OCI authentication.
812+
813+
Returns
814+
-------
815+
Client
816+
A configured synchronous httpx Client instance.
817+
"""
818+
kwargs["auth"] = kwargs.get("auth") or HttpxOCIAuth()
819+
return httpx.Client(**kwargs)
820+
821+
822+
def get_async_httpx_client(**kwargs: Any) -> httpx.AsyncClient:
823+
"""
824+
Creates and returns an asynchronous httpx AsyncClient configured with OCI authentication.
825+
826+
This function checks if an 'auth' keyword argument is provided. If not, it instantiates
827+
the default OCI authentication (HttpxOCIAuth) and injects it into the client configuration.
828+
Any additional keyword arguments are passed directly to the httpx.AsyncClient constructor.
829+
830+
Parameters
831+
----------
832+
**kwargs : Any
833+
Arbitrary keyword arguments for configuring the httpx.AsyncClient. An optional 'auth'
834+
argument can be provided to override the default OCI authentication.
835+
836+
Returns
837+
-------
838+
AsyncClient
839+
A configured asynchronous httpx AsyncClient instance.
840+
"""
841+
kwargs["auth"] = kwargs.get("auth") or HttpxOCIAuth()
842+
return httpx.AsyncClient(**kwargs)

docs/source/user_guide/large_language_model/aqua_client.rst

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,11 @@ Usage
147147
**Synchronous HTTPX Client**
148148

149149
.. code-block:: python3
150-
151-
import httpx
152150
import ads
153-
from ads.aqua import HttpxOCIAuth
154151
155152
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
156-
client = httpx.Client(auth=HttpxOCIAuth())
153+
154+
client = ads.aqua.get_httpx_client()
157155
158156
response = client.post(
159157
url="https://<MD_OCID>/predict",
@@ -170,9 +168,8 @@ Usage
170168

171169
.. code-block:: python3
172170
173-
import httpx
174171
import ads
175-
from ads.aqua import HttpxOCIAuth
176172
177173
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
178-
async_client = httpx.AsyncClient(auth=HttpxOCIAuth())
174+
175+
async_client = client = ads.aqua.get_async_httpx_client()

0 commit comments

Comments
 (0)