Skip to content

Commit 787473d

Browse files
committed
more examples
Signed-off-by: Dmitrii Tikhomirov <[email protected]>
1 parent 6e74f3f commit 787473d

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

experimental/fluent/agentic/src/test/java/io/serverlessworkflow/fluent/agentic/LC4JEquivalenceIT.java

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
package io.serverlessworkflow.fluent.agentic;
1717

1818
import static io.serverlessworkflow.fluent.agentic.AgentWorkflowBuilder.workflow;
19+
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.conditional;
20+
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.doTasks;
21+
import static io.serverlessworkflow.fluent.spec.dsl.DSL.tasks;
1922
import static org.assertj.core.api.Assertions.assertThat;
2023
import static org.junit.jupiter.api.Assertions.assertEquals;
2124

2225
import dev.langchain4j.agentic.AgenticServices;
2326
import dev.langchain4j.agentic.workflow.HumanInTheLoop;
27+
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
2428
import io.serverlessworkflow.api.types.TaskItem;
2529
import io.serverlessworkflow.api.types.Workflow;
2630
import io.serverlessworkflow.api.types.func.CallTaskJava;
@@ -176,17 +180,6 @@ public void parallelWorkflow() {
176180
assertEquals("Fake conflict response", result.get("movies"));
177181
}
178182

179-
// TODO
180-
@Test
181-
@DisplayName("Conditional agents via choice(...)")
182-
public void conditionalWorkflow() {
183-
184-
var category = AgentsUtils.newCategoryRouter();
185-
var a1 = AgentsUtils.newMedicalExpert();
186-
var a2 = AgentsUtils.newTechnicalExpert();
187-
var a3 = AgentsUtils.newLegalExpert();
188-
}
189-
190183
// TODO
191184
@Test
192185
@DisplayName("Error handling with agents")
@@ -220,6 +213,53 @@ public void errorHandling() {
220213
assertThat(result).containsKey("story");
221214
}
222215

216+
@SuppressWarnings("unchecked")
217+
@Test
218+
@DisplayName("Conditional agents via choice(...)")
219+
public void conditionalWorkflow() {
220+
221+
var category = AgentsUtils.newCategoryRouter();
222+
var a1 = AgentsUtils.newMedicalExpert();
223+
var a2 = AgentsUtils.newTechnicalExpert();
224+
var a3 = AgentsUtils.newLegalExpert();
225+
226+
Workflow wf =
227+
workflow("conditional")
228+
.sequence("process", category)
229+
.tasks(
230+
t ->
231+
t.switchCase(
232+
p ->
233+
p.onPredicate(
234+
item ->
235+
item.when(
236+
m ->
237+
"unknown"
238+
.equals(
239+
((Map<String, Object>) m).get("category")))
240+
.then(FlowDirectiveEnum.END))))
241+
.tasks(
242+
doTasks(
243+
conditional(
244+
m -> "medical".equals(((Map<String, Object>) m).get("category")), a1),
245+
conditional(
246+
m -> "technical".equals(((Map<String, Object>) m).get("category")), a2),
247+
conditional(
248+
m -> "legal".equals(((Map<String, Object>) m).get("category")), a3)))
249+
.build();
250+
251+
Map<String, Object> input = Map.of("question", "What is the best treatment for a common cold?");
252+
253+
Map<String, Object> result;
254+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
255+
result = app.workflowDefinition(wf).instance(input).start().get().asMap().orElseThrow();
256+
} catch (Exception e) {
257+
throw new RuntimeException("Workflow execution failed", e);
258+
}
259+
260+
assertThat(result).containsKey("response");
261+
}
262+
223263
@Test
224264
@DisplayName("Human in the loop")
225265
public void humanInTheLoop() {

0 commit comments

Comments
 (0)