I'll never understand beans

This commit is contained in:
Mahesh Kommareddi 2024-07-16 23:45:35 -04:00
parent 9983a530a5
commit 44cced45e3
8 changed files with 55 additions and 47 deletions

View File

@ -10,10 +10,13 @@ import com.ioa.tool.ToolRegistry;
import com.ioa.model.BedrockLanguageModel; import com.ioa.model.BedrockLanguageModel;
import com.ioa.service.WebSocketService; import com.ioa.service.WebSocketService;
import com.ioa.tool.Tool; import com.ioa.tool.Tool;
import com.ioa.util.TreeOfThought;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
@ -46,8 +49,13 @@ public class IoASystem {
} }
@Bean @Bean
public TeamFormation teamFormation(AgentRegistry agentRegistry, BedrockLanguageModel model) { public TreeOfThought treeOfThought(BedrockLanguageModel model, WebSocketService webSocketService) {
return new TeamFormation(agentRegistry, model); return new TreeOfThought(model, webSocketService);
}
@Bean
public TeamFormation teamFormation(AgentRegistry agentRegistry, TreeOfThought treeOfThought, WebSocketService webSocketService) {
return new TeamFormation(agentRegistry, treeOfThought, webSocketService);
} }
@Bean @Bean
@ -56,17 +64,17 @@ public class IoASystem {
} }
@Bean @Bean
public WebSocketService webSocketService() { public WebSocketService webSocketService(SimpMessagingTemplate messagingTemplate) {
return new WebSocketService(); return new WebSocketService(messagingTemplate);
} }
public static void main(String[] args) { public static void main(String[] args) {
var context = SpringApplication.run(IoASystem.class, args); ConfigurableApplicationContext context = SpringApplication.run(IoASystem.class, args);
AgentRegistry agentRegistry = context.getBean(AgentRegistry.class); AgentRegistry agentRegistry = context.getBean(AgentRegistry.class);
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 all agents // Register all agents
agentRegistry.registerAgent("agent1", new AgentInfo("agent1", "General Assistant", agentRegistry.registerAgent("agent1", new AgentInfo("agent1", "General Assistant",
Arrays.asList("general", "search"), Arrays.asList("general", "search"),
@ -92,32 +100,32 @@ public class IoASystem {
// Create all tasks // Create all tasks
List<Task> tasks = Arrays.asList( List<Task> tasks = Arrays.asList(
// new Task("task1", "Plan a weekend trip to Paris", new Task("task1", "Plan a weekend trip to Paris",
// Arrays.asList("travel", "booking"), 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", new Task("task2", "Organize a corporate team-building event in New York",
// Arrays.asList("event planning", "team management"), Arrays.asList("event planning", "team management"),
// Arrays.asList("findRestaurants", "bookTravel", "scheduleAppointment")), Arrays.asList("findRestaurants", "bookTravel", "scheduleAppointment")),
// new Task("task3", "Develop a personalized fitness and nutrition plan", new Task("task3", "Develop a personalized fitness and nutrition plan",
// Arrays.asList("health", "nutrition"), Arrays.asList("health", "nutrition"),
// Arrays.asList("getWeather", "findFitnessClasses", "getRecipe")), Arrays.asList("getWeather", "findFitnessClasses", "getRecipe")),
// new Task("task4", "Research and summarize recent advancements in renewable energy", new Task("task4", "Research and summarize recent advancements in renewable energy",
// Arrays.asList("research", "writing"), Arrays.asList("research", "writing"),
// Arrays.asList("webSearch", "getNewsUpdates", "translate")), Arrays.asList("webSearch", "getNewsUpdates", "translate")),
// new Task("task5", "Plan and execute a social media marketing campaign for a new product launch", new Task("task5", "Plan and execute a social media marketing campaign for a new product launch",
// Arrays.asList("marketing", "social media"), Arrays.asList("marketing", "social media"),
// Arrays.asList("webSearch", "getNewsUpdates", "scheduleAppointment")), Arrays.asList("webSearch", "getNewsUpdates", "scheduleAppointment")),
// new Task("task6", "Assist in planning a multi-city European vacation for a family of four", new Task("task6", "Assist in planning a multi-city European vacation for a family of four",
// Arrays.asList("travel", "family planning"), 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", 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("event planning", "tech expertise", "marketing", "travel coordination", "content creation"),
// Arrays.asList("scheduleAppointment", "webSearch", "bookTravel", "getWeather", "findRestaurants", "getNewsUpdates")), Arrays.asList("scheduleAppointment", "webSearch", "bookTravel", "getWeather", "findRestaurants", "getNewsUpdates")),
// new Task("task8", "Develop and launch a multi-lingual mobile app for sustainable tourism", 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("software development", "travel", "language expertise", "environmental science", "user experience design"),
// Arrays.asList("webSearch", "translate", "getWeather", "findRestaurants", "getNewsUpdates", "compareProductPrices")), 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", 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("health", "nutrition", "psychology", "corporate wellness", "data analysis"),

View File

@ -1,14 +1,16 @@
package com.ioa.service; package com.ioa.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class WebSocketService { public class WebSocketService {
@Autowired private final SimpMessagingTemplate messagingTemplate;
private SimpMessagingTemplate messagingTemplate;
public WebSocketService(SimpMessagingTemplate messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
public void sendUpdate(String topic, Object payload) { public void sendUpdate(String topic, Object payload) {
messagingTemplate.convertAndSend("/topic/" + topic, payload); messagingTemplate.convertAndSend("/topic/" + topic, payload);

View File

@ -5,8 +5,9 @@ import com.ioa.agent.AgentRegistry;
import com.ioa.model.BedrockLanguageModel; import com.ioa.model.BedrockLanguageModel;
import com.ioa.task.Task; import com.ioa.task.Task;
import com.ioa.util.TreeOfThought; import com.ioa.util.TreeOfThought;
import com.ioa.service.WebSocketService; import com.ioa.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.*; import java.util.*;
@ -14,15 +15,14 @@ import java.util.stream.Collectors;
@Component @Component
public class TeamFormation { public class TeamFormation {
private AgentRegistry agentRegistry; private final AgentRegistry agentRegistry;
private TreeOfThought treeOfThought; private final TreeOfThought treeOfThought;
private final WebSocketService webSocketService;
@Autowired
private WebSocketService webSocketService;
public TeamFormation(AgentRegistry agentRegistry, BedrockLanguageModel model) { public TeamFormation(AgentRegistry agentRegistry, TreeOfThought treeOfThought, WebSocketService webSocketService) {
this.agentRegistry = agentRegistry; this.agentRegistry = agentRegistry;
this.treeOfThought = new TreeOfThought(model); this.treeOfThought = treeOfThought;
this.webSocketService = webSocketService;
} }
public List<AgentInfo> formTeam(Task task) { public List<AgentInfo> formTeam(Task task) {

View File

@ -2,7 +2,6 @@ package com.ioa.util;
import com.ioa.model.BedrockLanguageModel; import com.ioa.model.BedrockLanguageModel;
import com.ioa.service.WebSocketService; import com.ioa.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@ -13,12 +12,11 @@ import java.util.Map;
@Component @Component
public class TreeOfThought { public class TreeOfThought {
private final BedrockLanguageModel model; private final BedrockLanguageModel model;
private final WebSocketService webSocketService;
@Autowired
private WebSocketService webSocketService;
public TreeOfThought(BedrockLanguageModel model) { public TreeOfThought(BedrockLanguageModel model, WebSocketService webSocketService) {
this.model = model; this.model = model;
this.webSocketService = webSocketService;
} }
public String reason(String task, int depth, int branches) { public String reason(String task, int depth, int branches) {