Was using wrong field in BedrockAPI to retrieve generated results. Working finally.

This commit is contained in:
Mahesh Kommareddi 2024-07-17 05:14:00 -04:00
parent 2859a502ba
commit 1634dfcdc5
10 changed files with 97 additions and 21 deletions

View File

@ -176,23 +176,23 @@ public class IoASystem {
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")),
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

@ -44,9 +44,16 @@ public class ConversationManager {
}
public void postMessage(String conversationId, String senderId, String content) {
System.out.println("DEBUG: Posting message - ConversationId: " + conversationId + ", SenderId: " + senderId + ", Content: " + content);
ConversationFSM conversation = conversations.get(conversationId);
if (conversation != null) {
if (content == null) {
System.out.println("WARNING: Attempting to post null content message");
return;
}
conversation.postMessage(new Message(conversationId, senderId, content));
} else {
System.out.println("WARNING: Conversation not found for id: " + conversationId);
}
}

View File

@ -10,6 +10,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.stereotype.Component;
import java.nio.file.Files;
@ -32,6 +34,7 @@ public class BedrockLanguageModel {
}
public String generate(String prompt, String imagePath) {
System.out.println("DEBUG: Generating response for prompt: " + prompt);
try {
ObjectNode requestBody = objectMapper.createObjectNode();
requestBody.put("anthropic_version", "bedrock-2023-05-31");
@ -39,7 +42,7 @@ public class BedrockLanguageModel {
ObjectNode message = messages.addObject();
message.put("role", "user");
requestBody.put("max_tokens", 20000);
requestBody.put("temperature", 0.4);
requestBody.put("temperature", 0.7);
requestBody.put("top_p", 0.9);
ArrayNode content = message.putArray("content");
@ -71,12 +74,30 @@ public class BedrockLanguageModel {
InvokeModelResponse response = bedrockClient.invokeModel(invokeRequest);
String responseBody = response.body().asUtf8String();
System.out.println("DEBUG: Raw response from Bedrock: " + responseBody);
ObjectNode responseJson = (ObjectNode) objectMapper.readTree(responseBody);
return responseJson.path("content").get(0).path("text").asText();
JsonNode responseJson = objectMapper.readTree(responseBody);
JsonNode contentArray = responseJson.path("content");
if (contentArray.isArray() && contentArray.size() > 0) {
JsonNode firstContent = contentArray.get(0);
String generatedText = firstContent.path("text").asText();
if (generatedText.isEmpty()) {
System.out.println("WARNING: Generated text is empty. Full response: " + responseBody);
return "No response generated";
}
System.out.println("DEBUG: Generated text: " + generatedText);
return generatedText;
} else {
System.out.println("WARNING: Unexpected response format. Full response: " + responseBody);
return "Unexpected response format";
}
} catch (Exception e) {
throw new RuntimeException("Error generating text with Bedrock", e);
System.out.println("ERROR: Failed to generate text with Bedrock: " + e.getMessage());
e.printStackTrace();
return "Error: " + e.getMessage();
}
}
}

View File

@ -37,29 +37,48 @@ public class TaskManager {
Task task = tasks.get(taskId);
AgentInfo agent = task.getAssignedAgent();
System.out.println("DEBUG: Executing task: " + taskId + " for agent: " + agent.getId());
conversationManager.postMessage(conversationId, agent.getId(), "Starting task: " + task.getDescription());
String executionPlanningTask = "Plan the execution of this task: " + task.getDescription() +
"\nAssigned agent capabilities: " + agent.getCapabilities() +
"\nAvailable tools: " + agent.getTools();
System.out.println("DEBUG: Generating execution plan for task: " + taskId);
Map<String, Object> reasoningResult = treeOfThought.reason(executionPlanningTask, 3, 2);
String reasoning = (String) reasoningResult.get("reasoning"); // Assuming the reasoning is stored under the key "reasoning"
String reasoning = (String) reasoningResult.get("reasoning");
System.out.println("DEBUG: Execution plan generated: " + reasoning);
if (reasoning == null || reasoning.isEmpty()) {
System.out.println("WARNING: Empty execution plan generated for task: " + taskId);
reasoning = "No execution plan generated.";
}
conversationManager.postMessage(conversationId, agent.getId(), "Task execution plan:\n" + reasoning);
String executionPrompt = "Based on this execution plan:\n" + reasoning +
"\nExecute the task using the available tools and provide the result.";
Map<String, Object> executionResult = treeOfThought.reason(executionPrompt, 1, 1);
String response = (String) executionResult.get("response"); // Assuming the response is stored under the key "response"
String response = (String) executionResult.get("response");
if (response == null || response.isEmpty()) {
System.out.println("WARNING: Empty response generated for task execution: " + taskId);
response = "No response generated.";
}
String result = executeToolsFromResponse(response, agent);
task.setResult(result);
conversationManager.postMessage(conversationId, agent.getId(), "Task result: " + result);
if (result != null && !result.isEmpty()) {
conversationManager.postMessage(conversationId, agent.getId(), "Task result: " + result);
} else {
conversationManager.postMessage(conversationId, agent.getId(), "Task completed, but no result was generated.");
}
}
private String executeToolsFromResponse(String response, AgentInfo agent) {
StringBuilder result = new StringBuilder();
for (String tool : agent.getTools()) {

View File

@ -17,8 +17,11 @@ public class TreeOfThought {
}
public Map<String, Object> reason(String task, int depth, int branches) {
System.out.println("DEBUG: Starting reasoning process for task: " + task);
Map<String, Object> treeData = exploreThought(task, depth, branches, "Root");
webSocketService.sendUpdate("tree_of_thought", treeData);
String reasoning = formatReasoning(treeData);
System.out.println("DEBUG: Reasoning result: " + reasoning);
return treeData;
}
@ -67,4 +70,30 @@ public class TreeOfThought {
"\nPath: " + path + "\nEvaluations:\n" + childEvaluations;
return model.generate(prompt, null);
}
private String formatReasoning(Map<String, Object> treeData) {
StringBuilder sb = new StringBuilder();
formatNode(treeData, sb, 0);
return sb.toString();
}
private void formatNode(Map<String, Object> node, StringBuilder sb, int depth) {
String indent = " ".repeat(depth);
sb.append(indent).append(node.get("name")).append("\n");
if (node.containsKey("evaluation")) {
sb.append(indent).append("Evaluation: ").append(node.get("evaluation")).append("\n");
}
if (node.containsKey("conclusion")) {
sb.append(indent).append("Conclusion: ").append(node.get("conclusion")).append("\n");
}
if (node.containsKey("children")) {
List<Map<String, Object>> children = (List<Map<String, Object>>) node.get("children");
for (Map<String, Object> child : children) {
formatNode(child, sb, depth + 1);
}
}
}
}