Skip to content

Commit

Permalink
[ML-48691] Adding http_request to serving to call /external-function
Browse files Browse the repository at this point in the history
Signed-off-by: Sunish Sheth <sunishsheth2009@gmail.com>
  • Loading branch information
sunishsheth2009 committed Jan 6, 2025
1 parent 6d6923e commit e733990
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 5 deletions.
68 changes: 63 additions & 5 deletions databricks/sdk/mixins/open_ai_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from databricks.sdk.service.serving import ServingEndpointsAPI
import json as js
from typing import Dict, Optional

from databricks.sdk.service.serving import (
ExternalFunctionRequestMethod,
ExternalFunctionResponse,
ServingEndpointsAPI,
)


class ServingEndpointsExt(ServingEndpointsAPI):
Expand Down Expand Up @@ -34,8 +41,9 @@ def get_open_ai_client(self):

return OpenAI(
base_url=self._api._cfg.host + "/serving-endpoints",
api_key="no-token", # Passing in a placeholder to pass validations, this will not be used
http_client=self._get_authorized_http_client())
api_key="no-token", # Passing in a placeholder to pass validations, this will not be used
http_client=self._get_authorized_http_client(),
)

def get_langchain_chat_open_ai_client(self, model):
try:
Expand All @@ -48,5 +56,55 @@ def get_langchain_chat_open_ai_client(self, model):
return ChatOpenAI(
model=model,
openai_api_base=self._api._cfg.host + "/serving-endpoints",
api_key="no-token", # Passing in a placeholder to pass validations, this will not be used
http_client=self._get_authorized_http_client())
api_key="no-token", # Passing in a placeholder to pass validations, this will not be used
http_client=self._get_authorized_http_client(),
)

def http_request(
self,
conn: str,
method: ExternalFunctionRequestMethod,
path: str,
*,
headers: Optional[Dict[str, str]] = None,
json: Optional[Dict[str, str]] = None,
params: Optional[Dict[str, str]] = None
) -> ExternalFunctionResponse:
"""Make external services call using the credentials stored in UC Connection.
:param conn: str
The connection name to use. This is required to identify the external connection.
:param method: :class:`ExternalFunctionRequestMethod`
The HTTP method to use (e.g., 'GET', 'POST'). This is required.
:param path: str
The relative path for the API endpoint. This is required.
:param headers: Dict[str,str] (optional)
Additional headers for the request. If not provided, only auth headers from connections would be
passed.
:param json: Dict[str,str] (optional)
JSON payload for the request.
:param params: Dict[str,str] (optional)
Query parameters for the request.
:returns: :class:`ExternalFunctionResponse`
"""
body = {}
if conn is not None:
body["conn"] = conn
if headers is not None:
body["headers"] = js.dumps(headers)
if json is not None:
body["json"] = js.dumps(json)
if method is not None:
body["method"] = method.value
if params is not None:
body["params"] = js.dumps(params)
if path is not None:
body["path"] = path
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
}

res = self._api.do("POST", "/external-function", body=body, headers=headers)
return ExternalFunctionResponse.from_dict(res)
132 changes: 132 additions & 0 deletions databricks/sdk/service/serving.py

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

0 comments on commit e733990

Please sign in to comment.