22
22
import static io .serverlessworkflow .fluent .agentic .langchain4j .Agents .CreativeWriter ;
23
23
import static io .serverlessworkflow .fluent .agentic .langchain4j .Agents .StyleEditor ;
24
24
import static io .serverlessworkflow .fluent .agentic .langchain4j .Models .BASE_MODEL ;
25
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
25
26
import static org .junit .jupiter .api .Assertions .assertNotNull ;
26
27
import static org .junit .jupiter .api .Assertions .assertTrue ;
27
28
import static org .mockito .ArgumentMatchers .any ;
32
33
import dev .langchain4j .agentic .UntypedAgent ;
33
34
import dev .langchain4j .agentic .scope .AgenticScope ;
34
35
import dev .langchain4j .agentic .workflow .WorkflowAgentsBuilder ;
36
+ import io .serverlessworkflow .fluent .agentic .AgenticServices ;
37
+ import io .serverlessworkflow .fluent .agentic .AgentsUtils ;
35
38
39
+ import java .util .ArrayList ;
36
40
import java .util .List ;
37
41
import java .util .Map ;
38
42
import java .util .function .Function ;
39
43
import java .util .function .Predicate ;
44
+ import java .util .stream .IntStream ;
40
45
41
- import io .serverlessworkflow .fluent .agentic .AgenticServices ;
42
- import io .serverlessworkflow .fluent .agentic .AgentsUtils ;
43
46
import org .junit .jupiter .api .Test ;
44
47
45
48
public class WorkflowAgentsIT {
@@ -49,38 +52,38 @@ void sequential_agents_tests() {
49
52
WorkflowAgentsBuilder builder = new LC4JWorkflowBuilder ();
50
53
51
54
CreativeWriter creativeWriter =
52
- spy (
53
- dev .langchain4j .agentic .AgenticServices .agentBuilder (CreativeWriter .class )
54
- .chatModel (BASE_MODEL )
55
- .outputName ("story" )
56
- .build ());
55
+ spy (
56
+ dev .langchain4j .agentic .AgenticServices .agentBuilder (CreativeWriter .class )
57
+ .chatModel (BASE_MODEL )
58
+ .outputName ("story" )
59
+ .build ());
57
60
58
61
AudienceEditor audienceEditor =
59
- spy (
60
- dev .langchain4j .agentic .AgenticServices .agentBuilder (AudienceEditor .class )
61
- .chatModel (BASE_MODEL )
62
- .outputName ("story" )
63
- .build ());
62
+ spy (
63
+ dev .langchain4j .agentic .AgenticServices .agentBuilder (AudienceEditor .class )
64
+ .chatModel (BASE_MODEL )
65
+ .outputName ("story" )
66
+ .build ());
64
67
65
68
StyleEditor styleEditor =
66
- spy (
67
- dev .langchain4j .agentic .AgenticServices .agentBuilder (StyleEditor .class )
68
- .chatModel (BASE_MODEL )
69
- .outputName ("story" )
70
- .build ());
69
+ spy (
70
+ dev .langchain4j .agentic .AgenticServices .agentBuilder (StyleEditor .class )
71
+ .chatModel (BASE_MODEL )
72
+ .outputName ("story" )
73
+ .build ());
71
74
72
75
UntypedAgent novelCreator =
73
- builder
74
- .sequenceBuilder ()
75
- .subAgents (creativeWriter , audienceEditor , styleEditor )
76
- .outputName ("story" )
77
- .build ();
76
+ builder
77
+ .sequenceBuilder ()
78
+ .subAgents (creativeWriter , audienceEditor , styleEditor )
79
+ .outputName ("story" )
80
+ .build ();
78
81
79
82
Map <String , Object > input =
80
- Map .of (
81
- "topic" , "dragons and wizards" ,
82
- "style" , "fantasy" ,
83
- "audience" , "young adults" );
83
+ Map .of (
84
+ "topic" , "dragons and wizards" ,
85
+ "style" , "fantasy" ,
86
+ "audience" , "young adults" );
84
87
85
88
String story = (String ) novelCreator .invoke (input );
86
89
System .out .println (story );
@@ -96,10 +99,10 @@ public void sequenceHelperTest() {
96
99
var audienceEditor = AgentsUtils .newAudienceEditor ();
97
100
var styleEditor = AgentsUtils .newStyleEditor ();
98
101
99
- NovelCreator novelCreator = AgenticServices . of ( NovelCreator . class )
100
- . flow ( workflow ( "seqFlow" )
101
- .sequence (creativeWriter , audienceEditor , styleEditor )
102
- ) .build ();
102
+ NovelCreator novelCreator =
103
+ AgenticServices . of ( NovelCreator . class )
104
+ .flow ( workflow ( "seqFlow" ). sequence (creativeWriter , audienceEditor , styleEditor ) )
105
+ .build ();
103
106
104
107
String story = novelCreator .createNovel ("dragons and wizards" , "young adults" , "fantasy" );
105
108
assertNotNull (story );
@@ -110,12 +113,25 @@ public void parallelWorkflow() {
110
113
var foodExpert = AgentsUtils .newFoodExpert ();
111
114
var movieExpert = AgentsUtils .newMovieExpert ();
112
115
113
- EveningPlannerAgent eveningPlannerAgent = AgenticServices .of (EveningPlannerAgent .class )
114
- .flow (workflow ("parallelFlow" )
115
- .parallel (foodExpert , movieExpert )
116
- ).build ();
116
+ Function <Map <String , List <String >>, List <EveningPlan >> planEvening =
117
+ input -> {
118
+ List <String > movies = input .getOrDefault ("findMovie" , List .of ());
119
+ List <String > meals = input .getOrDefault ("findMeal" , List .of ());
120
+ int max = Math .min (movies .size (), meals .size ());
121
+
122
+ return IntStream .range (0 , max )
123
+ .mapToObj (i -> new EveningPlan (movies .get (i ), meals .get (i )))
124
+ .toList ();
125
+ };
126
+
127
+ EveningPlannerAgent eveningPlannerAgent =
128
+ AgenticServices .of (EveningPlannerAgent .class )
129
+ .flow (workflow ("parallelFlow" ).parallel (foodExpert , movieExpert )
130
+ .outputAs (planEvening ))
131
+ .build ();
117
132
List <EveningPlan > result = eveningPlannerAgent .plan ("romantic" );
118
- assertTrue (result .size () > 0 );
133
+ System .out .println (result );
134
+ assertEquals (3 , result .size ());
119
135
}
120
136
121
137
@ Test
@@ -126,11 +142,10 @@ public void loopTest() {
126
142
127
143
Predicate <AgenticScope > until = s -> s .readState ("score" , 0 ).doubleValue () >= 0.8 ;
128
144
129
-
130
- StyledWriter styledWriter = AgenticServices .of (StyledWriter .class )
131
- .flow (workflow ("loopFlow" ).agent (creativeWriter )
132
- .loop (until , scorer , editor )
133
- ).build ();
145
+ StyledWriter styledWriter =
146
+ AgenticServices .of (StyledWriter .class )
147
+ .flow (workflow ("loopFlow" ).agent (creativeWriter ).loop (until , scorer , editor ))
148
+ .build ();
134
149
135
150
String story = styledWriter .writeStoryWithStyle ("dragons and wizards" , "fantasy" );
136
151
assertNotNull (story );
@@ -140,25 +155,26 @@ public void loopTest() {
140
155
public void humanInTheLoop () {
141
156
var astrologyAgent = AgentsUtils .newAstrologyAgent ();
142
157
143
- var askSign = new Function <Map <String , Object >, Map <String , Object >>() {
144
- @ Override
145
- public Map <String , Object > apply (Map <String , Object > holder ) {
146
- System .out .println ("What's your star sign?" );
147
- //var sign = System.console().readLine();
148
- holder .put ("sign" , "piscis" );
149
- return holder ;
150
- }
151
- };
152
-
153
- String result = AgenticServices .of (HoroscopeAgent .class )
154
- .flow (workflow ("humanInTheLoop" )
155
- .tasks (tasks -> tasks .callFn (fn (askSign )))
156
- .agent (astrologyAgent ))
157
- .build ()
158
- .invoke ("My name is Mario. What is my horoscope?" );
158
+ var askSign =
159
+ new Function <Map <String , Object >, Map <String , Object >>() {
160
+ @ Override
161
+ public Map <String , Object > apply (Map <String , Object > holder ) {
162
+ System .out .println ("What's your star sign?" );
163
+ // var sign = System.console().readLine();
164
+ holder .put ("sign" , "piscis" );
165
+ return holder ;
166
+ }
167
+ };
168
+
169
+ String result =
170
+ AgenticServices .of (HoroscopeAgent .class )
171
+ .flow (
172
+ workflow ("humanInTheLoop" )
173
+ .tasks (tasks -> tasks .callFn (fn (askSign )))
174
+ .agent (astrologyAgent ))
175
+ .build ()
176
+ .invoke ("My name is Mario. What is my horoscope?" );
159
177
160
178
assertNotNull (result );
161
-
162
179
}
163
-
164
180
}
0 commit comments