Zhuoxi2000 commented on code in PR #1427: URL: https://github.com/apache/flink-agents/pull/1427#discussion_r4643387784
########## api/src/main/java/org/apache/flink/agents/api/chat/model/ApproximateTokenEstimator.java: ########## @@ -0,0 +1,157 @@ +/* + * 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; + +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.messages.ContentBlock; +import org.apache.flink.agents.api.chat.messages.DataContentBlock; +import org.apache.flink.agents.api.chat.messages.ReasoningBlock; +import org.apache.flink.agents.api.chat.messages.TextBlock; +import org.apache.flink.agents.api.chat.messages.ToolCallBlock; +import org.apache.flink.agents.api.chat.messages.ToolResultBlock; +import org.apache.flink.agents.api.tools.Tool; + +import javax.annotation.Nullable; + +import java.lang.reflect.Array; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * Approximates the input tokens of a chat request from its size, for connections whose provider + * offers no way to count them. + * + * <p>The approximation is the same in Java and Python. Text costs one token per four ASCII + * characters and one token per other character. Every message, tool definition and media block adds + * a fixed cost. Tool call arguments are measured by their structure, so the result does not depend + * on how a map is rendered as text. Real token counts differ by model; the estimate is reported as + * {@linkplain TokenEstimate#isApproximate() approximate}. + */ +public final class ApproximateTokenEstimator { + + /** Fixed cost of one message: its role and the framing around its content. */ + static final int MESSAGE_OVERHEAD_TOKENS = 4; + + /** Fixed cost of one tool definition, beside its name, description and input schema. */ + static final int TOOL_OVERHEAD_TOKENS = 8; + + /** Flat cost of one image, audio, video or document block, whatever its size. */ + static final int MEDIA_BLOCK_TOKENS = 1024; + + private static final int UNITS_PER_TOKEN = 4; + private static final int NON_ASCII_UNITS = 4; + + private ApproximateTokenEstimator() {} + + /** + * Approximates the input tokens of a request with these messages and tools. + * + * @param messages the messages as they would be sent + * @param tools the tools as they would be bound to the request; null is a request without tools + */ + public static TokenEstimate estimate(List<ChatMessage> messages, @Nullable List<Tool> tools) { + long tokens = 0; + long units = 0; + for (ChatMessage message : messages) { + tokens += MESSAGE_OVERHEAD_TOKENS; + for (ContentBlock block : message.getBlocks()) { + if (block instanceof TextBlock) { + units += textUnits(((TextBlock) block).getText()); + } else if (block instanceof ReasoningBlock) { + units += textUnits(((ReasoningBlock) block).getText()); + } else if (block instanceof ToolCallBlock) { + ToolCallBlock call = (ToolCallBlock) block; + units += textUnits(call.getName()) + valueUnits(call.getInput()); + } else if (block instanceof ToolResultBlock) { + for (DataContentBlock nested : ((ToolResultBlock) block).getBlocks()) { + if (nested instanceof TextBlock) { + units += textUnits(((TextBlock) nested).getText()); + } else { + tokens += MEDIA_BLOCK_TOKENS; + } + } + } else { + tokens += MEDIA_BLOCK_TOKENS; + } + } + } + for (Tool tool : tools == null ? List.<Tool>of() : tools) { + tokens += TOOL_OVERHEAD_TOKENS; + units += + textUnits(tool.getName()) + + textUnits(tool.getDescription()) + + textUnits(tool.getMetadata().getInputSchema()); Review Comment: Python tools reach Java via json.dumps, with spaces (python_java_utils.py:223). So the same tool estimates differently per language. I measured lookup: Python 28 tokens, Java 29. -- 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]
