-
Notifications
You must be signed in to change notification settings - Fork 153
[api][plan][runtime][examples] Add pluggable in-chat model routing (MODEL_ROUTER) #964
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
Open
purushah
wants to merge
6
commits into
apache:main
Choose a base branch
from
purushah:model-routing-v1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e52d02
[api][plan][runtime][examples] Add pluggable in-chat model routing (M…
6ef088f
[bug][plan] Make the routing durable-call id deterministic across rec…
7e9302b
[review] Address weiqingy's review: harden routing failure paths
7d85d98
[review] Fix null tool-calls NPE in RoutingContext copy; correct conf…
7dade1a
[review] Register ModelRoutingEvent in EventType.ALL_CONSTANTS
6776d4c
[examples] Move OpenAiModelRoutingExample out of the auto-submitted p…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
api/src/main/java/org/apache/flink/agents/api/chat/model/routing/ModelRouter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| /* | ||
| * 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.chat.model.routing; | ||
|
|
||
| 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 java.lang.reflect.Constructor; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * A framework resource that <b>selects</b> a concrete chat model for a request. It does not call | ||
| * the backend itself — {@code ChatModelAction} resolves the router, runs its {@link | ||
| * RoutingStrategy} to get a {@link RoutingDecision}, and then runs the normal chat path against the | ||
| * chosen model. | ||
| * | ||
| * <p>Built with the fluent {@link #of(String...)} builder, which produces a {@link | ||
| * ResourceDescriptor} the framework instantiates reflectively. The strategy is carried by class | ||
| * name + args (see {@link RoutingStrategyDescriptor}) so it is plan-serializable. | ||
| * | ||
| * <p>Abstain ({@link RoutingDecision#abstain()}) → {@link #getDefaultModel()}. A returned name that | ||
| * is not a candidate is an invalid decision and is failed clearly by the caller. | ||
| */ | ||
| public class ModelRouter extends Resource { | ||
|
|
||
| private final List<RoutingCandidate> candidates; | ||
| private final String defaultModel; | ||
| private final boolean fallbackEnabled; | ||
| private final RoutingStrategy strategy; | ||
|
|
||
| public ModelRouter(ResourceDescriptor descriptor, ResourceContext resourceContext) | ||
| throws Exception { | ||
| super(descriptor, resourceContext); | ||
| List<String> names = descriptor.getArgument("candidates"); | ||
| if (names == null || names.isEmpty()) { | ||
| throw new IllegalArgumentException("ModelRouter requires at least one candidate."); | ||
| } | ||
| Map<String, String> descriptions = | ||
| descriptor.getArgument("candidate_descriptions", Collections.emptyMap()); | ||
| List<RoutingCandidate> parsed = new ArrayList<>(); | ||
| Set<String> uniqueNames = new LinkedHashSet<>(); | ||
| for (String name : names) { | ||
| if (!uniqueNames.add(name)) { | ||
| throw new IllegalArgumentException( | ||
| String.format("ModelRouter candidate '%s' is duplicated.", name)); | ||
| } | ||
| parsed.add(new RoutingCandidate(name, descriptions.get(name))); | ||
| } | ||
| this.candidates = Collections.unmodifiableList(parsed); | ||
| this.defaultModel = descriptor.getArgument("default_model"); | ||
| if (this.defaultModel != null && !isCandidate(this.defaultModel)) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "ModelRouter default model '%s' is not one of the candidates %s.", | ||
| this.defaultModel, getCandidateNames())); | ||
| } | ||
| this.fallbackEnabled = | ||
| Boolean.TRUE.equals(descriptor.getArgument("fallback", Boolean.FALSE)); | ||
| String strategyClazz = descriptor.getArgument("strategy_clazz"); | ||
| Map<String, Object> strategyArgs = | ||
| descriptor.getArgument("strategy_args", Collections.emptyMap()); | ||
| this.strategy = instantiateStrategy(strategyClazz, strategyArgs); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static RoutingStrategy instantiateStrategy(String clazz, Map<String, Object> args) | ||
| throws Exception { | ||
| if (clazz == null || clazz.isEmpty()) { | ||
| throw new IllegalArgumentException("ModelRouter requires a routing strategy."); | ||
| } | ||
| Class<?> c = Class.forName(clazz, true, Thread.currentThread().getContextClassLoader()); | ||
| try { | ||
| Constructor<?> ctor = c.getConstructor(Map.class); | ||
| return (RoutingStrategy) ctor.newInstance(args); | ||
| } catch (NoSuchMethodException noMapCtor) { | ||
| return (RoutingStrategy) c.getConstructor().newInstance(); | ||
| } | ||
| } | ||
|
|
||
| /** Run the strategy for the given context. */ | ||
| public RoutingDecision route(RoutingContext context) throws Exception { | ||
| return strategy.route(context); | ||
| } | ||
|
|
||
| public List<RoutingCandidate> getCandidates() { | ||
| return candidates; | ||
| } | ||
|
|
||
| public List<String> getCandidateNames() { | ||
| List<String> names = new ArrayList<>(); | ||
| for (RoutingCandidate candidate : candidates) { | ||
| names.add(candidate.getName()); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| public Optional<String> getDefaultModel() { | ||
| return Optional.ofNullable(defaultModel); | ||
| } | ||
|
|
||
| public boolean isFallbackEnabled() { | ||
| return fallbackEnabled; | ||
| } | ||
|
|
||
| /** Whether the given model name is one of this router's candidates. */ | ||
| public boolean isCandidate(String model) { | ||
| for (RoutingCandidate candidate : candidates) { | ||
| if (candidate.getName().equals(model)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public ResourceType getResourceType() { | ||
| return ResourceType.MODEL_ROUTER; | ||
| } | ||
|
|
||
| /** | ||
| * Start building a router over the given candidate model names (order matters for fallback). | ||
| */ | ||
| public static Builder of(String... candidates) { | ||
| return new Builder(Arrays.asList(candidates)); | ||
| } | ||
|
|
||
| /** Fluent builder that produces a {@link ResourceDescriptor} for a {@link ModelRouter}. */ | ||
| public static final class Builder { | ||
| private final List<String> candidates; | ||
| private final Map<String, String> descriptions = new HashMap<>(); | ||
| private RoutingStrategyDescriptor strategy; | ||
| private String defaultModel; | ||
| private boolean fallback = false; | ||
|
|
||
| private Builder(List<String> candidates) { | ||
| this.candidates = candidates; | ||
| } | ||
|
|
||
| public Builder strategy(RoutingStrategyDescriptor strategy) { | ||
| this.strategy = strategy; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Attach a human-readable description to a candidate, surfaced to strategies via {@link | ||
| * RoutingCandidate#getDescription()}. Descriptions are how semantic strategies — and future | ||
| * framework-managed LLM routing — learn what each candidate is for, so declare them here | ||
| * (once, on the router) rather than in per-strategy arguments. | ||
| */ | ||
| public Builder describe(String candidate, String description) { | ||
| if (!candidates.contains(candidate)) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Cannot describe '%s': not one of the candidates %s.", | ||
| candidate, candidates)); | ||
| } | ||
| descriptions.put(candidate, description); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder defaultModel(String defaultModel) { | ||
| this.defaultModel = defaultModel; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Whether to try remaining candidates (in declaration order) after the selected model has | ||
| * exhausted its own retry policy. Applies to the initial routed request only; tool-call | ||
| * rounds keep the already-selected model for conversation coherence. Fallback outcomes are | ||
| * recorded on the response ({@code model_routing} extra args) and as a second {@code | ||
| * ModelRoutingEvent} with source {@code fallback}. | ||
| */ | ||
| public Builder fallback(boolean fallback) { | ||
| this.fallback = fallback; | ||
| return this; | ||
| } | ||
|
|
||
| public ResourceDescriptor build() { | ||
| if (strategy == null) { | ||
| throw new IllegalStateException("ModelRouter requires a strategy(...)."); | ||
| } | ||
| // Rule keys are candidate names; validate here, where both lists are in hand, so a | ||
| // typo fails at the registration call site instead of throwing per record at runtime. | ||
| if (RuleBasedRoutingStrategy.class.getName().equals(strategy.getClazz())) { | ||
| Object rules = strategy.getArguments().get("rules"); | ||
| if (rules instanceof Map) { | ||
| for (Object ruleKey : ((Map<?, ?>) rules).keySet()) { | ||
| if (!candidates.contains(String.valueOf(ruleKey))) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Routing rule key '%s' is not one of the candidates %s.", | ||
| ruleKey, candidates)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Map<String, Object> args = new HashMap<>(); | ||
| args.put("candidates", new ArrayList<>(candidates)); | ||
| if (!descriptions.isEmpty()) { | ||
| args.put("candidate_descriptions", new HashMap<>(descriptions)); | ||
| } | ||
| if (defaultModel != null) { | ||
| args.put("default_model", defaultModel); | ||
| } | ||
| args.put("fallback", fallback); | ||
| args.put("strategy_clazz", strategy.getClazz()); | ||
| args.put("strategy_args", strategy.getArguments()); | ||
| return new ResourceDescriptor(ModelRouter.class.getName(), args); | ||
| } | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
api/src/main/java/org/apache/flink/agents/api/chat/model/routing/RoutingCandidate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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.chat.model.routing; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * A candidate chat model a {@link ModelRouter} may select, as seen by a {@link RoutingStrategy}. | ||
| * | ||
| * <p>Carries the candidate's registered model name plus an optional human-readable description | ||
| * (declared via {@code ModelRouter.Builder#describe}) that semantic strategies — and future | ||
| * framework-managed LLM routing — can use to decide. The name must resolve to a registered {@code | ||
| * CHAT_MODEL}. Per-candidate metadata (e.g. cost or load hints) is deferred until a strategy can | ||
| * actually consume it. | ||
| */ | ||
| public final class RoutingCandidate implements Serializable { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final String name; | ||
| private final String description; | ||
|
|
||
| public RoutingCandidate(String name, String description) { | ||
| if (name == null || name.isEmpty()) { | ||
| throw new IllegalArgumentException("Candidate name must be non-null and non-empty."); | ||
| } | ||
| this.name = name; | ||
| this.description = description == null ? "" : description; | ||
| } | ||
|
|
||
| public RoutingCandidate(String name) { | ||
| this(name, ""); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| RoutingCandidate that = (RoutingCandidate) o; | ||
| return name.equals(that.name) && description.equals(that.description); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name, description); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "RoutingCandidate{name='" + name + "', description='" + description + "'}"; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
After a rebase,
ModelRoutingEventwon't be inmain'sALL_CONSTANTS(EventType.java:45-54), andConditionExpressionValidator.java:125rejects anyEventType.Xthat isn't a key of it. So@Action("type == EventType.ModelRoutingEvent && ...")compiles, then fails plan validation. Plain@Action(EventType.ModelRoutingEvent)is unaffected.Worth adding the entry when you rebase?
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.
Right — will register
ModelRoutingEvent(plus a condition-expression test) during the rebase onto current main.