@@ -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 :
0 commit comments