purushah commented on code in PR #1484:
URL: https://github.com/apache/flink-agents/pull/1484#discussion_r4779789872
##########
api/src/main/java/org/apache/flink/agents/api/agents/AgentExecutionOptions.java:
##########
@@ -124,4 +124,11 @@ public class AgentExecutionOptions {
"short-term-memory.state-ttl.visibility",
ShortTermMemoryTtlVisibility.class,
ShortTermMemoryTtlVisibility.NEVER_RETURN_EXPIRED);
+
+ /**
+ * The most characters of one tool result that the built-in chat action
sends to the model; a
+ * longer result is cut in the middle. At least 100. Unset by default:
results are sent in full.
+ */
+ public static final ConfigOption<Integer>
CONTEXT_MANAGEMENT_TOOL_RESULTS_MAX_CHARS =
Review Comment:
Done. It is `ContextManagementOptions.TOOL_RESULTS_MAX_CHARS` now. Same key.
##########
api/src/main/java/org/apache/flink/agents/api/event/ChatRequestEvent.java:
##########
@@ -193,4 +200,46 @@ public Map<String, Object> getPromptArgs() {
public Map<String, Object> getUserContext() {
return UserDefinedContext.get(this);
}
+
+ /**
+ * Sets the policy of this request, which replaces the agent's default
policy as a whole. With
+ * {@code null}, the request uses the agent's default policy.
+ *
+ * @return this event
+ */
+ public ChatRequestEvent withContextManagement(@Nullable
ContextManagementPolicy policy) {
+ if (policy == null) {
+ getAttributes().remove(CONTEXT_MANAGEMENT);
+ } else {
+ setAttr(CONTEXT_MANAGEMENT, policy.toMap());
Review Comment:
Done. The event stores the policy and Jackson reads it. The input stays
strict: `"2000"`, `2000.7` and `""` are rejected.
##########
api/src/main/java/org/apache/flink/agents/api/chat/contextmanagement/ContextManagementPolicy.java:
##########
@@ -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.
+ */
+
+package org.apache.flink.agents.api.chat.contextmanagement;
+
+import org.apache.flink.agents.api.agents.AgentExecutionOptions;
+import org.apache.flink.agents.api.configuration.ReadableConfiguration;
+
+import javax.annotation.Nullable;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * How the built-in chat action bounds the model input of one chat request.
+ *
+ * <p>A chat request and its tool-call loop send the request's whole
conversation to the model on
+ * every call, including every earlier tool result. A policy limits how much
of one tool result is
+ * sent ({@link #getToolResultMaxChars()}). Only the messages sent to the
model change; the
+ * conversation the framework keeps for the request stays complete.
+ *
+ * <p>The default policy of an agent comes from the {@code
context-management.*} execution options
+ * ({@link #fromConfiguration}). A {@link
org.apache.flink.agents.api.event.ChatRequestEvent} can
+ * carry its own policy, which replaces the default as a whole for that
request; {@link #disabled()}
+ * switches context management off for the request.
+ */
+public final class ContextManagementPolicy {
+
+ /** Map form key of {@link #getToolResultMaxChars()}. */
+ public static final String TOOL_RESULT_MAX_CHARS = "tool_result_max_chars";
+
+ /** The smallest accepted limit. It leaves room for the marker of a cut
and text around it. */
+ public static final int MIN_TOOL_RESULT_MAX_CHARS = 100;
+
+ private static final ContextManagementPolicy DISABLED =
+ new ContextManagementPolicy(null, TOOL_RESULT_MAX_CHARS);
+
+ @Nullable private final Integer toolResultMaxChars;
+
+ /**
+ * @param name what an error calls the limit: its map form key or its
configuration key
+ */
+ private ContextManagementPolicy(@Nullable Integer toolResultMaxChars,
String name) {
+ if (toolResultMaxChars != null && toolResultMaxChars <
MIN_TOOL_RESULT_MAX_CHARS) {
+ throw new IllegalArgumentException(
+ name
+ + " must be at least "
+ + MIN_TOOL_RESULT_MAX_CHARS
+ + ", got "
+ + toolResultMaxChars);
+ }
+ this.toolResultMaxChars = toolResultMaxChars;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /** A policy with nothing set. On a chat request it switches context
management off. */
+ public static ContextManagementPolicy disabled() {
+ return DISABLED;
+ }
+
+ /**
+ * The policy declared by the {@code context-management.*} execution
options, disabled when none
+ * of them is set.
+ *
+ * @throws IllegalArgumentException if a configured value is not an
integer or is out of range,
+ * named by its configuration key
+ */
+ public static ContextManagementPolicy
fromConfiguration(ReadableConfiguration config) {
Review Comment:
Done. Moved to `ContextManager.defaultPolicy(config)` in `plan`.
##########
plan/src/main/java/org/apache/flink/agents/plan/actions/ChatModelAction.java:
##########
@@ -180,6 +194,9 @@ private static void saveToolRequestEventContext(
if (userContext != null) {
context.put(USER_CONTEXT, userContext);
}
+ if (requestPolicy != null) {
+ context.put(CONTEXT_MANAGEMENT, requestPolicy.toMap());
Review Comment:
Done. The recovery test covers a limit and `disabled()`.
##########
plan/src/main/java/org/apache/flink/agents/plan/actions/ChatModelAction.java:
##########
@@ -319,9 +338,16 @@ private static void chat(
Map<String, Object> promptArgs,
@Nullable Object outputSchema,
@Nullable Map<String, Object> userContext,
+ @Nullable ContextManagementPolicy requestPolicy,
RunnerContext ctx,
@Nullable String reason)
throws Exception {
+ ContextManagementPolicy policy =
+ requestPolicy != null
+ ? requestPolicy
+ :
ContextManagementPolicy.fromConfiguration(ctx.getConfig());
+ List<ChatMessage> sentMessages = ToolResultTruncation.apply(messages,
policy);
Review Comment:
Done. `ChatModelAction` calls `ContextManager.apply(messages, policy)`.
##########
python/flink_agents/api/core_options.py:
##########
@@ -396,6 +396,17 @@ class AgentExecutionOptions:
default=ShortTermMemoryTtlVisibility.NEVER_RETURN_EXPIRED,
)
+ # The most characters of one tool result that the built-in chat action
sends to
+ # the model; a longer result is cut in the middle. At least 100. Unset by
default:
+ # tool results are sent in full. The built-in chat action of Java agents
applies
+ # it. The one of Python agents does not yet; the option is declared here
to keep
+ # the Java and Python options the same.
+ CONTEXT_MANAGEMENT_TOOL_RESULTS_MAX_CHARS = ConfigOption(
Review Comment:
Removed.
##########
plan/src/main/java/org/apache/flink/agents/plan/actions/ChatModelAction.java:
##########
@@ -331,7 +357,7 @@ private static void chat(
ModelRoutingExecutor.execute(
initialRequestId,
route,
- messages,
+ sentMessages,
Review Comment:
Done. Added tests for fallback and escalation.
--
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]