Fixed LLM prompt and team parsing

This commit is contained in:
Mahesh Kommareddi 2024-07-16 21:36:12 -04:00
parent a310b2364b
commit 195d2daa59
4 changed files with 126 additions and 49 deletions

View File

@ -67,39 +67,89 @@ public class IoASystem {
TeamFormation teamFormation = context.getBean(TeamFormation.class); TeamFormation teamFormation = context.getBean(TeamFormation.class);
TaskManager taskManager = context.getBean(TaskManager.class); TaskManager taskManager = context.getBean(TaskManager.class);
// Register some example agents // Register all agents
AgentInfo agent1 = new AgentInfo("agent1", "General Assistant", agentRegistry.registerAgent("agent1", new AgentInfo("agent1", "General Assistant",
Arrays.asList("general", "search"), Arrays.asList("general", "search"),
Arrays.asList("webSearch", "getWeather", "setReminder")); Arrays.asList("webSearch", "getWeather", "setReminder")));
AgentInfo agent2 = new AgentInfo("agent2", "Travel Expert", agentRegistry.registerAgent("agent2", new AgentInfo("agent2", "Travel Expert",
Arrays.asList("travel", "booking"), Arrays.asList("travel", "booking"),
Arrays.asList("bookTravel", "calculateDistance", "findRestaurants")); Arrays.asList("bookTravel", "calculateDistance", "findRestaurants")));
agentRegistry.registerAgent("agent3", new AgentInfo("agent3", "Event Planner Extraordinaire",
Arrays.asList("event planning", "team management", "booking"),
Arrays.asList("findRestaurants", "bookTravel", "scheduleAppointment", "getWeather")));
agentRegistry.registerAgent("agent4", new AgentInfo("agent4", "Fitness Guru",
Arrays.asList("health", "nutrition", "motivation"),
Arrays.asList("findFitnessClasses", "getRecipe", "setReminder", "getWeather")));
agentRegistry.registerAgent("agent5", new AgentInfo("agent5", "Research Specialist",
Arrays.asList("research", "writing", "analysis"),
Arrays.asList("webSearch", "getNewsUpdates", "translate", "compareProductPrices")));
agentRegistry.registerAgent("agent6", new AgentInfo("agent6", "Digital Marketing Expert",
Arrays.asList("marketing", "social media", "content creation"),
Arrays.asList("webSearch", "getNewsUpdates", "scheduleAppointment", "getMovieRecommendations")));
agentRegistry.registerAgent("agent7", new AgentInfo("agent7", "Family Travel Coordinator",
Arrays.asList("travel", "family planning", "budgeting"),
Arrays.asList("bookTravel", "calculateDistance", "getWeather", "findRestaurants", "getFinancialAdvice")));
agentRegistry.registerAgent(agent1.getId(), agent1); // Create all tasks
agentRegistry.registerAgent(agent2.getId(), agent2); List<Task> tasks = Arrays.asList(
new Task("task1", "Plan a weekend trip to Paris",
Arrays.asList("travel", "booking"),
Arrays.asList("bookTravel", "findRestaurants", "getWeather")),
new Task("task2", "Organize a corporate team-building event in New York",
Arrays.asList("event planning", "team management"),
Arrays.asList("findRestaurants", "bookTravel", "scheduleAppointment")),
new Task("task3", "Develop a personalized fitness and nutrition plan",
Arrays.asList("health", "nutrition"),
Arrays.asList("getWeather", "findFitnessClasses", "getRecipe")),
new Task("task4", "Research and summarize recent advancements in renewable energy",
Arrays.asList("research", "writing"),
Arrays.asList("webSearch", "getNewsUpdates", "translate")),
new Task("task5", "Plan and execute a social media marketing campaign for a new product launch",
Arrays.asList("marketing", "social media"),
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")),
// Create a sample task new Task("task7", "Organize an international tech conference with virtual and in-person components",
Task task = new Task("task1", "Plan a weekend trip to Paris", Arrays.asList("event planning", "tech expertise", "marketing", "travel coordination", "content creation"),
Arrays.asList("travel", "booking"), Arrays.asList("scheduleAppointment", "webSearch", "bookTravel", "getWeather", "findRestaurants", "getNewsUpdates")),
Arrays.asList("bookTravel", "findRestaurants", "getWeather"));
// Form a team for the task new Task("task8", "Develop and launch a multi-lingual mobile app for sustainable tourism",
List<AgentInfo> team = teamFormation.formTeam(task); Arrays.asList("software development", "travel", "language expertise", "environmental science", "user experience design"),
System.out.println("Formed team: " + team); Arrays.asList("webSearch", "translate", "getWeather", "findRestaurants", "getNewsUpdates", "compareProductPrices")),
if (team.isEmpty()) { new Task("task9", "Create a comprehensive health and wellness program for a large corporation, including mental health support",
System.out.println("No suitable agents found for the task. Exiting."); Arrays.asList("health", "nutrition", "psychology", "corporate wellness", "data analysis"),
return; 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("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"))
);
for (Task task : tasks) {
System.out.println("\nProcessing task: " + task.getDescription());
List<AgentInfo> team = teamFormation.formTeam(task);
System.out.println("Formed team: " + team);
if (!team.isEmpty()) {
// Assign the task to all team members
for (AgentInfo agent : team) {
Task agentTask = new Task(task.getId() + "_" + agent.getId(), task.getDescription(), task.getRequiredCapabilities(), task.getRequiredTools());
agentTask.setAssignedAgent(agent);
taskManager.addTask(agentTask);
taskManager.executeTask(agentTask.getId());
System.out.println("Task result for " + agent.getId() + ": " + agentTask.getResult());
}
} else {
System.out.println("No suitable agents found for this task. Consider updating the agent pool or revising the task requirements.");
}
} }
// Assign the task to the first agent in the team
task.setAssignedAgent(team.get(0));
// Execute the task
taskManager.addTask(task);
taskManager.executeTask(task.getId());
// Print the result
System.out.println("Task result: " + task.getResult());
} }
} }

View File

@ -7,6 +7,7 @@ import com.ioa.task.Task;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -25,38 +26,64 @@ public class TeamFormation {
List<String> requiredTools = task.getRequiredTools(); List<String> requiredTools = task.getRequiredTools();
List<AgentInfo> potentialAgents = agentRegistry.searchAgents(requiredCapabilities); List<AgentInfo> potentialAgents = agentRegistry.searchAgents(requiredCapabilities);
System.out.println("Potential agents: " + potentialAgents); System.out.println("DEBUG: Potential agents: " + potentialAgents);
String teamFormationTask = "Form the best team for this task: " + task.getDescription() + String initialPrompt = "Task: " + task.getDescription() + "\n" +
"\nRequired capabilities: " + requiredCapabilities + "Required capabilities: " + String.join(", ", requiredCapabilities) + "\n" +
"\nRequired tools: " + requiredTools + "Required tools: " + String.join(", ", requiredTools) + "\n" +
"\nAvailable agents and their tools: " + formatAgentTools(potentialAgents) + "Available agents:\n" + formatAgentDetails(potentialAgents) + "\n" +
"\nPlease respond with a comma-separated list of agent IDs that form the best team for this task."; "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, ...'";
System.out.println("Sending prompt to language model: " + teamFormationTask); System.out.println("DEBUG: Sending initial prompt to language model: " + initialPrompt);
String response = model.generate(teamFormationTask, null); String reasoning = model.generate(initialPrompt, null);
System.out.println("DEBUG: Language model reasoning:\n" + reasoning);
System.out.println("Language model response: " + response); return parseTeamComposition(reasoning, potentialAgents);
return parseTeamComposition(response, potentialAgents);
} }
private String formatAgentTools(List<AgentInfo> agents) { private String formatAgentDetails(List<AgentInfo> agents) {
return agents.stream() StringBuilder sb = new StringBuilder();
.map(agent -> agent.getId() + " (capabilities: " + agent.getCapabilities() + ", tools: " + agent.getTools() + ")") for (AgentInfo agent : agents) {
.collect(Collectors.joining(", ")); 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 List<AgentInfo> parseTeamComposition(String composition, List<AgentInfo> potentialAgents) { private List<AgentInfo> parseTeamComposition(String reasoning, List<AgentInfo> potentialAgents) {
List<String> selectedIds = Arrays.asList(composition.split(",")); // Extract the team selection from the reasoning
System.out.println("Parsed agent IDs: " + selectedIds); String[] lines = reasoning.split("\n");
String selectedTeamLine = "";
for (String line : lines) {
if (line.startsWith("Selected Team:")) {
selectedTeamLine = line.substring("Selected Team:".length()).trim();
break;
}
}
if (selectedTeamLine.isEmpty()) {
System.out.println("DEBUG: No team selection found in the response.");
return Collections.emptyList();
}
List<String> selectedIds = Arrays.asList(selectedTeamLine.split(",\\s*"));
System.out.println("DEBUG: Parsed agent IDs: " + selectedIds);
List<AgentInfo> team = potentialAgents.stream() List<AgentInfo> team = potentialAgents.stream()
.filter(agent -> selectedIds.contains(agent.getId().trim())) .filter(agent -> selectedIds.contains(agent.getId().trim()))
.collect(Collectors.toList()); .collect(Collectors.toList());
System.out.println("Final team: " + team); System.out.println("DEBUG: Final team: " + team);
return team; return team;
} }
} }