Working tree-of-thought for agent selection

This commit is contained in:
Mahesh Kommareddi 2024-07-16 22:10:36 -04:00
parent 195d2daa59
commit cb5012a077
8 changed files with 58 additions and 54 deletions

View File

@ -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"))
);

View File

@ -16,4 +16,8 @@ public class AgentInfo {
this.capabilities = capabilities;
this.tools = tools;
}
public List<String> getCapabilities() {
return this.capabilities;
}
}

View File

@ -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<AgentInfo> searchAgents(List<String> capabilities) {
return searchAgents(capabilities, 1.0); // Default to exact match
}
public List<AgentInfo> searchAgents(List<String> 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<AgentInfo> searchAgentsPartial(List<String> capabilities) {
return searchAgents(capabilities, 0.0); // Return all agents with any matching capability
}
private double calculateMatchScore(List<String> agentCapabilities, List<String> requiredCapabilities) {
long matchingCapabilities = requiredCapabilities.stream()
.filter(agentCapabilities::contains)
.count();
return (double) matchingCapabilities / requiredCapabilities.size();
}
public List<AgentInfo> getAllAgents() {
return List.copyOf(agents.values());
return new ArrayList<>(agents.values());
}
}

View File

@ -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<AgentInfo> formTeam(Task task) {
List<String> requiredCapabilities = task.getRequiredCapabilities();
List<String> requiredTools = task.getRequiredTools();
List<AgentInfo> potentialAgents = agentRegistry.searchAgents(requiredCapabilities);
List<AgentInfo> 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<AgentInfo> 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<AgentInfo> agents) {
return agents.stream()
.map(agent -> agent.getId() + " (capabilities: " + agent.getCapabilities() + ", tools: " + agent.getTools() + ")")
.collect(Collectors.joining(", "));
}
private List<AgentInfo> parseTeamComposition(String reasoning, List<AgentInfo> 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<String> selectedIds = Arrays.asList(selectedTeamLine.split(",\\s*"));