diff --git a/src/main/java/com/ioa/IoASystem.java b/src/main/java/com/ioa/IoASystem.java index 3ec8824..f306842 100644 --- a/src/main/java/com/ioa/IoASystem.java +++ b/src/main/java/com/ioa/IoASystem.java @@ -109,27 +109,27 @@ public class IoASystem { Arrays.asList("webSearch", "getNewsUpdates", "scheduleAppointment")), new Task("task6", "Assist in planning a multi-city European vacation for a family of four", Arrays.asList("travel", "family planning"), - Arrays.asList("bookTravel", "calculateDistance", "getWeather", "findRestaurants")), + Arrays.asList("bookTravel", "calculateDistance", "getWeather", "findRestaurants")) - new Task("task7", "Organize an international tech conference with virtual and in-person components", - Arrays.asList("event planning", "tech expertise", "marketing", "travel coordination", "content creation"), - Arrays.asList("scheduleAppointment", "webSearch", "bookTravel", "getWeather", "findRestaurants", "getNewsUpdates")), + // new Task("task7", "Organize an international tech conference with virtual and in-person components", + // Arrays.asList("event planning", "tech expertise", "marketing", "travel coordination", "content creation"), + // Arrays.asList("scheduleAppointment", "webSearch", "bookTravel", "getWeather", "findRestaurants", "getNewsUpdates")), - new Task("task8", "Develop and launch a multi-lingual mobile app for sustainable tourism", - Arrays.asList("software development", "travel", "language expertise", "environmental science", "user experience design"), - Arrays.asList("webSearch", "translate", "getWeather", "findRestaurants", "getNewsUpdates", "compareProductPrices")), + // new Task("task8", "Develop and launch a multi-lingual mobile app for sustainable tourism", + // Arrays.asList("software development", "travel", "language expertise", "environmental science", "user experience design"), + // Arrays.asList("webSearch", "translate", "getWeather", "findRestaurants", "getNewsUpdates", "compareProductPrices")), - new Task("task9", "Create a comprehensive health and wellness program for a large corporation, including mental health support", - Arrays.asList("health", "nutrition", "psychology", "corporate wellness", "data analysis"), - Arrays.asList("findFitnessClasses", "getRecipe", "setReminder", "getWeather", "scheduleAppointment", "getFinancialAdvice")), + // new Task("task9", "Create a comprehensive health and wellness program for a large corporation, including mental health support", + // Arrays.asList("health", "nutrition", "psychology", "corporate wellness", "data analysis"), + // Arrays.asList("findFitnessClasses", "getRecipe", "setReminder", "getWeather", "scheduleAppointment", "getFinancialAdvice")), - new Task("task10", "Plan and execute a global product launch campaign for a revolutionary eco-friendly technology", - Arrays.asList("marketing", "environmental science", "international business", "public relations", "social media"), - Arrays.asList("webSearch", "getNewsUpdates", "scheduleAppointment", "translate", "compareProductPrices", "bookTravel")), + // new Task("task10", "Plan and execute a global product launch campaign for a revolutionary eco-friendly technology", + // Arrays.asList("marketing", "environmental science", "international business", "public relations", "social media"), + // Arrays.asList("webSearch", "getNewsUpdates", "scheduleAppointment", "translate", "compareProductPrices", "bookTravel")), - new Task("task11", "Design and implement a smart city initiative focusing on transportation, energy, and public safety", - Arrays.asList("urban planning", "environmental science", "data analysis", "public policy", "technology integration"), - Arrays.asList("webSearch", "getWeather", "calculateDistance", "getNewsUpdates", "getFinancialAdvice", "findHomeServices")) + // new Task("task11", "Design and implement a smart city initiative focusing on transportation, energy, and public safety", + // Arrays.asList("urban planning", "environmental science", "data analysis", "public policy", "technology integration"), + // Arrays.asList("webSearch", "getWeather", "calculateDistance", "getNewsUpdates", "getFinancialAdvice", "findHomeServices")) ); diff --git a/src/main/java/com/ioa/agent/AgentInfo.java b/src/main/java/com/ioa/agent/AgentInfo.java index 55b156d..96628b1 100644 --- a/src/main/java/com/ioa/agent/AgentInfo.java +++ b/src/main/java/com/ioa/agent/AgentInfo.java @@ -16,4 +16,8 @@ public class AgentInfo { this.capabilities = capabilities; this.tools = tools; } + + public List getCapabilities() { + return this.capabilities; + } } \ No newline at end of file diff --git a/src/main/java/com/ioa/agent/AgentRegistry.java b/src/main/java/com/ioa/agent/AgentRegistry.java index 272f8f4..5ea4d42 100644 --- a/src/main/java/com/ioa/agent/AgentRegistry.java +++ b/src/main/java/com/ioa/agent/AgentRegistry.java @@ -3,9 +3,7 @@ package com.ioa.agent; import com.ioa.tool.ToolRegistry; import org.springframework.stereotype.Component; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; @Component @@ -32,12 +30,28 @@ public class AgentRegistry { } public List searchAgents(List capabilities) { + return searchAgents(capabilities, 1.0); // Default to exact match + } + + public List searchAgents(List capabilities, double matchThreshold) { return agents.values().stream() - .filter(agent -> agent.getCapabilities().containsAll(capabilities)) + .filter(agent -> calculateMatchScore(agent.getCapabilities(), capabilities) >= matchThreshold) + .sorted(Comparator.comparingDouble((AgentInfo agent) -> calculateMatchScore(agent.getCapabilities(), capabilities)).reversed()) .collect(Collectors.toList()); } + public List searchAgentsPartial(List capabilities) { + return searchAgents(capabilities, 0.0); // Return all agents with any matching capability + } + + private double calculateMatchScore(List agentCapabilities, List requiredCapabilities) { + long matchingCapabilities = requiredCapabilities.stream() + .filter(agentCapabilities::contains) + .count(); + return (double) matchingCapabilities / requiredCapabilities.size(); + } + public List getAllAgents() { - return List.copyOf(agents.values()); + return new ArrayList<>(agents.values()); } } \ No newline at end of file diff --git a/src/main/java/com/ioa/team/TeamFormation.java b/src/main/java/com/ioa/team/TeamFormation.java index 19b30d4..d0c19b5 100644 --- a/src/main/java/com/ioa/team/TeamFormation.java +++ b/src/main/java/com/ioa/team/TeamFormation.java @@ -4,76 +4,62 @@ import com.ioa.agent.AgentInfo; import com.ioa.agent.AgentRegistry; import com.ioa.model.BedrockLanguageModel; import com.ioa.task.Task; +import com.ioa.util.TreeOfThought; import org.springframework.stereotype.Component; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @Component public class TeamFormation { private AgentRegistry agentRegistry; - private BedrockLanguageModel model; + private TreeOfThought treeOfThought; public TeamFormation(AgentRegistry agentRegistry, BedrockLanguageModel model) { this.agentRegistry = agentRegistry; - this.model = model; + this.treeOfThought = new TreeOfThought(model); } public List formTeam(Task task) { List requiredCapabilities = task.getRequiredCapabilities(); List requiredTools = task.getRequiredTools(); - List potentialAgents = agentRegistry.searchAgents(requiredCapabilities); + List potentialAgents = agentRegistry.searchAgentsPartial(requiredCapabilities); System.out.println("DEBUG: Potential agents: " + potentialAgents); - String initialPrompt = "Task: " + task.getDescription() + "\n" + - "Required capabilities: " + String.join(", ", requiredCapabilities) + "\n" + - "Required tools: " + String.join(", ", requiredTools) + "\n" + - "Available agents:\n" + formatAgentDetails(potentialAgents) + "\n" + - "Instructions: Analyze the task requirements and the available agents. " + - "Form the best team by selecting agents whose combined capabilities and tools meet the task requirements. " + - "Consider the following steps:\n" + - "1. Identify which capabilities and tools are crucial for the task.\n" + - "2. Match these requirements with the available agents.\n" + - "3. Consider how agents can complement each other's skills and tools.\n" + - "4. Aim to cover all required capabilities and tools with the smallest effective team.\n" + - "5. If a perfect match isn't possible, prioritize the most important requirements.\n" + - "Provide your reasoning for each step, then conclude with a final team selection in this format: 'Selected Team: agent1, agent2, ...'"; + String teamFormationTask = "Form the best team for this task: " + task.getDescription() + + "\nRequired capabilities: " + requiredCapabilities + + "\nRequired tools: " + requiredTools + + "\nAvailable agents and their tools: " + formatAgentTools(potentialAgents) + + "\nAnalyze the task, evaluate agents, and propose a team composition. " + + "Conclude with a final team selection in the format: 'Final Team Selection: agent1, agent2, ...'"; - System.out.println("DEBUG: Sending initial prompt to language model: " + initialPrompt); - - String reasoning = model.generate(initialPrompt, null); - System.out.println("DEBUG: Language model reasoning:\n" + reasoning); + String reasoning = treeOfThought.reason(teamFormationTask, 3, 2); + System.out.println("DEBUG: Tree of Thought reasoning:\n" + reasoning); return parseTeamComposition(reasoning, potentialAgents); } - private String formatAgentDetails(List agents) { - StringBuilder sb = new StringBuilder(); - for (AgentInfo agent : agents) { - sb.append(agent.getId()).append(":\n") - .append(" Capabilities: ").append(String.join(", ", agent.getCapabilities())).append("\n") - .append(" Tools: ").append(String.join(", ", agent.getTools())).append("\n\n"); - } - return sb.toString(); + private String formatAgentTools(List agents) { + return agents.stream() + .map(agent -> agent.getId() + " (capabilities: " + agent.getCapabilities() + ", tools: " + agent.getTools() + ")") + .collect(Collectors.joining(", ")); } private List parseTeamComposition(String reasoning, List potentialAgents) { - // Extract the team selection from the reasoning String[] lines = reasoning.split("\n"); String selectedTeamLine = ""; for (String line : lines) { - if (line.startsWith("Selected Team:")) { - selectedTeamLine = line.substring("Selected Team:".length()).trim(); + if (line.startsWith("Final Team Selection:")) { + selectedTeamLine = line.substring("Final Team Selection:".length()).trim(); break; } } if (selectedTeamLine.isEmpty()) { System.out.println("DEBUG: No team selection found in the response."); - return Collections.emptyList(); + return List.of(); } List selectedIds = Arrays.asList(selectedTeamLine.split(",\\s*")); diff --git a/target/classes/com/ioa/IoASystem.class b/target/classes/com/ioa/IoASystem.class index 525f66a..10fd1b8 100644 Binary files a/target/classes/com/ioa/IoASystem.class and b/target/classes/com/ioa/IoASystem.class differ diff --git a/target/classes/com/ioa/agent/AgentInfo.class b/target/classes/com/ioa/agent/AgentInfo.class index 6ac0c10..2431e20 100644 Binary files a/target/classes/com/ioa/agent/AgentInfo.class and b/target/classes/com/ioa/agent/AgentInfo.class differ diff --git a/target/classes/com/ioa/agent/AgentRegistry.class b/target/classes/com/ioa/agent/AgentRegistry.class index 8cf70e1..e17b46f 100644 Binary files a/target/classes/com/ioa/agent/AgentRegistry.class and b/target/classes/com/ioa/agent/AgentRegistry.class differ diff --git a/target/classes/com/ioa/team/TeamFormation.class b/target/classes/com/ioa/team/TeamFormation.class index d793ce0..f731d05 100644 Binary files a/target/classes/com/ioa/team/TeamFormation.class and b/target/classes/com/ioa/team/TeamFormation.class differ