Design an agentic chatbot. Users chat with it in conversations. For each query, the chatbot chooses the tools it needs, calls them and builds its reply from their results. A real system would use an LLM to choose the tools. Here, the fixed rules below replace the model, so every reply is deterministic. Keep the tool choice and the tools behind interfaces, so a real LLM or API could replace them without changing the chatbot's flow.
Every tool has a name, keywords, a response template and data that maps an input to an output. The data stands in for the tool's backend. Tools can fail, and a tool can be restricted to certain users.
AgenticChatbot(String fallbackResponse, int maxRetries, int maxToolCallsPerQuery)
fallbackResponse is the reply to a query that selects no tool.maxRetries times in the same query.maxToolCallsPerQuery tool calls in total, retries included.boolean startConversation(String conversationId, String userId)
userId.false if conversationId already exists.boolean registerTool(String toolName, List<String> keywords, String responseTemplate)
responseTemplate may contain {input} and {output}. Both are replaced by the values of a successful call.false if toolName is already registered.boolean addToolData(String toolName, String input, String output)
input returns output. Adding an existing input again replaces its output.false if the tool does not exist.boolean setToolFailures(String toolName, int failureCount)
failureCount calls to the tool fail, in any conversation. This replaces any failures still pending for the tool.false if the tool does not exist.boolean restrictTool(String toolName)
false if the tool does not exist.boolean grantToolAccess(String userId, String toolName)
userId use the tool. Access can be granted before or after the tool is restricted.false if the tool does not exist.String sendQuery(String conversationId, String query)
"" and records nothing if the conversation does not exist.List<String> getHistory(String conversationId)
"USER: <query>", "TOOL: <toolName>(<input>) -> <output>", "TOOL: <toolName>(<input>) -> FAILED" or "BOT: <reply>".USER line."Sorry, you do not have access to <toolName>.""Please share more details for <toolName>."TOOL line for every call. After a failed call, retry while the retry limit and the call limit allow. On success, the sentence is the template with every {input} and {output} filled in. If no call succeeds: "Sorry, <toolName> is unavailable right now."fallbackResponse if no tool was selected. Record the BOT line and return the reply.conversationId, userId and toolName have 1 to 30 English letters or digits.query has 1 to 20 words separated by single spaces.input has 1 to 20 lowercase English letters or digits.keywords has 1 to 10 items.fallbackResponse and responseTemplate have 1 to 200 characters, and output has 1 to 100 characters. They use printable ASCII characters, and the only braces are {input} and {output} inside templates.0 ≤ maxRetries ≤ 51 ≤ maxToolCallsPerQuery ≤ 1000 ≤ failureCount ≤ 1,000AgenticChatbot(fallbackResponse = "Sorry, I can only help with flights and hotels.", maxRetries = 0, maxToolCallsPerQuery = 5)registerTool(toolName = "flightStatus", keywords = ["flight", "plane"], responseTemplate = "Flight {input} is {output}.") returns trueaddToolData(toolName = "flightStatus", input = "uk811", output = "delayed by 40 minutes") returns trueregisterTool(toolName = "hotelPrice", keywords = ["hotel", "stay"], responseTemplate = "One night at {input} costs {output}.") returns trueaddToolData(toolName = "hotelPrice", input = "seaview", output = "4500 rupees") returns trueregisterTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns trueaddToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns trueregisterTool(toolName = "hotelPrice", keywords = ["room"], responseTemplate = "Rooms cost {output}.") returns falseaddToolData(toolName = "trainStatus", input = "t100", output = "on time") returns falsestartConversation(conversationId = "chat1", userId = "riya") returns truestartConversation(conversationId = "chat1", userId = "arjun") returns falsesendQuery(conversationId = "chat1", query = "good morning") returns "Sorry, I can only help with flights and hotels."sendQuery(conversationId = "chat1", query = "which gate should i go to") returns "Please share more details for gateNumber."sendQuery(conversationId = "chat1", query = "how much is a stay at seaview and is my plane uk811 late") returns "One night at seaview costs 4500 rupees. Flight uk811 is delayed by 40 minutes."sendQuery(conversationId = "chat5", query = "is my flight late") returns ""getHistory(conversationId = "chat1") returns ["USER: good morning", "BOT: Sorry, I can only help with flights and hotels.", "USER: which gate should i go to", "BOT: Please share more details for gateNumber.", "USER: how much is a stay at seaview and is my plane uk811 late", "TOOL: hotelPrice(seaview) -> 4500 rupees", "TOOL: flightStatus(uk811) -> delayed by 40 minutes", "BOT: One night at seaview costs 4500 rupees. Flight uk811 is delayed by 40 minutes."]Explanation:
AgenticChatbot(fallbackResponse = "I can only help with flights.", maxRetries = 1, maxToolCallsPerQuery = 3)registerTool(toolName = "flightStatus", keywords = ["flight"], responseTemplate = "Flight {input} is {output}.") returns trueaddToolData(toolName = "flightStatus", input = "ai202", output = "on time") returns trueaddToolData(toolName = "flightStatus", input = "uk811", output = "delayed by 40 minutes") returns trueregisterTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns trueaddToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns trueaddToolData(toolName = "gateNumber", input = "uk811", output = "C4") returns trueregisterTool(toolName = "seatUpgrade", keywords = ["upgrade"], responseTemplate = "Seat upgrade on flight {input} is {output}.") returns trueaddToolData(toolName = "seatUpgrade", input = "ai202", output = "confirmed") returns truerestrictTool(toolName = "seatUpgrade") returns truegrantToolAccess(userId = "riya", toolName = "seatUpgrade") returns truerestrictTool(toolName = "mealOrder") returns falsestartConversation(conversationId = "chat1", userId = "riya") returns truestartConversation(conversationId = "chat2", userId = "arjun") returns truesendQuery(conversationId = "chat1", query = "is flight ai202 on time") returns "Flight ai202 is on time."sendQuery(conversationId = "chat1", query = "and flight uk811") returns "Flight uk811 is delayed by 40 minutes."sendQuery(conversationId = "chat1", query = "which gate do i use") returns "Flight uk811 boards at gate C4."sendQuery(conversationId = "chat2", query = "which gate do i use") returns "Please share more details for gateNumber."sendQuery(conversationId = "chat1", query = "please upgrade my seat on ai202") returns "Seat upgrade on flight ai202 is confirmed."sendQuery(conversationId = "chat2", query = "upgrade my seat on flight ai202") returns "Sorry, you do not have access to seatUpgrade. Flight ai202 is on time."Explanation:
AgenticChatbot(fallbackResponse = "I can help with flights and hotels.", maxRetries = 2, maxToolCallsPerQuery = 4)registerTool(toolName = "flightStatus", keywords = ["flight"], responseTemplate = "Flight {input} is {output}.") returns trueaddToolData(toolName = "flightStatus", input = "ai202", output = "on time") returns trueregisterTool(toolName = "hotelPrice", keywords = ["hotel"], responseTemplate = "One night at {input} costs {output}.") returns trueaddToolData(toolName = "hotelPrice", input = "seaview", output = "4500 rupees") returns trueregisterTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns trueaddToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns truestartConversation(conversationId = "chat7", userId = "meera") returns truesetToolFailures(toolName = "flightStatus", failureCount = 2) returns truesetToolFailures(toolName = "visaRules", failureCount = 1) returns falsesendQuery(conversationId = "chat7", query = "status of flight ai202") returns "Flight ai202 is on time."setToolFailures(toolName = "hotelPrice", failureCount = 5) returns truesendQuery(conversationId = "chat7", query = "hotel seaview price and gate for flight ai202") returns "Sorry, hotelPrice is unavailable right now. Flight ai202 boards at gate B12. Sorry, flightStatus is unavailable right now."getHistory(conversationId = "chat7") returns ["USER: status of flight ai202", "TOOL: flightStatus(ai202) -> FAILED", "TOOL: flightStatus(ai202) -> FAILED", "TOOL: flightStatus(ai202) -> on time", "BOT: Flight ai202 is on time.", "USER: hotel seaview price and gate for flight ai202", "TOOL: hotelPrice(seaview) -> FAILED", "TOOL: hotelPrice(seaview) -> FAILED", "TOOL: hotelPrice(seaview) -> FAILED", "TOOL: gateNumber(ai202) -> B12", "BOT: Sorry, hotelPrice is unavailable right now. Flight ai202 boards at gate B12. Sorry, flightStatus is unavailable right now."]sendQuery(conversationId = "chat7", query = "price of hotel seaview") returns "One night at seaview costs 4500 rupees."Explanation:
AgenticChatbot(fallbackResponse = "I can tell you your gate.", maxRetries = 0, maxToolCallsPerQuery = 1)registerTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns trueaddToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns truestartConversation(conversationId = "chat3", userId = "kabir") returns truesendQuery(conversationId = "chat3", query = "i am on ai202") returns "I can tell you your gate."sendQuery(conversationId = "chat3", query = "hello") returns "I can tell you your gate."sendQuery(conversationId = "chat3", query = "thanks") returns "I can tell you your gate."sendQuery(conversationId = "chat3", query = "ok") returns "I can tell you your gate."sendQuery(conversationId = "chat3", query = "great") returns "I can tell you your gate."sendQuery(conversationId = "chat3", query = "which gate") returns "Flight ai202 boards at gate B12."sendQuery(conversationId = "chat3", query = "gate again") returns "Please share more details for gateNumber."Explanation: