Skip to content

Commit 27fd6bd

Browse files
committed
code improvement
1 parent b229da5 commit 27fd6bd

File tree

8 files changed

+75
-23
lines changed

8 files changed

+75
-23
lines changed

chain-workflow/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Chain Workflow Agent
2+
3+
Implementation
4+
of [Chain Workflow pattern](https://javaaidev.com/docs/agentic-patterns/patterns/chain-workflow)
5+
6+
See [doc](https://javaaidev.com/docs/agentic-patterns/reference-implementation/chain-workflow-agent)

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
import org.springframework.ai.chat.client.ChatClient;
99
import org.springframework.core.Ordered;
1010

11-
public abstract class ChainStepAgent<Req, Res> extends TaskExecutionAgent<Req, Res> implements
11+
/**
12+
* A step in the chain
13+
*
14+
* @param <Request> Task input type
15+
* @param <Response> Task output type
16+
*/
17+
public abstract class ChainStepAgent<Request, Response> extends
18+
TaskExecutionAgent<Request, Response> implements
1219
Ordered {
1320

1421
protected ChainStepAgent(ChatClient chatClient,
@@ -22,6 +29,14 @@ protected ChainStepAgent(ChatClient chatClient,
2229
super(chatClient, responseType, observationRegistry);
2330
}
2431

25-
protected abstract Res call(Req request, Map<String, Object> context,
26-
WorkflowChain<Req, Res> chain);
32+
/**
33+
* Call the current step
34+
*
35+
* @param request Task input
36+
* @param context Shared context between different steps
37+
* @param chain The chain, see {@linkplain WorkflowChain}
38+
* @return Task output
39+
*/
40+
protected abstract Response call(Request request, Map<String, Object> context,
41+
WorkflowChain<Request, Response> chain);
2742
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
import com.javaaidev.agenticpatterns.taskexecution.TaskExecutionAgent;
44
import io.micrometer.observation.ObservationRegistry;
55
import java.lang.reflect.Type;
6-
import java.util.ArrayList;
7-
import java.util.List;
6+
import java.util.concurrent.CopyOnWriteArrayList;
87
import org.jspecify.annotations.Nullable;
98
import org.springframework.ai.chat.client.ChatClient;
109

10+
/**
11+
* Chain Workflow agent, refer to <a
12+
* href="https://javaaidev.com/docs/agentic-patterns/patterns/chain-workflow">doc</a>
13+
*
14+
* @param <Request> Task input type
15+
* @param <Response> Task output type
16+
*/
1117
public class ChainWorkflowAgent<Request, Response> extends
1218
TaskExecutionAgent<Request, Response> {
1319

14-
private final List<ChainStepAgent<Request, Response>> stepAgents = new ArrayList<>();
20+
private final CopyOnWriteArrayList<ChainStepAgent<Request, Response>> stepAgents = new CopyOnWriteArrayList<>();
1521

1622
protected ChainWorkflowAgent(
1723
ChatClient chatClient,
@@ -26,6 +32,11 @@ public ChainWorkflowAgent(
2632
super(chatClient, responseType, observationRegistry);
2733
}
2834

35+
/**
36+
* Add a step in the chain
37+
*
38+
* @param stepAgent A step in the chain
39+
*/
2940
public void addStep(ChainStepAgent<Request, Response> stepAgent) {
3041
stepAgents.add(stepAgent);
3142
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
import org.slf4j.LoggerFactory;
1111
import org.springframework.core.OrderComparator;
1212

13+
/**
14+
* Chain to manage steps
15+
*
16+
* @param <Request> Task input type
17+
* @param <Response> Task output type
18+
*/
1319
public class WorkflowChain<Request, Response> {
1420

1521
private final Deque<ChainStepAgent<Request, Response>> agents;
@@ -22,6 +28,13 @@ public WorkflowChain(List<ChainStepAgent<Request, Response>> agents) {
2228
LOGGER.info("Added {} agents to the chain", this.agents.size());
2329
}
2430

31+
/**
32+
* Call next step in the chain
33+
*
34+
* @param request Task input
35+
* @param lastResponse Last task output
36+
* @return Task output
37+
*/
2538
public Response callNext(Request request, @Nullable Response lastResponse) {
2639
if (agents.isEmpty()) {
2740
return lastResponse;

examples/src/main/java/com/javaaidev/agenticpatterns/examples/chainworkflow/ArticleWritingAgent.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ protected String getPromptTemplate() {
7272
}
7373

7474
@Override
75-
protected @Nullable Map<String, Object> getPromptContext(
76-
@Nullable ArticleWritingRequest articleWritingRequest) {
75+
protected Map<String, Object> getPromptContext(
76+
@Nullable ArticleWritingRequest request) {
7777
return Map.of(
78-
"topic", AgentUtils.safeGet(articleWritingRequest, ArticleWritingRequest::topic, "")
78+
"topic", AgentUtils.safeGet(request, ArticleWritingRequest::topic, "")
7979
);
8080
}
8181
}
@@ -153,12 +153,12 @@ protected ArticleImprovementResponse call(ArticleImprovementRequest request,
153153
}
154154

155155
@Override
156-
protected @Nullable Map<String, Object> getPromptContext(
157-
@Nullable ArticleImprovementRequest articleImprovementRequest) {
156+
protected Map<String, Object> getPromptContext(
157+
@Nullable ArticleImprovementRequest request) {
158158
return Map.of(
159159
"instruction", instruction,
160160
"article",
161-
AgentUtils.safeGet(articleImprovementRequest, ArticleImprovementRequest::article, "")
161+
AgentUtils.safeGet(request, ArticleImprovementRequest::article, "")
162162
);
163163
}
164164

examples/src/main/java/com/javaaidev/agenticpatterns/examples/evaluatoroptimizer/CodeGenerationAgent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ protected String getInitialResultPromptTemplate() {
2929

3030
@Override
3131
protected @Nullable Map<String, Object> buildInitialResultPromptContext(
32-
@Nullable CodeGenerationRequest codeGenerationRequest) {
32+
@Nullable CodeGenerationRequest request) {
3333
return Map.of("input",
34-
AgentUtils.safeGet(codeGenerationRequest, CodeGenerationRequest::input, ""));
34+
AgentUtils.safeGet(request, CodeGenerationRequest::input, ""));
3535
}
3636

3737
@Override

examples/src/main/java/com/javaaidev/agenticpatterns/examples/routingworkflow/CustomerSupportAgent.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
import org.springframework.ai.chat.client.ChatClient;
1414
import org.springframework.ai.chat.client.ChatClient.ChatClientRequestSpec;
1515

16+
/**
17+
* A routing workflow agent for customer support
18+
*/
1619
public class CustomerSupportAgent extends
1720
RoutingWorkflowAgent<CustomerSupportRequest, CustomerSupportResponse> {
1821

19-
protected CustomerSupportAgent(ChatClient chatClient,
22+
public CustomerSupportAgent(ChatClient chatClient,
2023
@Nullable Type responseType, @Nullable ObservationRegistry observationRegistry) {
2124
super(chatClient, responseType, observationRegistry);
2225
initRoutes();
2326
}
2427

25-
protected CustomerSupportAgent(ChatClient chatClient,
28+
public CustomerSupportAgent(ChatClient chatClient,
2629
@Nullable ObservationRegistry observationRegistry) {
2730
super(chatClient, observationRegistry);
2831
initRoutes();
@@ -65,10 +68,10 @@ protected String getPromptTemplate() {
6568

6669
@Override
6770
protected @Nullable Map<String, Object> getPromptContext(
68-
@Nullable CustomerSupportRequest customerSupportRequest) {
71+
@Nullable CustomerSupportRequest request) {
6972
return Map.of(
7073
"question",
71-
AgentUtils.safeGet(customerSupportRequest, CustomerSupportRequest::question, "")
74+
AgentUtils.safeGet(request, CustomerSupportRequest::question, "")
7275
);
7376
}
7477

@@ -92,11 +95,11 @@ protected String getPromptTemplate() {
9295
}
9396

9497
@Override
95-
protected @Nullable Map<String, Object> getPromptContext(
96-
@Nullable CustomerSupportRequest customerSupportRequest) {
98+
protected Map<String, Object> getPromptContext(
99+
@Nullable CustomerSupportRequest request) {
97100
return Map.of(
98101
"question",
99-
AgentUtils.safeGet(customerSupportRequest, CustomerSupportRequest::question, "")
102+
AgentUtils.safeGet(request, CustomerSupportRequest::question, "")
100103
);
101104
}
102105

@@ -121,10 +124,10 @@ protected String getPromptTemplate() {
121124

122125
@Override
123126
protected @Nullable Map<String, Object> getPromptContext(
124-
@Nullable CustomerSupportRequest customerSupportRequest) {
127+
@Nullable CustomerSupportRequest request) {
125128
return Map.of(
126129
"question",
127-
AgentUtils.safeGet(customerSupportRequest, CustomerSupportRequest::question, "")
130+
AgentUtils.safeGet(request, CustomerSupportRequest::question, "")
128131
);
129132
}
130133

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.javaaidev.agenticpatterns.routingworkflow;
3+
4+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)