Repository navigation
fix(databricks): backport the ai_decide AI Function route to rc/1.106.0 (#45722) #45921
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
base: rc/1.106.0
Are you sure you want to change the base?
Changes from all commits
7f68ae2
d09a829
e9670fd
574dedf
0aa5861
dc8f289
0951a08
bbcbebe
afd6f45
de1fef7
7417436
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,20 +4,71 @@ | |
| from types import MappingProxyType | ||
| from typing import Final | ||
|
|
||
| from pydantic import TypeAdapter, ValidationError | ||
|
|
||
| from litellm.llms.base_llm.decisions.transformation import BaseDecisionsConfig | ||
| from litellm.types.decisions import DecisionsIRRequest, UnsupportedDecisionsRequest | ||
|
|
||
| DATABRICKS_AI_DECIDE_MODEL: Final = "ai_decide" | ||
| _AI_DECIDE_PATH: Final = "/api/2.0/ai-functions/ai-decide" | ||
| _SERVING_ENDPOINT_NAME: Final = re.compile(r"[A-Za-z0-9_-][A-Za-z0-9._-]*") | ||
| _MAPPING_ADAPTER: Final[TypeAdapter[Mapping[str, object]]] = TypeAdapter(Mapping[str, object]) | ||
|
|
||
|
|
||
| def is_ai_decide_model(model: str) -> bool: | ||
| return model == DATABRICKS_AI_DECIDE_MODEL | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low ·
|
||
|
|
||
|
|
||
| def validate_serving_endpoint_name(name: str) -> str: | ||
| if _SERVING_ENDPOINT_NAME.fullmatch(name) is None: | ||
| raise ValueError( | ||
| f"Databricks serving endpoint name {name!r} must be the bare endpoint name (letters, digits, '-', '_' " | ||
| "and '.', with no '/', '?', '#', spaces, or a leading '.'), e.g. databricks-openjev-qwen35-4b" | ||
| "Databricks decisions model must be 'ai_decide' (the ai_decide AI Function) or a bare serving endpoint " | ||
| "name (letters, digits, '-', '_' and '.', with no '/', '?', '#', spaces, or a leading '.'), e.g. " | ||
| "databricks-openjev-qwen35-4b" | ||
| ) | ||
| return name | ||
|
|
||
|
|
||
| def validate_databricks_decisions_model(model: str) -> str: | ||
| if is_ai_decide_model(model): | ||
| return model | ||
| return validate_serving_endpoint_name(model) | ||
|
|
||
|
|
||
| def databricks_workspace_host(api_base: str) -> str: | ||
| return api_base.rstrip("/").removesuffix("/serving-endpoints") | ||
|
|
||
|
|
||
| def _mapping(value: object) -> Mapping[str, object] | None: | ||
| try: | ||
| return _MAPPING_ADAPTER.validate_python(value) | ||
| except ValidationError: | ||
| return None | ||
|
|
||
|
|
||
| def _systemone_answer(answer: object) -> object: | ||
| fields: Final = _mapping(answer) | ||
| if fields is None or fields.get("type") != "noul" or "probability" not in fields: | ||
| return answer | ||
| return MappingProxyType( | ||
| {**{key: value for key, value in fields.items() if key != "probability"}, "noul": fields["probability"]} | ||
| ) | ||
|
|
||
|
|
||
| def ai_decide_response(payload: Mapping[str, object]) -> Mapping[str, object]: | ||
| response: Final = _mapping(payload.get("response")) | ||
| answers: Final = None if response is None else _mapping(response.get("answers")) | ||
| if response is None or answers is None: | ||
| return payload | ||
| return MappingProxyType( | ||
| { | ||
| **{key: value for key, value in payload.items() if key != "response"}, | ||
| **response, | ||
| "answers": MappingProxyType({key: _systemone_answer(answer) for key, answer in answers.items()}), | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class DatabricksDecisionsConnection: | ||
| api_base: str | ||
|
|
@@ -35,13 +86,30 @@ def missing_api_base_message(self, custom_llm_provider: str) -> str: | |
| ) | ||
|
|
||
| def canonical_model(self, model: str) -> str: | ||
| return validate_serving_endpoint_name(model) | ||
| return validate_databricks_decisions_model(model) | ||
|
|
||
| def get_complete_url(self, api_base: str, model: str) -> str: | ||
| return f"{api_base.rstrip('/')}/{validate_serving_endpoint_name(model)}/invocations" | ||
| if is_ai_decide_model(model): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low · Accepted API base forms differ between ai_decide and serving endpoints
This predates the PR, and the docs say to use
Review by code-review · Reviewed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This backport keeps the code identical to #45722 on main, so a change here would belong there first. It's Low, so I'm tracking it as a follow-up |
||
| return f"{databricks_workspace_host(api_base)}{_AI_DECIDE_PATH}" | ||
| return f"{api_base.rstrip('/')}/{validate_databricks_decisions_model(model)}/invocations" | ||
|
|
||
| def transform_decisions_request( | ||
| self, | ||
| model: str, | ||
| request: DecisionsIRRequest, | ||
| custom_llm_provider: str, | ||
| ) -> Mapping[str, object] | UnsupportedDecisionsRequest: | ||
| body: Final = super().transform_decisions_request(model, request, custom_llm_provider) | ||
| if isinstance(body, UnsupportedDecisionsRequest) or not is_ai_decide_model(model): | ||
| return body | ||
| return MappingProxyType({key: value for key, value in body.items() if key != "model"}) | ||
|
|
||
| def unwrap_response(self, payload: object) -> object: | ||
| fields: Final = _mapping(payload) | ||
| return payload if fields is None else ai_decide_response(fields) | ||
|
|
||
| def classifier_response(self, body: Mapping[str, object], requested_model: str) -> Mapping[str, object]: | ||
| return MappingProxyType({**body, "model": requested_model}) | ||
| return MappingProxyType({**ai_decide_response(body), "model": requested_model}) | ||
|
|
||
| def connection(self, api_base: str | None, api_key: str | None) -> DatabricksDecisionsConnection: | ||
| base: Final = self.resolve_api_base(api_base) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| from litellm.litellm_core_utils.litellm_logging import Logging | ||
| from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER | ||
| from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler | ||
| from litellm.llms.databricks.decisions.transformation import DATABRICKS_DECISIONS_CONFIG | ||
| from litellm.llms.databricks.decisions.transformation import DATABRICKS_DECISIONS_CONFIG, is_ai_decide_model | ||
| from litellm.llms.laya.common_utils import laya_response_model | ||
| from litellm.proxy.pass_through_endpoints.llm_provider_handlers.typesafe_passthrough_logging_handler import ( | ||
| TypeSafePassthroughLoggingHandler, | ||
|
|
@@ -35,6 +35,7 @@ | |
| JevProbability: TypeAlias = Annotated[float, Field(ge=0.0, le=1.0)] | ||
| ClassifierProvider: TypeAlias = Literal["typesafe", "laya", "bespoke", "databricks"] | ||
| DEFAULT_JEV_INSTRUCTIONS: Final = _DEFAULT_JEV_INSTRUCTIONS | ||
| _WITHOUT_MODEL: Final = MappingProxyType({"model": True}) | ||
|
|
||
|
|
||
| class JevChoiceQuestion(LiteLLMBaseModel): | ||
|
|
@@ -183,7 +184,7 @@ async def evaluate( | |
| ) | ||
| response: Final = await self._http_client.post( # pyright: ignore[reportUnknownMemberType] # AsyncHTTPHandler has a dynamic post signature | ||
| self._request_url(request.model), | ||
| json=request.model_dump(mode="json"), | ||
| json=request.model_dump(mode="json", exclude=self._excluded_fields(request.model)), | ||
| headers=MappingProxyType({**authorization, "Content-Type": "application/json"}), # pyright: ignore[reportArgumentType] # HTTP headers are not mutated by AsyncHTTPHandler | ||
| timeout=timeout_s, | ||
| ) | ||
|
|
@@ -196,6 +197,9 @@ async def evaluate( | |
| verbose_router_logger.warning("JEV response logging failed (%s)", type(exc).__name__) | ||
| return TypeAdapter(JevSystemOneResponse).validate_python(normalized_body) | ||
|
|
||
| def _excluded_fields(self, model: str) -> Mapping[str, bool] | None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low · Rule that ai_decide drops
|
||
| return _WITHOUT_MODEL if self._provider == "databricks" and is_ai_decide_model(model) else None | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low · Possible Duplicated Code / Feature Envy: rule to drop
|
||
|
|
||
| def _request_url(self, model: str) -> str: | ||
| if self._provider == "databricks": | ||
| return DATABRICKS_DECISIONS_CONFIG.get_complete_url(self._api_base, model) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low ·
ai_decidestring switch repeated across modules instead of a tagged unionAGENTS.mdsays "Use tagged unions + match". Instead,is_ai_decide_model(model)is anifon a rawstrin four places:validate_databricks_decisions_modelget_complete_urltransform_decisions_requestjev_classifier._excluded_fieldsParsing the model once into an
AiDecide | ServingEndpointunion incanonical_modelwould remove these branches. This is a judgement-level breach (Standards axis).transformation.py:33, 92, 103 and jev_classifier.py:201 all call
is_ai_decide_modelReview by code-review · Reviewed
74174369dd8bThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This backport keeps the code identical to #45722 on main, so a change here would belong there first. It's Low, so I'm tracking it as a follow-up