001/* 002 * Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.agentsflex.core.prompt; 017 018import com.agentsflex.core.message.Message; 019import com.agentsflex.core.model.chat.tool.Tool; 020import com.agentsflex.core.model.chat.tool.ToolScanner; 021import com.agentsflex.core.util.Metadata; 022 023import java.util.*; 024 025 026public abstract class Prompt extends Metadata { 027 028 public abstract List<Message> getMessages(); 029 030 private List<Tool> tools; 031 private String toolChoice; 032 033 public void addTool(Tool tool) { 034 if (this.tools == null) 035 this.tools = new java.util.ArrayList<>(); 036 this.tools.add(tool); 037 } 038 039 public void addTools(Collection<? extends Tool> functions) { 040 if (this.tools == null) { 041 this.tools = new java.util.ArrayList<>(); 042 } 043 if (functions != null) { 044 this.tools.addAll(functions); 045 } 046 } 047 048 public void addToolsFromClass(Class<?> funcClass, String... methodNames) { 049 if (this.tools == null) 050 this.tools = new java.util.ArrayList<>(); 051 this.tools.addAll(ToolScanner.scan(funcClass, methodNames)); 052 } 053 054 public void addToolsFromObject(Object funcObject, String... methodNames) { 055 if (this.tools == null) 056 this.tools = new java.util.ArrayList<>(); 057 this.tools.addAll(ToolScanner.scan(funcObject, methodNames)); 058 } 059 060 public List<Tool> getTools() { 061 return tools; 062 } 063 064 public Map<String, Tool> getToolsMap() { 065 if (tools == null) { 066 return Collections.emptyMap(); 067 } 068 Map<String, Tool> map = new HashMap<>(tools.size()); 069 for (Tool tool : tools) { 070 map.put(tool.getName(), tool); 071 } 072 return map; 073 } 074 075 public void setTools(List<? extends Tool> tools) { 076 if (tools == null) { 077 this.tools = null; 078 } else { 079 this.tools = new ArrayList<>(tools); 080 } 081 } 082 083 public String getToolChoice() { 084 return toolChoice; 085 } 086 087 public void setToolChoice(String toolChoice) { 088 this.toolChoice = toolChoice; 089 } 090}