joeyutong commented on code in PR #1264:
URL: https://github.com/apache/flink-agents/pull/1264#discussion_r4311401600


##########
python/flink_agents/integrations/decision_models/openai_decision_model.py:
##########
@@ -0,0 +1,266 @@
+################################################################################
+#  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.
+################################################################################
+"""Decision resources backed by the official OpenAI Python SDK."""
+
+import json
+from collections.abc import Mapping
+from typing import Any, ClassVar
+
+from openai import OpenAI
+from openai.types import Decision as OpenAIDecision
+from openai.types.decision import Answer as OpenAIAnswer
+from pydantic import Field, JsonValue, PrivateAttr
+from typing_extensions import override
+
+from flink_agents.api.decision_models import (
+    BaseDecisionModelConnection,
+    BaseDecisionModelSetup,
+    ChoiceAnswer,
+    ChoiceQuestion,
+    DecisionAnswer,
+    DecisionQuestion,
+    DecisionRequest,
+    DecisionResult,
+    PredicateAnswer,
+    PredicateQuestion,
+    RefusalAnswer,
+    ScoreAnswer,
+    ScoreQuestion,
+)
+from flink_agents.api.decision_models.decision_model import 
_validate_model_name
+from flink_agents.integrations.decision_models.decision_model_utils import (
+    create_result,
+    prepare_request_params,
+)
+
+
+def _text(value: JsonValue, field: str) -> str:
+    if value is None:
+        msg = f"OpenAI {field} is required."
+        raise ValueError(msg)
+    return (
+        value
+        if isinstance(value, str)
+        else json.dumps(value, ensure_ascii=False, allow_nan=False)
+    )
+
+
+def _description(value: JsonValue, field: str) -> dict[str, str]:
+    return {} if value is None else {"description": _text(value, field)}
+
+
+def _questions(request: DecisionRequest) -> list[dict[str, Any]]:
+    max_questions = OpenAIDecisionModelConnection._MAX_QUESTIONS
+    if len(request.questions) > max_questions:
+        msg = f"OpenAI supports at most {max_questions} questions per decision 
request."
+        raise ValueError(msg)
+    questions = []
+    for name, question in request.questions.items():
+        entry: dict[str, Any] = {
+            "name": name,
+            "type": question.type,
+            "instructions": _text(
+                question.instructions, f"question {name!r} instructions"
+            ),
+        }
+        if isinstance(question, PredicateQuestion):
+            criteria = question.criteria
+            if criteria is not None and (
+                criteria.true is not None or criteria.false is not None
+            ):
+                msg = f"OpenAI question {name!r} does not support predicate 
criteria."
+                raise ValueError(msg)

Review Comment:
   Predicate criteria now fold into OpenAI instructions using Pydantic’s 
`yes`/`no` mapping and merge precedence, including criteria-only questions. 
Updated the existing SDK payload tests and provider conversion guide.
   
   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]

Reply via email to