Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ public static AgentTaskConfigurer loop(Predicate<AgenticScope> exitCondition, Ob
return list -> list.loop(l -> l.subAgents(agents).exitCondition(exitCondition));
}

public static AgentTaskConfigurer loop(
Predicate<AgenticScope> exitCondition, int maxIterations, Object... agents) {
return list ->
list.loop(
l -> l.subAgents(agents).exitCondition(exitCondition).maxIterations(maxIterations));
}

public static AgentTaskConfigurer parallel(Object... agents) {
return list -> list.parallel(agents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package io.serverlessworkflow.fluent.agentic;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agentic.Agent;
import dev.langchain4j.agentic.internal.AgentSpecification;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;
import java.util.List;
Expand Down Expand Up @@ -234,4 +236,143 @@ String draftNew(
@V("allowedDomains") List<String> allowedDomains,
@V("links") List<String> links);
}

interface CreativeWriter {

@UserMessage(
"""
You are a creative writer.
Generate a draft of a story no more than
3 sentences long around the given topic.
Return only the story and nothing else.
The topic is {{topic}}.
""")
@Agent("Generates a story based on the given topic")
String generateStory(@V("topic") String topic);
}

interface AudienceEditor {

@UserMessage(
"""
You are a professional editor.
Analyze and rewrite the following story to better align
with the target audience of {{audience}}.
Return only the story and nothing else.
The story is "{{story}}".
""")
@Agent("Edits a story to better fit a given audience")
String editStory(@V("story") String story, @V("audience") String audience);
}

interface StyleEditor {

@UserMessage(
"""
You are a professional editor.
Analyze and rewrite the following story to better fit and be more coherent with the {{style}} style.
Return only the story and nothing else.
The story is "{{story}}".
""")
@Agent("Edits a story to better fit a given style")
String editStory(@V("story") String story, @V("style") String style);
}

interface StyleScorer {

@UserMessage(
"""
You are a critical reviewer.
Give a review score between 0.0 and 1.0 for the following
story based on how well it aligns with the style '{{style}}'.
Return only the score and nothing else.

The story is: "{{story}}"
""")
@Agent("Scores a story based on how well it aligns with a given style")
double scoreStyle(@V("story") String story, @V("style") String style);
}

interface FoodExpert {

@UserMessage(
"""
You are a great evening planner.
Propose a list of 3 meals matching the given mood.
The mood is {{mood}}.
For each meal, just give the name of the meal.
Provide a list with the 3 items and nothing else.
""")
@Agent
List<String> findMeal(@V("mood") String mood);
}

interface AstrologyAgent {
@SystemMessage(
"""
You are an astrologist that generates horoscopes based on the user's name and zodiac sign.
""")
@UserMessage(
"""
Generate the horoscope for {{name}} who is a {{sign}}.
""")
@Agent("An astrologist that generates horoscopes based on the user's name and zodiac sign.")
String horoscope(@V("name") String name, @V("sign") String sign);
}

enum RequestCategory {
LEGAL,
MEDICAL,
TECHNICAL,
UNKNOWN
}

interface CategoryRouter {

@UserMessage(
"""
Analyze the following user request and categorize it as 'legal', 'medical' or 'technical'.
In case the request doesn't belong to any of those categories categorize it as 'unknown'.
Reply with only one of those words and nothing else.
The user request is: '{{request}}'.
""")
@Agent("Categorizes a user request")
RequestCategory classify(@V("request") String request);
}

interface MedicalExpert {

@dev.langchain4j.service.UserMessage(
"""
You are a medical expert.
Analyze the following user request under a medical point of view and provide the best possible answer.
The user request is {{it}}.
""")
@Tool("A medical expert")
String medicalRequest(String request);
}

interface LegalExpert {

@dev.langchain4j.service.UserMessage(
"""
You are a legal expert.
Analyze the following user request under a legal point of view and provide the best possible answer.
The user request is {{it}}.
""")
@Tool("A legal expert")
String legalRequest(String request);
}

interface TechnicalExpert {

@dev.langchain4j.service.UserMessage(
"""
You are a technical expert.
Analyze the following user request under a technical point of view and provide the best possible answer.
The user request is {{it}}.
""")
@Tool("A technical expert")
String technicalRequest(String request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,84 @@ public static Agents.MovieExpert newMovieExpert() {
.chatModel(BASE_MODEL)
.build());
}

public static Agents.CreativeWriter newCreativeWriter() {
return spy(
AgenticServices.agentBuilder(Agents.CreativeWriter.class)
.outputName("story")
.chatModel(BASE_MODEL)
.build());
}

public static Agents.AudienceEditor newAudienceEditor() {
return spy(
AgenticServices.agentBuilder(Agents.AudienceEditor.class)
.outputName("story")
.chatModel(BASE_MODEL)
.build());
}

public static Agents.StyleEditor newStyleEditor() {
return spy(
AgenticServices.agentBuilder(Agents.StyleEditor.class)
.outputName("story")
.chatModel(BASE_MODEL)
.build());
}

public static Agents.StyleScorer newStyleScorer() {
return spy(
AgenticServices.agentBuilder(Agents.StyleScorer.class)
.outputName("score")
.chatModel(BASE_MODEL)
.build());
}

public static Agents.FoodExpert newFoodExpert() {
return spy(
AgenticServices.agentBuilder(Agents.FoodExpert.class)
.chatModel(BASE_MODEL)
.outputName("meals")
.build());
}

public static Agents.AstrologyAgent newAstrologyAgent() {
return spy(
AgenticServices.agentBuilder(Agents.AstrologyAgent.class)
.chatModel(BASE_MODEL)
.outputName("horoscope")
.build());
}

public static Agents.CategoryRouter newCategoryRouter() {
return spy(
AgenticServices.agentBuilder(Agents.CategoryRouter.class)
.chatModel(BASE_MODEL)
.outputName("category")
.build());
}

public static Agents.MedicalExpert newMedicalExpert() {
return spy(
AgenticServices.agentBuilder(Agents.MedicalExpert.class)
.chatModel(BASE_MODEL)
.outputName("response")
.build());
}

public static Agents.TechnicalExpert newTechnicalExpert() {
return spy(
AgenticServices.agentBuilder(Agents.TechnicalExpert.class)
.chatModel(BASE_MODEL)
.outputName("response")
.build());
}

public static Agents.LegalExpert newLegalExpert() {
return spy(
AgenticServices.agentBuilder(Agents.LegalExpert.class)
.chatModel(BASE_MODEL)
.outputName("response")
.build());
}
}
Loading