joeyutong commented on code in PR #1264: URL: https://github.com/apache/flink-agents/pull/1264#discussion_r4311400488
########## python/flink_agents/integrations/decision_models/decision_model_utils.py: ########## @@ -0,0 +1,208 @@ +################################################################################ +# 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. +################################################################################ +"""Shared utilities for decision model integrations.""" + +from collections.abc import Collection, Mapping, Sequence +from decimal import Decimal +from typing import Any + +from pydantic import JsonValue + +from flink_agents.api.decision_models import ( + ChoiceAnswer, + ChoiceQuestion, + DecisionAnswer, + DecisionRequest, + DecisionResult, + PredicateAnswer, + PredicateQuestion, + RefusalAnswer, + ScoreAnswer, + ScoreQuestion, + TokenUsage, +) +from flink_agents.api.decision_models.decision_model import _validate_request_params + + +def prepare_request_params( + request_params: Mapping[str, JsonValue] | None, + *, + reserved_fields: Collection[str], +) -> dict[str, Any]: + """Copy call parameters and protect fields supplied by the framework.""" + params = _validate_request_params(request_params) + conflicts = params.keys() & set(reserved_fields) + extra_body = params.get("extra_body") + if isinstance(extra_body, dict): + conflicts |= extra_body.keys() & set(reserved_fields) + if conflicts: + msg = f"request_params cannot override reserved fields: {', '.join(sorted(conflicts))}." + raise ValueError(msg) + return params + + +def same_json_value(left: JsonValue, right: JsonValue) -> bool: + """Compare JSON content without conflating booleans with numeric values.""" + if isinstance(left, dict): + return ( + isinstance(right, dict) + and left.keys() == right.keys() + and all(same_json_value(value, right[key]) for key, value in left.items()) + ) + if isinstance(left, list): + return ( + isinstance(right, list) + and len(left) == len(right) + and all(same_json_value(a, b) for a, b in zip(left, right, strict=True)) + ) + return isinstance(left, bool) == isinstance(right, bool) and left == right + + +def _rounding_bounds(value: float) -> tuple[Decimal, Decimal]: + displayed = Decimal(str(value)) + places = max(2, -int(displayed.as_tuple().exponent)) + half_unit = Decimal(10) ** -places / 2 + return displayed - half_unit, displayed + half_unit Review Comment: Removed probability-sum and score/probability consistency checks instead of adding a tolerance. Basic value bounds and answer/request matching remain; both SDK paths preserve the reported numbers. Tests and the guide now reflect this contract. Updated in [55aadd78](https://github.com/apache/flink-agents/commit/55aadd783364e0ccdb02de0583ae141846421503). ########## api/src/main/java/org/apache/flink/agents/api/decision/BaseDecisionModelSetup.java: ########## @@ -0,0 +1,124 @@ +/* + * 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. + */ + +package org.apache.flink.agents.api.decision; + +import org.apache.flink.agents.api.resource.Resource; +import org.apache.flink.agents.api.resource.ResourceContext; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.resource.ResourceType; +import org.apache.flink.annotation.Internal; + +import javax.annotation.Nullable; + +import java.util.Map; +import java.util.Objects; + +/** + * A decision model configured with a connection and a model name. + * + * <p>Named connections are resolved through the resource context in {@link #open()}. Directly + * supplied connections remain the caller's responsibility. Closing a setup does not close a shared + * connection. Each call supplies its own evidence, questions, and provider request parameters. + */ +public class BaseDecisionModelSetup extends Resource { + @Nullable private final String connectionName; + private final String model; + @Nullable private BaseDecisionModelConnection connection; + + /** + * Configure a decision model using the descriptor's {@code connection} and {@code model}. + * + * @param descriptor configuration containing a connection resource name or connection instance + * and a non-blank provider model name + * @param resourceContext resource lookup context, required for a named connection + */ + public BaseDecisionModelSetup( + ResourceDescriptor descriptor, @Nullable ResourceContext resourceContext) { + super(Objects.requireNonNull(descriptor, "descriptor"), resourceContext); + Object configuredConnection = descriptor.getArgument("connection"); + if (configuredConnection instanceof BaseDecisionModelConnection) { + this.connectionName = null; + this.connection = (BaseDecisionModelConnection) configuredConnection; + } else { + this.connectionName = requireNonBlankString(configuredConnection, "connection"); + } + this.model = requireNonBlankString(descriptor.getArgument("model"), "model"); Review Comment: Aligned Setup lifecycle with Chat: named connections resolve in `open()`, where native Java configuration is validated; Python wrappers leave configuration and defaults to Python. Real Pemja tests cover default model/connection and explicit overrides. Updated in [55aadd78](https://github.com/apache/flink-agents/commit/55aadd783364e0ccdb02de0583ae141846421503). -- 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]
