mchades commented on code in PR #12858: URL: https://github.com/apache/gravitino/pull/12858#discussion_r4115654769
########## clients/client-python/gravitino/api/semantic/ai_context_object.py: ########## @@ -0,0 +1,209 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import copy +import math +from typing import Any, Final, Optional + +from gravitino.exceptions.base import IllegalArgumentException +from gravitino.utils.precondition import Precondition + +MAX_ADDITIONAL_PROPERTY_NESTING_DEPTH: Final[int] = 100 + +_STANDARD_PROPERTIES: Final[frozenset] = frozenset( + {"instructions", "synonyms", "examples"} +) + + +class AIContextObject: + """The structured form of AI context attached to a Semantic Model member. + + Unknown JSON-compatible properties are exposed through + :meth:`additional_properties` and are retained losslessly. + """ + + def __init__( + self, + instructions: Optional[str] = None, + synonyms: Optional[list[str]] = None, + examples: Optional[list[str]] = None, + additional_properties: Optional[dict[str, Any]] = None, + ): + _check_string_elements("synonyms", synonyms) + _check_string_elements("examples", examples) + + self._instructions = instructions + self._synonyms = None if synonyms is None else list(synonyms) + self._examples = None if examples is None else list(examples) + self._additional_properties = _normalize_additional_properties( + additional_properties + ) + + def instructions(self) -> Optional[str]: + """Returns the free-form instructions, or `None` if it is not set.""" + return self._instructions + + def synonyms(self) -> Optional[list[str]]: + """Returns the synonyms, or `None` if they are not set.""" + return None if self._synonyms is None else list(self._synonyms) + + def examples(self) -> Optional[list[str]]: + """Returns the examples, or `None` if they are not set.""" + return None if self._examples is None else list(self._examples) + + def additional_properties(self) -> dict[str, Any]: + """Returns the additional JSON-compatible properties, empty if none are set.""" + return copy.deepcopy(self._additional_properties) + + def __eq__(self, other: object) -> bool: + if not isinstance(other, AIContextObject): + return False + return ( + self._instructions == other.instructions() + and self._synonyms == other.synonyms() + and self._examples == other.examples() + and _freeze_json_value(self._additional_properties) + == _freeze_json_value(other.additional_properties()) + ) + + def __hash__(self) -> int: + return hash( + ( + self._instructions, + None if self._synonyms is None else tuple(self._synonyms), + None if self._examples is None else tuple(self._examples), + _freeze_json_value(self._additional_properties), + ) + ) + + def __repr__(self) -> str: + return ( + f"AIContextObject(instructions={self._instructions!r}, " + f"synonyms={self._synonyms!r}, examples={self._examples!r}, " + f"additionalProperties={self._additional_properties!r})" + ) + + +def _normalize_additional_properties( + properties: Optional[dict[str, Any]], +) -> dict[str, Any]: + if properties is None: + return {} + + normalized = {} + for name, value in properties.items(): + Precondition.check_argument( + isinstance(name, str), "additional property name must be a string" + ) + Precondition.check_argument( + name not in _STANDARD_PROPERTIES, + f"additional property must not duplicate standard property: {name}", + ) + normalized[name] = _normalize_json_value(value, name, set(), 0) + return normalized + + +def _normalize_json_value( + value: Any, path: str, visiting: set[int], container_depth: int +) -> Any: + if value is None or isinstance(value, (str, bool)): + return value + + if isinstance(value, (int, float)): Review Comment: One value semantics gap remains at `fd4f8531`: contexts containing `2.5` and `Decimal("2.5")` compare unequal because `_freeze_json_value` includes the Python type. I reproduced the same inequality (including the enclosing definition) and a dictionary lookup miss after `json.dumps(...)` followed by `json.loads(..., parse_float=Decimal)`, without changing the number. Java normalizes float values to `BigDecimal`. Please normalize float/Decimal consistently for equality and hashing, preserve the bool/integer distinctions, and add a regression test for these two representations. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
