wenjin272 commented on code in PR #1444: URL: https://github.com/apache/flink-agents/pull/1444#discussion_r4703249515
########## python/flink_agents/plan/routing/model_router.py: ########## @@ -0,0 +1,93 @@ +################################################################################ +# 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. +################################################################################# +"""The model router resource the engine instantiates for a router declaration.""" + +from typing import Any + +from pydantic import Field, PrivateAttr, model_validator + +from flink_agents.api.chat_models.routing import RouterDeclaration, RoutingStrategy +from flink_agents.api.resource import ( + Resource, + ResourceDescriptor, + ResourceName, + ResourceType, +) + +# The class path a declaration names: the placeholder the builder and the YAML +# ``router`` alias write, which is also the path of the api declaration class that +# descriptors named while the declaration was the resource. +_DECLARATION_CLASS = ResourceName.ModelRouter.MODEL_ROUTER + + +class ModelRouter(Resource): + """The ``MODEL_ROUTER`` resource: a router declaration decoded for the engine. + + It selects nothing by itself; ``flink_agents.plan.routing.resolver`` asks the + strategy for the first model. The declaration rules run when the agent plan is + built or restored (``validate_routing``), and again here on the effective + arguments, because the provider merges ``ModelRouter.*`` configuration into the + descriptor arguments when it creates this resource. + """ + + _routing_executor: Any = PrivateAttr(default=None) + + candidates: list[str] + candidate_descriptions: dict[str, str] = Field(default_factory=dict) + default_model: str | None = None + fallback: bool = False + strategy_type: str + strategy_args: dict[str, Any] = Field(default_factory=dict) + strategy_executor_class: str | None = None + + @model_validator(mode="after") + def _validate_effective_arguments(self) -> "ModelRouter": + RouterDeclaration( + **self.model_dump(include=set(RouterDeclaration.model_fields)) + ) + return self + + @classmethod + def resource_type(cls) -> ResourceType: + """Return the router resource type.""" + return ResourceType.MODEL_ROUTER + + @property + def strategy(self) -> RoutingStrategy: + """Return the strategy declaration.""" + return RoutingStrategy( + self.strategy_type, self.strategy_args, self.strategy_executor_class + ) + + @staticmethod + def resource_descriptor(declaration: ResourceDescriptor) -> ResourceDescriptor: + """Return ``declaration`` with this class named as the resource to provide. + + A Python descriptor that names the declaration is provided as this resource. + Any other descriptor, including a Java one, is returned unchanged. + """ + if declaration.language != "python" or ( + f"{declaration.target_module}.{declaration.target_clazz}" + != _DECLARATION_CLASS + ): + return declaration Review Comment: Could we align this with Java and always resolve Python router descriptors to the framework-owned router implementation? I don't think we need to support replacing the router resource through a custom `clazz`; custom routing strategies already have their own extension point. I see that retaining the old class path and custom `clazz` behavior is intended to preserve compatibility. We don't need to preserve backward compatibility for this refactoring at this stage, so let's use the opportunity to make the declaration/resource boundary consistent across Java and Python. The declaration should use an explicit placeholder instead of retaining `flink_agents.api.chat_models.routing.ModelRouter` as its `clazz`, since that class is now only the API declaration entry point. For naming, could we use `MODEL_ROUTER_PLACEHOLDER` instead of `DECIDE_IN_RUNTIME_ModelRouter` on both sides? The implementation is resolved during plan construction, so “runtime” is misleading here. We could also rename the existing MCP placeholder to `MCP_SERVER_PLACEHOLDER`, updating the corresponding constants, aliases, and tests together. -- 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]
