Skip to content

Commit b291a2d

Browse files
committed
code refactoring
1 parent 421d308 commit b291a2d

File tree

12 files changed

+161
-140
lines changed

12 files changed

+161
-140
lines changed

chain-workflow/src/main/java/com/javaaidev/agenticpatterns/chainworkflow/ChainWorkflowAgent.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.javaaidev.agenticpatterns.chainworkflow;
22

3-
import com.javaaidev.agenticpatterns.taskexecution.TaskExecutionAgent;
3+
import com.javaaidev.agenticpatterns.taskexecution.NoLLMTaskExecutionAgent;
44
import io.micrometer.observation.ObservationRegistry;
55
import java.lang.reflect.Type;
66
import java.util.concurrent.CopyOnWriteArrayList;
77
import org.jspecify.annotations.Nullable;
8-
import org.springframework.ai.chat.client.ChatClient;
98

109
/**
1110
* Chain Workflow agent, refer to <a
@@ -15,21 +14,19 @@
1514
* @param <Response> Type of agent output
1615
*/
1716
public class ChainWorkflowAgent<Request, Response> extends
18-
TaskExecutionAgent<Request, Response> {
17+
NoLLMTaskExecutionAgent<Request, Response> {
1918

2019
private final CopyOnWriteArrayList<ChainStepAgent<Request, Response>> stepAgents = new CopyOnWriteArrayList<>();
2120

2221
protected ChainWorkflowAgent(
23-
ChatClient chatClient,
2422
@Nullable ObservationRegistry observationRegistry) {
25-
super(chatClient, null, observationRegistry);
23+
super(null, observationRegistry);
2624
}
2725

2826
public ChainWorkflowAgent(
29-
ChatClient chatClient,
3027
@Nullable Type responseType,
3128
@Nullable ObservationRegistry observationRegistry) {
32-
super(chatClient, responseType, observationRegistry);
29+
super(responseType, observationRegistry);
3330
}
3431

3532
/**
@@ -50,9 +47,4 @@ private Response doCall(@Nullable Request request) {
5047
var chain = new WorkflowChain<>(stepAgents);
5148
return chain.callNext(request, null);
5249
}
53-
54-
@Override
55-
protected String getPromptTemplate() {
56-
return "";
57-
}
5850
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.javaaidev.agenticpatterns.examples.chainworkflow;
2+
3+
import com.javaaidev.agenticpatterns.examples.chainworkflow.ArticleWritingAgent.ArticleWritingRequest;
4+
import com.javaaidev.agenticpatterns.examples.chainworkflow.ArticleWritingAgent.ArticleWritingResponse;
5+
import com.javaaidev.agenticpatterns.taskexecution.TaskExecutionAgent;
6+
import io.micrometer.observation.ObservationRegistry;
7+
import org.jspecify.annotations.Nullable;
8+
import org.springframework.ai.chat.client.ChatClient;
9+
10+
class ArticleGenerationAgent extends
11+
TaskExecutionAgent<ArticleWritingRequest, ArticleWritingResponse> {
12+
13+
protected ArticleGenerationAgent(ChatClient chatClient,
14+
@Nullable ObservationRegistry observationRegistry) {
15+
super(chatClient, ArticleWritingResponse.class, observationRegistry);
16+
}
17+
18+
@Override
19+
protected String getPromptTemplate() {
20+
return """
21+
Write an article about {topic}
22+
""";
23+
}
24+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.javaaidev.agenticpatterns.examples.chainworkflow;
2+
3+
import com.javaaidev.agenticpatterns.chainworkflow.ChainStepAgent;
4+
import com.javaaidev.agenticpatterns.chainworkflow.WorkflowChain;
5+
import com.javaaidev.agenticpatterns.core.AgentUtils;
6+
import io.micrometer.observation.ObservationRegistry;
7+
import java.util.Map;
8+
import org.jspecify.annotations.Nullable;
9+
import org.springframework.ai.chat.client.ChatClient;
10+
11+
class ArticleImprovementAgent extends
12+
ChainStepAgent<ArticleImprovementRequest, ArticleImprovementResponse> {
13+
14+
private final String instruction;
15+
private final int order;
16+
17+
protected ArticleImprovementAgent(ChatClient chatClient,
18+
@Nullable ObservationRegistry observationRegistry, String instruction, int order) {
19+
super(chatClient, ArticleImprovementResponse.class, observationRegistry);
20+
this.instruction = instruction;
21+
this.order = order;
22+
}
23+
24+
@Override
25+
protected String getPromptTemplate() {
26+
return """
27+
Goal: Improve an article by following the instruction:
28+
29+
{instruction}
30+
31+
Article content:
32+
{article}
33+
""";
34+
}
35+
36+
@Override
37+
protected ArticleImprovementResponse call(ArticleImprovementRequest request,
38+
Map<String, Object> context,
39+
WorkflowChain<ArticleImprovementRequest, ArticleImprovementResponse> chain) {
40+
var response = this.call(request);
41+
return chain.callNext(new ArticleImprovementRequest(response.article()), response);
42+
}
43+
44+
@Override
45+
protected Map<String, Object> getPromptContext(
46+
@Nullable ArticleImprovementRequest request) {
47+
return Map.of(
48+
"instruction", instruction,
49+
"article",
50+
AgentUtils.safeGet(request, ArticleImprovementRequest::article, "")
51+
);
52+
}
53+
54+
@Override
55+
public int getOrder() {
56+
return order;
57+
}
58+
59+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.javaaidev.agenticpatterns.examples.chainworkflow;
2+
3+
import com.javaaidev.agenticpatterns.chainworkflow.ChainWorkflowAgent;
4+
import io.micrometer.observation.ObservationRegistry;
5+
import java.util.List;
6+
import org.jspecify.annotations.Nullable;
7+
import org.springframework.ai.chat.client.ChatClient;
8+
9+
class ArticleImprovementChainAgent extends
10+
ChainWorkflowAgent<ArticleImprovementRequest, ArticleImprovementResponse> {
11+
12+
private final ChatClient chatClient;
13+
14+
protected ArticleImprovementChainAgent(
15+
ChatClient chatClient,
16+
@Nullable ObservationRegistry observationRegistry) {
17+
super(ArticleImprovementResponse.class, observationRegistry);
18+
this.chatClient = chatClient;
19+
initStepAgents();
20+
}
21+
22+
private void initStepAgents() {
23+
var instructions = List.of(
24+
"""
25+
Review the Structure
26+
- Ensure the article has a clear introduction, body, and conclusion.
27+
- Check if ideas flow logically from one section to another.
28+
- Ensure paragraphs are well-organized and each one has a clear purpose.
29+
""",
30+
"""
31+
Improve Clarity and Conciseness
32+
- Remove unnecessary words and redundant phrases.
33+
- Simplify complex sentences for better readability.
34+
- Use active voice where possible.
35+
""",
36+
"""
37+
Enhance Readability
38+
- Break long paragraphs into shorter ones.
39+
- Use bullet points or subheadings for easier scanning.
40+
- Vary sentence length to maintain reader interest.
41+
"""
42+
);
43+
for (int i = 0; i < instructions.size(); i++) {
44+
addStep(
45+
new ArticleImprovementAgent(chatClient, observationRegistry, instructions.get(i), i));
46+
}
47+
}
48+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.javaaidev.agenticpatterns.examples.chainworkflow;
2+
3+
record ArticleImprovementRequest(String article) {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.javaaidev.agenticpatterns.examples.chainworkflow;
2+
3+
record ArticleImprovementResponse(String article) {
4+
5+
}
Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package com.javaaidev.agenticpatterns.examples.chainworkflow;
22

3-
import com.javaaidev.agenticpatterns.chainworkflow.ChainStepAgent;
4-
import com.javaaidev.agenticpatterns.chainworkflow.ChainWorkflowAgent;
5-
import com.javaaidev.agenticpatterns.chainworkflow.WorkflowChain;
6-
import com.javaaidev.agenticpatterns.core.AgentUtils;
73
import com.javaaidev.agenticpatterns.examples.chainworkflow.ArticleWritingAgent.ArticleWritingRequest;
84
import com.javaaidev.agenticpatterns.examples.chainworkflow.ArticleWritingAgent.ArticleWritingResponse;
95
import com.javaaidev.agenticpatterns.taskexecution.TaskExecutionAgent;
106
import io.micrometer.observation.ObservationRegistry;
11-
import java.util.List;
12-
import java.util.Map;
137
import org.jspecify.annotations.Nullable;
148
import org.springframework.ai.chat.client.ChatClient;
159

@@ -48,123 +42,5 @@ public record ArticleWritingResponse(String article) {
4842

4943
}
5044

51-
private record ArticleImprovementRequest(String article) {
5245

53-
}
54-
55-
private record ArticleImprovementResponse(String article) {
56-
57-
}
58-
59-
private static class ArticleGenerationAgent extends
60-
TaskExecutionAgent<ArticleWritingRequest, ArticleWritingResponse> {
61-
62-
protected ArticleGenerationAgent(ChatClient chatClient,
63-
@Nullable ObservationRegistry observationRegistry) {
64-
super(chatClient, ArticleWritingResponse.class, observationRegistry);
65-
}
66-
67-
@Override
68-
protected String getPromptTemplate() {
69-
return """
70-
Write an article about {topic}
71-
""";
72-
}
73-
74-
@Override
75-
protected Map<String, Object> getPromptContext(
76-
@Nullable ArticleWritingRequest request) {
77-
return Map.of(
78-
"topic", AgentUtils.safeGet(request, ArticleWritingRequest::topic, "")
79-
);
80-
}
81-
}
82-
83-
private static class ArticleImprovementChainAgent extends
84-
ChainWorkflowAgent<ArticleImprovementRequest, ArticleImprovementResponse> {
85-
86-
protected ArticleImprovementChainAgent(
87-
ChatClient chatClient,
88-
@Nullable ObservationRegistry observationRegistry) {
89-
super(chatClient, ArticleImprovementResponse.class, observationRegistry);
90-
initStepAgents();
91-
}
92-
93-
private void initStepAgents() {
94-
var instructions = List.of(
95-
"""
96-
Review the Structure
97-
- Ensure the article has a clear introduction, body, and conclusion.
98-
- Check if ideas flow logically from one section to another.
99-
- Ensure paragraphs are well-organized and each one has a clear purpose.
100-
""",
101-
"""
102-
Improve Clarity and Conciseness
103-
- Remove unnecessary words and redundant phrases.
104-
- Simplify complex sentences for better readability.
105-
- Use active voice where possible.
106-
""",
107-
"""
108-
Enhance Readability
109-
- Break long paragraphs into shorter ones.
110-
- Use bullet points or subheadings for easier scanning.
111-
- Vary sentence length to maintain reader interest.
112-
"""
113-
);
114-
for (int i = 0; i < instructions.size(); i++) {
115-
addStep(
116-
new ArticleImprovementAgent(chatClient, observationRegistry, instructions.get(i), i));
117-
}
118-
}
119-
}
120-
121-
122-
private static class ArticleImprovementAgent extends
123-
ChainStepAgent<ArticleImprovementRequest, ArticleImprovementResponse> {
124-
125-
private final String instruction;
126-
private final int order;
127-
128-
protected ArticleImprovementAgent(ChatClient chatClient,
129-
@Nullable ObservationRegistry observationRegistry, String instruction, int order) {
130-
super(chatClient, ArticleImprovementResponse.class, observationRegistry);
131-
this.instruction = instruction;
132-
this.order = order;
133-
}
134-
135-
@Override
136-
protected String getPromptTemplate() {
137-
return """
138-
Goal: Improve an article by following the instruction:
139-
140-
{instruction}
141-
142-
Article content:
143-
{article}
144-
""";
145-
}
146-
147-
@Override
148-
protected ArticleImprovementResponse call(ArticleImprovementRequest request,
149-
Map<String, Object> context,
150-
WorkflowChain<ArticleImprovementRequest, ArticleImprovementResponse> chain) {
151-
var response = this.call(request);
152-
return chain.callNext(new ArticleImprovementRequest(response.article()), response);
153-
}
154-
155-
@Override
156-
protected Map<String, Object> getPromptContext(
157-
@Nullable ArticleImprovementRequest request) {
158-
return Map.of(
159-
"instruction", instruction,
160-
"article",
161-
AgentUtils.safeGet(request, ArticleImprovementRequest::article, "")
162-
);
163-
}
164-
165-
@Override
166-
public int getOrder() {
167-
return order;
168-
}
169-
}
17046
}

examples/src/main/java/com/javaaidev/agenticpatterns/examples/parallelizationworkflow/AlgorithmArticleGenerationAgent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ protected String getPromptTemplate() {
3939
@Override
4040
protected @Nullable Map<String, Object> getRequestPromptContext(
4141
@Nullable AlgorithmArticleGenerationRequest request) {
42-
return AgentUtils.objectToMap(request);
42+
return Map.of("algorithm",
43+
AgentUtils.safeGet(request, AlgorithmArticleGenerationRequest::algorithm, ""));
4344
}
4445

4546
@Override

examples/src/main/resources/application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spring:
77
api-key: ${OPENAI_API_KEY:}
88
chat:
99
options:
10-
model: gpt-4o-mini
10+
model: gpt-4o
1111
temperature: 0.0
1212
logging:
1313
level:

parallelization-workflow/src/main/java/com/javaaidev/agenticpatterns/parallelizationworkflow/DirectAssembling.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private Response doCall(@Nullable Request request) {
4343
/**
4444
* Assemble subtask execution results into the response
4545
*
46-
* @param results Results of subtask execution
46+
* @param results Results of subtask execution, see {@linkplain TaskExecutionResults}
4747
* @return Response
4848
*/
4949
protected abstract Response assemble(TaskExecutionResults results);

0 commit comments

Comments
 (0)