Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Fixing http_request open_ai_client mixin #870

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions databricks/sdk/mixins/open_ai_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import json as js
from typing import Dict, Optional
from dataclasses import asdict, dataclass
from typing import Any, Dict, Optional

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


class ServingEndpointsExt(ServingEndpointsAPI):

@dataclass
class ExternalFunctionResponseOverride(ExternalFunctionResponse):
text: Dict[str, Any] = None
"""The content of the response"""

@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "ExternalFunctionResponse":
"""Deserializes the ExternalFunctionResponse from a dictionary."""
return cls(status_code=200, text=d)

def to_dict(self) -> Dict[str, Any]:
"""Serializes the object back into a dictionary."""
result = asdict(self) # Use dataclasses.asdict to serialize fields
# Ensure the text field is serialized correctly
if self.text is not None and not isinstance(self.text, str):
result["text"] = js.dumps(self.text) # Serialize text as JSON if it's a dict
return result

# Using the HTTP Client to pass in the databricks authorization
# This method will be called on every invocation, so when using with model serving will always get the refreshed token
def _get_authorized_http_client(self):
Expand Down Expand Up @@ -82,10 +102,15 @@ def http_request(self,
:returns: :class:`ExternalFunctionResponse`
"""

return super.http_request(connection_name=conn,
method=method,
path=path,
headers=js.dumps(headers),
json=js.dumps(json),
params=js.dumps(params),
)
databricks.sdk.service.serving.ExternalFunctionResponse = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, this will change ALL calls to the original http_request. We must avoid these types of side effects. Even if we revert it after the call, it will cause issues in multi threaded environments.

ServingEndpointsExt.ExternalFunctionResponseOverride)

response = super().http_request(connection_name=conn,
method=method,
path=path,
headers=js.dumps(headers) if headers is not None else None,
json=js.dumps(json) if json is not None else None,
params=js.dumps(params) if params is not None else None)

# Convert the overridden response back to the original response type
return ExternalFunctionResponse.from_dict(response.to_dict())
Loading