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);
TaskManager taskManager = context.getBean(TaskManager.class);
// Register some example agents
AgentInfo agent1 = new AgentInfo("agent1", "General Assistant",
// Register all agents
agentRegistry.registerAgent("agent1", new AgentInfo("agent1", "General Assistant",
Arrays.asList("general", "search"),
Arrays.asList("webSearch", "getWeather", "setReminder"));
AgentInfo agent2 = new AgentInfo("agent2", "Travel Expert",
Arrays.asList("webSearch", "getWeather", "setReminder")));
agentRegistry.registerAgent("agent2", new AgentInfo("agent2", "Travel Expert",
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);
agentRegistry.registerAgent(agent2.getId(), agent2);
// Create a sample task
Task task = new Task("task1", "Plan a weekend trip to Paris",
// Create all tasks
List<Task> tasks = Arrays.asList(
new Task("task1", "Plan a weekend trip to Paris",
Arrays.asList("travel", "booking"),
Arrays.asList("bookTravel", "findRestaurants", "getWeather"));
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")),
// Form a team for the task
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("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("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()) {
System.out.println("No suitable agents found for the task. Exiting.");
return;
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 java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@ -25,38 +26,64 @@ public class TeamFormation {
List<String> requiredTools = task.getRequiredTools();
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() +
"\nRequired capabilities: " + requiredCapabilities +
"\nRequired tools: " + requiredTools +
"\nAvailable agents and their tools: " + formatAgentTools(potentialAgents) +
"\nPlease respond with a comma-separated list of agent IDs that form the best team for this task.";
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, ...'";
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(response, potentialAgents);
return parseTeamComposition(reasoning, potentialAgents);
}
private String formatAgentTools(List<AgentInfo> agents) {
return agents.stream()
.map(agent -> agent.getId() + " (capabilities: " + agent.getCapabilities() + ", tools: " + agent.getTools() + ")")
.collect(Collectors.joining(", "));
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 List<AgentInfo> parseTeamComposition(String composition, List<AgentInfo> potentialAgents) {
List<String> selectedIds = Arrays.asList(composition.split(","));
System.out.println("Parsed agent IDs: " + selectedIds);
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();
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()
.filter(agent -> selectedIds.contains(agent.getId().trim()))
.collect(Collectors.toList());
System.out.println("Final team: " + team);
System.out.println("DEBUG: Final team: " + team);
return team;
}
}