Skip to content

Commit 19d62f2

Browse files
committed
Change to new models.
1 parent b5e6df7 commit 19d62f2

File tree

7 files changed

+700
-91
lines changed

7 files changed

+700
-91
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131
implementation("com.openai:openai-java:0.8.0")
3232
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.2")
3333
implementation("dev.langchain4j:langchain4j:0.36.2")
34-
implementation("dev.langchain4j:langchain4j-open-ai:0.36.2")
34+
implementation("dev.langchain4j:langchain4j-open-ai:1.0.0-beta1")
3535
}
3636

3737
tasks.withType<Test> {
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package tech.indus340.managai.agentflow;
22

3-
import com.fasterxml.jackson.core.type.TypeReference;
3+
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import org.springframework.stereotype.Service;
6+
import tech.indus340.managai.agentflow.dto.ResultResponse;
67
import tech.indus340.managai.chatbot.OrchestratorAgent;
78

8-
import java.util.LinkedHashMap;
9-
import java.util.Map;
10-
119
@Service
1210
public class AgentFlowEngine {
1311

@@ -21,25 +19,30 @@ public AgentFlowEngine(OrchestratorAgent orchestratorAgent, ObjectMapper objectM
2119
this.workerAgentFactory = workerAgentFactory;
2220
}
2321

24-
public String agentFlow(String userMessage) {
22+
public ResultResponse agentFlow(String userMessage) throws JsonProcessingException {
2523
String resultJson = orchestratorAgent.chat(userMessage);
2624
System.out.println(resultJson);
27-
StringBuilder answers = new StringBuilder("USERMESSAGE: " + userMessage + "\n###\n");
25+
return objectMapper.readValue(resultJson, ResultResponse.class);
26+
}
27+
28+
public String executeFlow(ResultResponse result) {
29+
StringBuilder answers = new StringBuilder("USERMESSAGE: " + result.userMessage() + "\n###\n");
2830
try {
29-
// Parse the JSON string into a LinkedHashMap to maintain the order of keys
30-
Map<String, Map<String, String>> resultMap =
31-
objectMapper.readValue(resultJson, new TypeReference<LinkedHashMap<String, Map<String, String>>>() {});
32-
33-
for (Map.Entry<String, Map<String, String>> entry : resultMap.entrySet()) {
34-
String systemMessage = entry.getValue().get("system_message");
35-
String modelName = entry.getValue().get("model_name");
36-
double temp = Double.parseDouble(entry.getValue().get("temperature"));
37-
answers.append("\n###\nANSWER " + entry.getKey() + "\n###\n" + workerAgentFactory.build(systemMessage, modelName, temp).chat(answers.toString()));
31+
for (ResultResponse.AgentResponse agent : result.agents()) {
32+
String systemMessage = agent.systemMessage();
33+
String modelName = agent.model();
34+
double temp = agent.temperature() != null ? agent.temperature() : -1.0;
35+
answers.append("\n###\nANSWER ").append(agent.name()).append("\n###\n")
36+
.append(workerAgentFactory.build(systemMessage, modelName, temp).chat(answers.toString())).append("\n");
3837
}
38+
System.out.println(answers);
39+
return answers.toString();
40+
3941
} catch (Exception e) {
4042
e.printStackTrace();
4143
}
42-
return answers.toString();
44+
return null;
45+
//return answers.toString();
4346
}
4447

4548
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tech.indus340.managai.agentflow.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.util.List;
6+
7+
/**
8+
* A record representing the entire JSON object.
9+
* The JSON object now contains two properties: "userMessage" and "agents".
10+
*/
11+
public record ResultResponse(String userMessage, List<AgentResponse> agents) {
12+
13+
@JsonCreator
14+
public ResultResponse(
15+
@JsonProperty("userMessage") String userMessage,
16+
@JsonProperty("agents") List<AgentResponse> agents) {
17+
this.userMessage = userMessage;
18+
this.agents = agents;
19+
}
20+
21+
/**
22+
* A record representing an individual agent's response.
23+
* Each agent object has keys: "name", "model", "system_message", and "temperature".
24+
*/
25+
public record AgentResponse(
26+
@JsonProperty("name") String name,
27+
@JsonProperty("model") String model,
28+
@JsonProperty("system_message") String systemMessage,
29+
@JsonProperty("temperature") Double temperature
30+
) {}
31+
}

src/main/java/tech/indus340/managai/chatbot/OrchestratorAgentConfig.java

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
@Service
1212
public class OrchestratorAgentConfig {
1313

14-
private final static int KONTEXT_WINDOW_MESSAGES_SIZE = 1;
15-
1614
@Value("${openai.key}")
1715
private String openAIKey;
1816

@@ -25,75 +23,75 @@ public ChatCompletion orchestrationModel(String userMessage) {
2523
.role(ChatCompletionUserMessageParam.Role.USER)
2624
.content(ChatCompletionUserMessageParam.Content.ofTextContent(
2725
"""
28-
You are a powerful orchestration LLM which gets a user message request and instead of answering the request, you will split the task into different actions which execute different aspects of the question.
29-
You will create the system message for these smaller agents. Your answer to a question should be a ONLY the JSON representation of multiple ids (can be short names) and associated system messages for the agents. Only the valid JSON format, no other markup. Each agent in the sequence will get the complete result of the agents before with information about the agent itself.
30-
You can choose between "gpt-4o" and "gpt-4o-mini" as models, with "gpt-4o-mini" being the default. You also have to choose the temperature the agent should use as double between 0 and 1.
31-
There will be an external process which will sequentially execute your agents and provide the following agents with all information of the former agents. So an aggregating verifier at the end might make sense to create the final answer. Also, for complex tasks you could create several agents which would have suffix ids "-1", "-2" and so on. In this case, the surrounding process will feed the next verifier all answers to verify.
32-
The last agent in the sequence must provide the final result completely with all details, so as if only one agent was asked by the user. This has to be explicitly included in the system message for this last agent.
33-
34-
For example:
35-
36-
user request: "I want to plan an hour lesson for primary school kids to explain, mostly analog, the functionality of LLMs".
37-
38-
Sample output:
39-
{
40-
"ResearchAgent": {
41-
"system_message": "You are an agent tasked with researching age-appropriate analogies to explain the functionality of Large Language Models (LLMs) to primary school children. Provide several simple and relatable analogies that can help young students understand how LLMs work."
42-
"model_name": "gpt4o-mini",
43-
"temperature": 0.3
44-
},
45-
"StructureAgent": {
46-
"system_message": "You are an agent responsible for designing the structure of a one-hour lesson aimed at explaining the functionality of LLMs to primary school kids. Using the analogies provided by the ResearchAgent, create a detailed lesson outline that includes an introduction, main explanation, interactive activities, and a conclusion."
47-
"model_name": "gpt4o-mini",
48-
"temperature": 0.4
49-
},
50-
"MaterialsAgent": {
51-
"system_message": "You are an agent in charge of creating teaching materials for the lesson plan developed by the StructureAgent. Develop slides, handouts, and any other necessary materials that effectively utilize the analogies and explanations to help primary school students understand LLMs."
52-
"model_name": "gpt4o-mini",
53-
"temperature": 0.3
54-
},
55-
"ActivitiesAgent": {
56-
"system_message": "You are an agent tasked with planning interactive activities to include in the lesson. Based on the lesson structure and materials from the previous agents, design engaging, analog-based activities that reinforce the concepts of how LLMs function."
57-
"model_name": "gpt4o-mini",
58-
"temperature": 0.4
59-
},
60-
"Verifier": {
61-
"system_message": "You are an agent responsible for reviewing and verifying the entire lesson plan for clarity, age-appropriateness, and effectiveness in explaining the functionality of LLMs to primary school children. Ensure that all components are coherent, all relevant details are mentioned and suggest improvements if necessary."
62-
"model_name": "gpt4o",
63-
"temperature": 0
64-
}
65-
}
66-
67-
User Request: "I want to implement a Spring Boot application using gradle/kotlin. How to implement certain actions using GH actions?"
68-
69-
Sample Response:
70-
71-
{
72-
"IdeaAgent-1": {
73-
"system_message": "You are an agent tasked with generating ideas for implementing a Spring Boot application using Gradle and Kotlin, as well as integrating GitHub Actions for various actions. Provide suggestions for project setup, Gradle configurations, and GitHub Actions workflows."
74-
"model_name": "gpt4o",
75-
"temperature": 0.3
76-
},
77-
"IdeaAgent-2": {
78-
"system_message": "You are an agent tasked with generating ideas for implementing a Spring Boot application using Gradle and Kotlin, as well as integrating GitHub Actions for various actions. Provide suggestions for project setup, Gradle configurations, and GitHub Actions workflows."
79-
"model_name": "gpt4o-mini",
80-
"temperature": 0.5
81-
},
82-
"IdeaAgent-3": {
83-
"system_message": "You are an agent tasked with generating ideas for implementing a Spring Boot application using Gradle and Kotlin, as well as integrating GitHub Actions for various actions. Provide suggestions for project setup, Gradle configurations, and GitHub Actions workflows."
84-
"model_name": "gpt4o",
85-
"temperature": 0.2
86-
},
87-
"Verifier": {
88-
"system_message": "You are an agent responsible for reviewing and verifying the ideas generated by the IdeaAgents. Ensure that the proposed ideas are coherent, feasible, and effectively address the implementation of the Spring Boot application and the GitHub Actions workflows. Provide feedback and suggest improvements if necessary."
89-
"model_name": "gpt4o",
90-
"temperature": 0
91-
}
92-
}
93-
This is the user request message: """ + userMessage
26+
You are a powerful orchestration LLM which gets a user message request and instead of answering the request, you will split the task into different actions which execute different aspects of the question.
27+
You will create the system message for these smaller agents. Your answer to a question should be ONLY the JSON representation of multiple ids (can be short names) and associated system messages for the agents. Only valid JSON, no other markup.
28+
Each agent in the sequence will get the complete result of the agents before with information about the agent itself.
29+
You can choose between "gpt-4o-mini" (cheap, for every day tasks) and "o3-mini" (10x more expensive, for reasoning tasks), with "gpt-4o-mini" being the default. You also have to choose the temperature the agent should use (only for gpt-4o-mini, a double between 0 and 1).
30+
There will be an external process which will sequentially execute your agents and provide the following agents with all information from the previous agents.
31+
Make sure that the final agent merges all relevant information from the steps before, this answer is the final answer to the user.
32+
For example:
33+
user request: "I want to plan an hour lesson for primary school kids to explain, mostly analog, the functionality of LLMs".
34+
Sample output:
35+
{agents: [
36+
{
37+
"name": "ResearchAgent",
38+
"system_message": "You are an agent tasked with researching age-appropriate analogies to explain the functionality of Large Language Models (LLMs) to primary school children. Provide several simple and relatable analogies that can help young students understand how LLMs work.",
39+
"model": "o3-mini"
40+
},
41+
{
42+
"name": "StructureAgent",
43+
"system_message": "You are an agent responsible for designing the structure of a one-hour lesson aimed at explaining the functionality of LLMs to primary school kids. Using the analogies provided by the ResearchAgent, create a detailed lesson outline that includes an introduction, main explanation, interactive activities, and a conclusion.",
44+
"model": "gpt-4o-mini",
45+
"temperature": 0.4
46+
},
47+
{
48+
"name": "MaterialsAgent",
49+
"system_message": "You are an agent in charge of creating teaching materials for the lesson plan developed by the StructureAgent. Develop slides, handouts, and any other necessary materials that effectively utilize the analogies and explanations to help primary school students understand LLMs.",
50+
"model": "gpt-4o-mini",
51+
"temperature": 0.3
52+
},
53+
{
54+
"name": "ActivityAgent",
55+
"system_message": "You are an agent tasked with planning interactive activities to include in the lesson. Based on the lesson structure and materials from the previous agents, design engaging, analog-based activities that reinforce the concepts of how LLMs function.",
56+
"model": "gpt-4o-mini",
57+
"temperature": 0.4
58+
},
59+
{
60+
"name": "Verifier",
61+
"system_message": "You are an agent responsible for reviewing and verifying the entire lesson plan for clarity, age-appropriateness, and effectiveness in explaining the functionality of LLMs to primary school children. Ensure that all components are coherent, all relevant details are mentioned and suggest improvements if necessary. Merge all relevant information from the steps before, your answer is the final answer to the user.",
62+
"model": "o3-mini"
63+
}
64+
}
65+
User Request: "I want to implement a Spring Boot application using gradle/kotlin. How to implement certain actions using GH actions?"
66+
Sample Response:
67+
{agents: [
68+
{
69+
"name": "IdeaAgent-1",
70+
"system_message": "You are an agent tasked with generating ideas for implementing a Spring Boot application using Gradle and Kotlin, as well as integrating GitHub Actions for various actions. Provide suggestions for project setup, Gradle configurations, and GitHub Actions workflows.",
71+
"model": "o3-mini"
72+
},
73+
{
74+
"name": "IdeaAgent-2",
75+
"system_message": "You are an agent tasked with generating ideas for implementing a Spring Boot application using Gradle and Kotlin, as well as integrating GitHub Actions for various actions. Provide suggestions for project setup, Gradle configurations, and GitHub Actions workflows.",
76+
"model": "gpt-4o-mini",
77+
"temperature": 0.8
78+
},
79+
{
80+
"name": "IdeaAgent-3",
81+
"system_message": "You are an agent tasked with generating ideas for implementing a Spring Boot application using Gradle and Kotlin, as well as integrating GitHub Actions for various actions. Provide suggestions for project setup, Gradle configurations, and GitHub Actions workflows.",
82+
"model": "gpt-4o-mini",
83+
"temperature": 0.2
84+
},
85+
{
86+
"name": "Agent-Verifier",
87+
"system_message": "You are an agent responsible for reviewing and verifying the ideas generated by the IdeaAgents. Ensure that the proposed ideas are coherent, feasible, and effectively address the implementation of the Spring Boot application and the GitHub Actions workflows. Merge all relevant information from the steps before, your answer is the final answer to the user.",
88+
"model": "o3-mini"
89+
}
90+
}
91+
This is the user request message: """ + userMessage
9492
)).build()))
9593
)
96-
.model(ChatModel.O1_PREVIEW)
94+
.model("o3-mini")
9795
.build();
9896
OpenAIClient client = OpenAIOkHttpClient.builder()
9997
.apiKey(openAIKey).build();

src/main/java/tech/indus340/managai/chatbot/WorkerAgentConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ public class WorkerAgentConfig {
1414
private String openAIKey;
1515

1616
public ChatLanguageModel workerModel(String modelName, double temp) {
17+
if (temp < 0) {
18+
return OpenAiChatModel.builder()
19+
.apiKey(openAIKey)
20+
.modelName(modelName)
21+
.timeout(ofSeconds(720))
22+
.build();
23+
}
1724
return OpenAiChatModel.builder()
1825
.apiKey(openAIKey)
1926
.modelName(modelName)
2027
.temperature(temp)
21-
.timeout(ofSeconds(360))
28+
.timeout(ofSeconds(720))
2229
.build();
2330
}
2431

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package tech.indus340.managai.controller;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import org.springframework.http.ResponseEntity;
4-
import org.springframework.web.bind.annotation.GetMapping;
5-
import org.springframework.web.bind.annotation.RequestParam;
6-
import org.springframework.web.bind.annotation.RestController;
5+
import org.springframework.web.bind.annotation.*;
76
import tech.indus340.managai.agentflow.AgentFlowEngine;
7+
import tech.indus340.managai.agentflow.dto.ResultResponse;
88

99
@RestController
1010
public class ManagaiController {
@@ -16,7 +16,12 @@ public ManagaiController(AgentFlowEngine agentFlowEngine) {
1616
}
1717

1818
@GetMapping("/managai")
19-
public ResponseEntity<String> queryMcp(@RequestParam("message") String message) {
19+
public ResponseEntity<ResultResponse> queryMcp(@RequestParam("message") String message) throws JsonProcessingException {
2020
return ResponseEntity.ofNullable(agentFlowEngine.agentFlow(message));
2121
}
22+
23+
@PostMapping("/managai")
24+
public ResponseEntity<String> executeMcp(@RequestBody ResultResponse processFlow) {
25+
return ResponseEntity.ofNullable(agentFlowEngine.executeFlow(processFlow));
26+
}
2227
}

0 commit comments

Comments
 (0)