ArafatKhan2198 commented on code in PR #9915: URL: https://github.com/apache/ozone/pull/9915#discussion_r3264705458
########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/chatbot/agent/ChatbotAgent.java: ########## @@ -0,0 +1,773 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.ozone.recon.chatbot.agent; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.recon.chatbot.ChatbotConfigKeys; +import org.apache.hadoop.ozone.recon.chatbot.llm.LLMClient; +import org.apache.hadoop.ozone.recon.chatbot.llm.LLMClient.ChatMessage; +import org.apache.hadoop.ozone.recon.chatbot.llm.LLMClient.LLMResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Main chatbot agent that orchestrates the conversation flow. + * Handles tool selection (figuring out what API to call), executing those calls, + * and summarization (feeding the data back to the LLM to write a nice answer). + */ +@Singleton +public class ChatbotAgent { + + private static final Logger LOG = LoggerFactory.getLogger(ChatbotAgent.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final Pattern JSON_PATTERN = Pattern.compile("\\{.*\\}", Pattern.DOTALL); Review Comment: Resolved. Removed JSON_PATTERN and added extractFirstJsonObject(), which uses brace-depth counting with string-awareness (ignores {/} inside quoted values, handles escapes). It returns the first complete outermost JSON object, so it works for SINGLE_ENDPOINT, MULTI_ENDPOINT (nested tool_calls), and DOCUMENTATION_QUERY, and when the LLM wraps JSON in prose. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
