Threading the team based system

This commit is contained in:
Mahesh Kommareddi 2024-07-17 04:25:03 -04:00
parent a68d6fd419
commit 2859a502ba
2 changed files with 36 additions and 9 deletions

View File

@ -38,6 +38,11 @@ import org.springframework.messaging.simp.SimpMessagingTemplate;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CountDownLatch;
@SpringBootApplication
public class IoASystem {
@ -191,6 +196,9 @@ public class IoASystem {
);
// Create a thread pool
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (Task task : tasks) {
System.out.println("\nProcessing task: " + task.getDescription());
List<AgentInfo> team = teamFormation.formTeam(task);
@ -205,20 +213,29 @@ public class IoASystem {
conversationManager.addParticipant(conversationId, agent);
}
// Assign the task to all team members
// Create a CountDownLatch to wait for all agents to complete their tasks
CountDownLatch latch = new CountDownLatch(team.size());
// Assign the task to all team members and execute in parallel
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(), conversationId);
executorService.submit(() -> {
try {
Task agentTask = new Task(task.getId() + "_" + agent.getId(), task.getDescription(), task.getRequiredCapabilities(), task.getRequiredTools());
agentTask.setAssignedAgent(agent);
taskManager.addTask(agentTask);
taskManager.executeTask(agentTask.getId(), conversationId);
} finally {
latch.countDown();
}
});
}
// Start the conversation
conversationManager.startConversation(conversationId, "Let's work on the task: " + task.getDescription());
// Wait for the conversation to finish (you might want to implement a more sophisticated mechanism)
// Wait for all agents to complete their tasks
try {
Thread.sleep(30000); // Wait for 30 seconds
latch.await(5, TimeUnit.MINUTES); // Wait for up to 5 minutes
} catch (InterruptedException e) {
e.printStackTrace();
}
@ -230,5 +247,15 @@ public class IoASystem {
System.out.println("No suitable agents found for this task. Consider updating the agent pool or revising the task requirements.");
}
}
// Shutdown the executor service
executorService.shutdown();
try {
if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} catch (InterruptedException e) {
executorService.shutdownNow();
}
}
}