Skip to content

Commit 795c69f

Browse files
committed
Add support for passing additional kwargs when instantiating an OpenAI client for Databricks model serving
Signed-off-by: Sid Murching <sid.murching@databricks.com>
1 parent 3c0b170 commit 795c69f

2 files changed

Lines changed: 79 additions & 6 deletions

File tree

databricks/sdk/mixins/open_ai_client.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,54 @@ def auth_flow(self, request: httpx.Request) -> httpx.Request:
3131
http_client = httpx.Client(auth=databricks_token_auth)
3232
return http_client
3333

34-
def get_open_ai_client(self):
34+
def get_open_ai_client(self, **kwargs):
35+
"""Create an OpenAI client configured for Databricks Model Serving.
36+
37+
Returns an OpenAI client instance that is pre-configured to send requests to
38+
Databricks Model Serving endpoints. The client uses Databricks authentication
39+
to query endpoints within the workspace associated with the current WorkspaceClient
40+
instance.
41+
42+
Args:
43+
**kwargs: Additional parameters to pass to the OpenAI client constructor.
44+
Common parameters include:
45+
- timeout (float): Request timeout in seconds (e.g., 30.0)
46+
- max_retries (int): Maximum number of retries for failed requests (e.g., 3)
47+
48+
Any parameter accepted by the OpenAI client constructor can be passed here.
49+
50+
Returns:
51+
OpenAI: An OpenAI client instance configured for Databricks Model Serving.
52+
53+
Raises:
54+
ImportError: If the OpenAI library is not installed.
55+
56+
Example:
57+
>>> client = workspace_client.serving_endpoints.get_open_ai_client()
58+
>>> # With custom timeout and retries
59+
>>> client = workspace_client.serving_endpoints.get_open_ai_client(
60+
... timeout=30.0,
61+
... max_retries=5
62+
... )
63+
"""
3564
try:
3665
from openai import OpenAI
3766
except Exception:
3867
raise ImportError(
3968
"Open AI is not installed. Please install the Databricks SDK with the following command `pip install databricks-sdk[openai]`"
4069
)
4170

42-
return OpenAI(
43-
base_url=self._api._cfg.host + "/serving-endpoints",
44-
api_key="no-token", # Passing in a placeholder to pass validations, this will not be used
45-
http_client=self._get_authorized_http_client(),
46-
)
71+
# Default parameters that are required for Databricks integration
72+
client_params = {
73+
"base_url": self._api._cfg.host + "/serving-endpoints",
74+
"api_key": "no-token", # Passing in a placeholder to pass validations, this will not be used
75+
"http_client": self._get_authorized_http_client(),
76+
}
77+
78+
# Update with any additional parameters passed by the user
79+
client_params.update(kwargs)
80+
81+
return OpenAI(**client_params)
4782

4883
def get_langchain_chat_open_ai_client(self, model):
4984
try:

tests/test_open_ai_mixin.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,44 @@ def test_open_ai_client(monkeypatch):
1919
assert client.api_key == "no-token"
2020

2121

22+
def test_open_ai_client_with_custom_params(monkeypatch):
23+
from databricks.sdk import WorkspaceClient
24+
25+
monkeypatch.setenv("DATABRICKS_HOST", "test_host")
26+
monkeypatch.setenv("DATABRICKS_TOKEN", "test_token")
27+
w = WorkspaceClient(config=Config())
28+
29+
# Test with timeout and max_retries parameters
30+
client = w.serving_endpoints.get_open_ai_client(timeout=30.0, max_retries=3)
31+
32+
assert client.base_url == "https://test_host/serving-endpoints/"
33+
assert client.api_key == "no-token"
34+
assert client.timeout == 30.0
35+
assert client.max_retries == 3
36+
37+
38+
def test_open_ai_client_with_additional_kwargs(monkeypatch):
39+
from databricks.sdk import WorkspaceClient
40+
41+
monkeypatch.setenv("DATABRICKS_HOST", "test_host")
42+
monkeypatch.setenv("DATABRICKS_TOKEN", "test_token")
43+
w = WorkspaceClient(config=Config())
44+
45+
# Test with additional kwargs that OpenAI client might accept
46+
client = w.serving_endpoints.get_open_ai_client(
47+
timeout=60.0,
48+
max_retries=5,
49+
default_headers={"Custom-Header": "test-value"}
50+
)
51+
52+
assert client.base_url == "https://test_host/serving-endpoints/"
53+
assert client.api_key == "no-token"
54+
assert client.timeout == 60.0
55+
assert client.max_retries == 5
56+
assert "Custom-Header" in client.default_headers
57+
assert client.default_headers["Custom-Header"] == "test-value"
58+
59+
2260
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Requires Python > 3.7")
2361
def test_langchain_open_ai_client(monkeypatch):
2462
from databricks.sdk import WorkspaceClient

0 commit comments

Comments
 (0)