yunfengzhou-hub commented on code in PR #1114: URL: https://github.com/apache/flink-agents/pull/1114#discussion_r4058885560
########## api/src/main/java/org/apache/flink/agents/api/subagent/InputSchemas.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.subagent; + +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import javax.annotation.Nullable; + +/** + * Renders the input type a sub-agent declares as the JSON Schema a chat model is told about, so + * that a sub-agent which types its arguments does not also have to spell out their schema. + * + * <p>Rendering goes through the same Jackson generator {@code ReActAgent} renders a POJO output + * schema with, which keeps the two type-to-schema paths in this module on one implementation and + * adds no dependency. + */ +final class InputSchemas { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private InputSchemas() {} + + /** + * The schema of {@code type}, or {@code null} when the type states no shape a model could build + * a call from. That is {@link Object}, the type a sub-agent declares when it declares none, and + * any type that does not render as a JSON object, because the parameters of a callable must be + * one. + * + * @throws IllegalArgumentException if rendering the type fails, which is a declaration mistake + * worth failing on rather than dropping silently. + */ + @Nullable + static String fromType(@Nullable Class<?> type) { + if (type == null || type == Object.class) { + return null; + } + JsonNode schema = render(type); + return "object".equals(schema.path("type").asText()) ? schema.toString() : null; + } + + private static JsonNode render(Class<?> type) { + try { + return MAPPER.generateJsonSchema(type).getSchemaNode(); Review Comment: Took the first option: match on the schema name, and run the same step on nested objects and array items. Both cases now agree with Python, and the mirror tests on both sides cover them. -- 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]
