Repository navigation
[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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1e9010c
[api][plan][runtime][examples] Add pluggable in-chat model routing (M…
purushah 040fb5f
[bug][plan] Make the routing durable-call id deterministic across rec…
purushah 8520e82
[review] Address weiqingy's review: harden routing failure paths
purushah 99938eb
[review] Fix null tool-calls NPE in RoutingContext copy; correct conf…
purushah ad3a55f
[review] Register ModelRoutingEvent in EventType.ALL_CONSTANTS
purushah df1a8e1
[examples] Move OpenAiModelRoutingExample out of the auto-submitted p…
purushah e9a99fa
[review] Park routing metadata once per reasoning loop instead of car…
purushah c93d63e
[review] Extract routing resolution and model invocation from ChatMod…
purushah c073a82
[review] Round-2 review: Python enum member, routed-id/retry/backstop…
purushah cd95d5f
[hotfix] Sort imports in cross-language plan test (ruff I001)
purushah e95f20b
[review] Validate rule regex patterns at build(); drop redundant init…
purushah 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
259 changes: 259 additions & 0 deletions
259
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,259 @@ | ||
| /* | ||
| * 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; | ||
| import java.util.regex.Pattern; | ||
| import java.util.regex.PatternSyntaxException; | ||
|
|
||
| /** | ||
| * 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 and rule values are regex patterns; validate both | ||
| // here, where the full map is in hand, so a typo fails at the registration call site | ||
| // instead of throwing per record at runtime (an invalid pattern is never cached by | ||
| // the resource cache, so it would otherwise re-throw on every routed request). | ||
| if (RuleBasedRoutingStrategy.class.getName().equals(strategy.getClazz())) { | ||
| Object rules = strategy.getArguments().get("rules"); | ||
| if (rules instanceof Map) { | ||
| for (Map.Entry<?, ?> rule : ((Map<?, ?>) rules).entrySet()) { | ||
| if (!candidates.contains(String.valueOf(rule.getKey()))) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Routing rule key '%s' is not one of the candidates %s.", | ||
| rule.getKey(), candidates)); | ||
| } | ||
| if (!(rule.getValue() instanceof String)) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Routing rule pattern for candidate '%s' must be a non-null String, got %s.", | ||
| rule.getKey(), | ||
| rule.getValue() == null | ||
| ? "null" | ||
| : rule.getValue().getClass().getSimpleName())); | ||
| } | ||
| try { | ||
| Pattern.compile((String) rule.getValue()); | ||
| } catch (PatternSyntaxException e) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Routing rule pattern '%s' for candidate '%s' is not a valid regex.", | ||
| rule.getValue(), rule.getKey()), | ||
| e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } | ||
| } |
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.