langchain4j笔记-智能体系统01

智能体系统

入门使用
java 复制代码
import dev.langchain4j.agentic.Agent;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;

public interface CreativeWriter {

    @UserMessage("""
            你是一位创意作家。
            请围绕给定主题,写一个不超过三句话的故事。
            只输出故事,不要输出其他内容。
            主题是:{{topic}}.
            """)
    @Agent(name = "generateStory",
            value = "根据特定的主题来创作一个故事",
            description = "根据特定的主题来创作一个故事",
            outputKey = "story")
    String generateStory(@V("topic") String topic);
}
java 复制代码
public class AgenticTest extends BaseTest {

    @Test
    void test() {
        CreativeWriter creativeWriter = AgenticServices
                .agentBuilder(CreativeWriter.class)
                .chatModel(BASE_MODEL)
                .outputKey("story")
                .build();
        String content = creativeWriter.generateStory("搞笑");
        System.out.println(content);
    }
}
AgenticScope,存储了Agent流转的数据
Sequential workflow
java 复制代码
public interface StyleEditor {

    @UserMessage("""
            你是一位专业编辑。
            请分析并改写以下故事,使其更符合"{{style}}"风格,并与之更加连贯一致。
            只返回故事内容,不要返回其他任何内容。
            故事内容为:"{{story}}"。
            """)
    @Agent("编辑故事以更好地适配特定风格")
    String editStory(@V("story") String story, @V("style") String style);
}
public interface AudienceEditor {

    @UserMessage("""
            你是一位专业编辑。
            请分析并改写以下故事,使其更符合目标受众"{{audience}}"的需求。
            只返回故事内容,不要返回其他任何内容。
            故事内容为:"{{story}}"。
            """)
    @Agent("编辑故事以更好地适配特定受众")
    String editStory(@V("story") String story, @V("audience") String audience);
}
java 复制代码
public class AgenticTest extends BaseTest {

    @Test
    void test() {


        CreativeWriter creativeWriter = AgenticServices
                .agentBuilder(CreativeWriter.class)
                .chatModel(BASE_MODEL)
                .outputKey("story")
                .build();

        AudienceEditor audienceEditor = AgenticServices
                .agentBuilder(AudienceEditor.class)
                .chatModel(BASE_MODEL)
                .outputKey("story")
                .build();

        StyleEditor styleEditor = AgenticServices
                .agentBuilder(StyleEditor.class)
                .chatModel(BASE_MODEL)
                .outputKey("story")
                .build();

        UntypedAgent novelCreator = AgenticServices
                .sequenceBuilder()
                .subAgents(creativeWriter, audienceEditor, styleEditor)
                .outputKey("story")
                .build();

        Map<String, Object> input = Map.of(
                "topic", "龙与巫师",
                "style", "奇幻",
                "audience", "年轻人和成年人"
        );

        String story = (String) novelCreator.invoke(input);

        System.out.println(story);
    }
}

单个代理也可以用UntypedAgent来定义

java 复制代码
UntypedAgent creativeWriter = AgenticServices.agentBuilder()
        .chatModel(BASE_MODEL)
        .description("根据特定的主题来创作一个故事")
        .userMessage("""
                你是一位创意作家。
                请围绕给定主题,写一个不超过三句话的故事。
                只输出故事,不要输出其他内容。
                主题是:{{topic}}.
                """)
        .inputKey(String.class, "topic")
        .returnType(String.class) 
        .outputKey("story")
        .build();

可以将 UntypedAgent 接口替换为一个更具体的接口

java 复制代码
public interface NovelCreator {
    @Agent
    String createNovel(@V("topic") String topic, @V("audience") String audience, @V("style") String style);
}
java 复制代码
NovelCreator novelCreator = AgenticServices
                .sequenceBuilder(NovelCreator.class)
                .subAgents(creativeWriter, audienceEditor, styleEditor)
                .outputKey("story")
                .build();

String story = novelCreator.createNovel("龙与巫师", "年轻人", "奇幻");
System.out.println(story);
循环
java 复制代码
public interface StyleScorer {

    @UserMessage("""
            你是一位评论家。请根据故事与"{{style}}"风格的契合程度,为以下故事打出 0.0 到 1.0 之间的评分。
            只返回评分,不要输出其他任何内容。故事内容为:"{{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);
}
java 复制代码
@Test
void test02() {

    CreativeWriter creativeWriter = AgenticServices
            .agentBuilder(CreativeWriter.class)
            .chatModel(BASE_MODEL)
            .outputKey("story")
            .build();

    StyleEditor styleEditor = AgenticServices
            .agentBuilder(StyleEditor.class)
            .chatModel(BASE_MODEL)
            .outputKey("story")
            .build();

    StyleScorer styleScorer = AgenticServices
            .agentBuilder(StyleScorer.class)
            .chatModel(BASE_MODEL)
            .outputKey("score")
            .build();

    UntypedAgent styleReviewLoop = AgenticServices
            .loopBuilder()
            .subAgents(styleScorer, styleEditor)
            .maxIterations(5)
            .testExitAtLoopEnd(true)
            .exitCondition( (agenticScope, loopCounter) -> {
                double score = agenticScope.readState("score", 0.0);
                return loopCounter <= 3 ? score >= 0.8 : score >= 0.6;
            })
            .build();

    StyledWriter styledWriter = AgenticServices
            .sequenceBuilder(StyledWriter.class)
            .subAgents(creativeWriter, styleReviewLoop)
            .outputKey("story")
            .build();

    String story = styledWriter.writeStoryWithStyle("dragons and wizards", "comedy");
}
相关推荐
Java内核笔记10 小时前
告别十亿美元的错误 : Spring Boot 4 空安全 (JSpecify) 实战
java·spring boot·后端
糖果店的幽灵10 小时前
langgraph的 MessagesState 解读
java·开发语言·人工智能·windows·langgraph
谙弆悕博士11 小时前
系统集成项目管理工程师教程(第3版)笔记——第4章:信息系统架构
笔记·系统架构·项目管理·创业创新·学习方法·业界资讯·物理
极光代码工作室12 小时前
基于SpringBoot的在线博客系统
java·springboot·web开发·后端开发
我是唐青枫12 小时前
Java Spring Security 实战详解:从登录认证到 JWT 权限控制
java·spring
茯苓gao12 小时前
嵌入式开发笔记:Qt信号槽机制深度解析——从原理到实战的全方位指南
开发语言·笔记·嵌入式硬件·qt·学习
ihuyigui12 小时前
海外签收通知短信接口
android·java·开发语言·前端·数据库·后端
海棠Flower未眠12 小时前
SpringBoot 消息死信队列(荣耀典藏版)
java·数据库·spring boot
暖和_白开水13 小时前
数据分析agent (七):contextvars 模块上下文request_id
java·前端·数据分析
weixin_7275356213 小时前
MinIO大文件上传深度拆解:从原理到生产落地的完整指南
java·中间件