news 2026/4/28 1:37:59

boot整合AgentScope智能体

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
boot整合AgentScope智能体
<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.13</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.testaidemo</groupId><artifactId>testAgentScopeDemo</artifactId><version>0.0.1-SNAPSHOT</version><name>testAgentScopeDemo</name><description>testAgentScopeDemo</description><properties><java.version>17</java.version><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.18.0</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.5.0</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.20.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.47</version></dependency><dependency><groupId>io.agentscope</groupId><artifactId>agentscope</artifactId><version>1.0.9</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><executions><execution><id>default-compile</id><phase>compile</phase><goals><goal>compile</goal></goals><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></execution><execution><id>default-testCompile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></execution></executions></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>server: port:8008spring: application: name: testAgentScopeDemo package com.testaidemo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class TestAgentScopeDemoApplication{public static void main(String[]args){SpringApplication.run(TestAgentScopeDemoApplication.class, args);}}package com.testaidemo.dto;importlombok.Data;@Data public class JudgeResult{private Boolean finished;private String correctAnswer;}/** * 定义模型 */ package com.testaidemo.commont;importio.agentscope.core.formatter.dashscope.DashScopeMultiAgentFormatter;importio.agentscope.core.model.DashScopeChatModel;public class ModelFactory{/** * 单智能体 * @return */ public static DashScopeChatModelcreateQwenModel(){String apiKey=System.getenv("DASHSCOPE_API_KEY");if(apiKey==null||apiKey.isBlank()){throw new IllegalStateException("DASHSCOPE_API_KEY is required");}returnDashScopeChatModel.builder().apiKey(apiKey).modelName("qwen-plus").build();}/** * 多智能体 * @return */ public static DashScopeChatModelcreateMultiQwenModel(){String apiKey=System.getenv("DASHSCOPE_API_KEY");if(apiKey==null||apiKey.isBlank()){throw new IllegalStateException("DASHSCOPE_API_KEY is required");}returnDashScopeChatModel.builder().apiKey(apiKey).modelName("qwen3-max").formatter(new DashScopeMultiAgentFormatter()).build();}}package com.testaidemo.commont;importio.agentscope.core.message.ToolResultBlock;importio.agentscope.core.tool.Tool;importio.agentscope.core.tool.ToolEmitter;importio.agentscope.core.tool.ToolParam;/** * 调用工具 */ public class WeatherService{@Tool(description="查看城市天气")public String getWeather(@ToolParam(name="city", description="City name")String city){returncity +" weather: Sunny, 25°C";}@Tool(description="2个数子相加")public int add(@ToolParam(name="a", description="First number")int a, @ToolParam(name="b", description="Second number")int b){returna + b;}@Tool(description="生成数据")public ToolResultBlock generate(@ToolParam(name="count")int count, ToolEmitter emitter){for(int i=0;i<count;i++){emitter.emit(ToolResultBlock.text("Progress "+ i));}returnToolResultBlock.text("Completed");}}package com.testaidemo.controller;importcom.testaidemo.commont.ModelFactory;importcom.testaidemo.commont.WeatherService;importcom.testaidemo.dto.JudgeResult;importio.agentscope.core.ReActAgent;importio.agentscope.core.memory.InMemoryMemory;importio.agentscope.core.message.Msg;importio.agentscope.core.message.MsgRole;importio.agentscope.core.message.TextBlock;importio.agentscope.core.model.DashScopeChatModel;importio.agentscope.core.model.GenerateOptions;importio.agentscope.core.pipeline.MsgHub;importio.agentscope.core.tool.Toolkit;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importreactor.core.publisher.Flux;importjava.util.List;/** * 测试接口 */ @RestController @RequestMapping("/ai")public class ChatController{/** * http://127.0.0.1:8008/ai/sendReActAgent * 简单单智能体 * @return */ @GetMapping("/sendReActAgent")public StringsendReActAgent(){DashScopeChatModel model=ModelFactory.createQwenModel();ReActAgent agent=ReActAgent.builder().name("Nexus").sysPrompt("You are a helpful enterprise assistant.").model(model).memory(new InMemoryMemory()).build();Msg response=agent.call(Msg.builder().textContent("你好,请介绍一下 AgentScope 是什么").build()).block();if(response!=null){returnresponse.getTextContent();}else{returnnull;}}/** * http://127.0.0.1:8008/ai/sendReActAgentStream * 简单智能体:流输出(有点问题) * @return */ @GetMapping("/sendReActAgentStream")public Flux<String>sendReActAgentStream(String prompt){DashScopeChatModel model=ModelFactory.createQwenModel();GenerateOptions options=GenerateOptions.builder().stream(Boolean.TRUE).build();returnmodel.stream(List.of(Msg.builder().textContent(prompt).build()), List.of(), options).map(chatResponse ->chatResponse.getContent().stream().filter(TextBlock.class::isInstance).map(TextBlock.class::cast).map(TextBlock::getText).reduce("", String::concat)).filter(text ->text!=null&&!text.isBlank());}/** * * http://127.0.0.1:8008/ai/sendMultiReActAgent * 多智能体 */ @GetMapping("/sendMultiReActAgent")public voidsendMultiReActAgent(){DashScopeChatModel model=ModelFactory.createMultiQwenModel();// Topic String topic=""" The two circles are externally tangent and there is no relative sliding. The radius of circle A is1/3 the radius of circle B. Circle A rolls around circle B one trip back to its starting point. How manytimeswill circle A revolveintotal?""";// Create debaters ReActAgent alice=ReActAgent.builder().name("Alice").sysPrompt("You're a debater named Alice. Topic: "+ topic).model(model).memory(new InMemoryMemory()).build();ReActAgent bob=ReActAgent.builder().name("Bob").sysPrompt("You're a debater named Bob. Topic: "+ topic).model(model).memory(new InMemoryMemory()).build();// Create moderator ReActAgent moderator=ReActAgent.builder().name("Moderator").sysPrompt("You're a moderator evaluating a debate on: "+ topic).model(model).memory(new InMemoryMemory()).build();// Run debatefor(int round=1;round<=5;round++){System.out.println("\n=== Round "+ round +" ===\n");try(MsgHub hub=MsgHub.builder().participants(alice, bob, moderator).build()){hub.enter().block();Msg aliceMsg=alice.call(Msg.builder().name("user").role(MsgRole.USER).content(TextBlock.builder().text("Present your argument.").build()).build()).block();System.out.println("Alice: "+ aliceMsg.getTextContent());Msg bobMsg=bob.call(Msg.builder().name("user").role(MsgRole.USER).content(TextBlock.builder().text("Respond to Alice and present your argument.").build()).build()).block();System.out.println("Bob: "+ bobMsg.getTextContent());}// Moderator judgment Msg judgeMsg=moderator.call(Msg.builder().name("user").role(MsgRole.USER).content(TextBlock.builder().text("Evaluate the debate. Is there a correct answer?").build()).build(), JudgeResult.class).block();JudgeResult result=judgeMsg.getStructuredData(JudgeResult.class);if(result.getFinished()){System.out.println("\n=== Debate Concluded ===");System.out.println("Answer: "+ result.getCorrectAnswer());break;}}}/** * http://127.0.0.1:8008/ai/sendReActAgentTool * 测试调用工具 * @return */ @GetMapping("/sendReActAgentTool")public StringsendReActAgentTool(){DashScopeChatModel model=ModelFactory.createQwenModel();Toolkit toolkit=new Toolkit();toolkit.registerTool(new WeatherService());ReActAgent agent=ReActAgent.builder().name("Assistant").model(model).toolkit(toolkit).build();Msg response=agent.call(Msg.builder().textContent("你好,查看一下苏州天气").build()).block();if(response!=null){returnresponse.getTextContent();}else{returnnull;}}}备注: AgentScope-Java版 Github地址:https://github.com/agentscope-ai/agentscope-java 帮助文档:https://java.agentscope.io/en/intro.html
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/28 1:37:03

AMO-Bench:高中数学竞赛大语言模型评估体系构建

1. 项目背景与核心价值AMO-Bench这个项目名中的"AMO"实际上暗指了American Mathematics Olympiad&#xff08;美国数学奥林匹克竞赛&#xff09;的缩写&#xff0c;而"Bench"则代表了基准测试。这个命名方式巧妙地揭示了项目的核心目标——构建一个专门针对…

作者头像 李华
网站建设 2026/4/28 1:36:29

激活引导技术:实时调控大模型行为的创新方法

1. 项目概述最近在AI控制领域出现了一个有趣的新方向——激活引导&#xff08;Activation Steering&#xff09;。这个技术让我想起了早期神经网络研究中那些试图理解中间层表征的探索&#xff0c;但它的目标更加实用&#xff1a;通过直接干预模型内部激活值来引导输出行为。我…

作者头像 李华
网站建设 2026/4/28 1:34:22

PHP V6 单商户常见问题——小程序接口申请

小程序接口申请问题现象很多小伙伴微信小程序位置信息申请无法通过&#xff0c;其中注意点为&#xff1a;解决方案1. 开通接口登录微信小程序平台&#xff0c;找到开发管理下的&#xff0c;接口设置&#xff0c;开通wx.getLocation&#xff0c;wx.chooseLocation2. 申请接口理由…

作者头像 李华
网站建设 2026/4/28 1:25:44

告别“盲盒”时代:Gitee CodePecker重塑开源供应链安全底座

在现代软件开发浪潮中&#xff0c;开源组件已经成为构建应用架构的绝对基石。不可否认&#xff0c;当前超过九成的企业在其IT底层系统中深度依赖开源组件&#xff0c;然而&#xff0c;随之而来的安全隐患同样不容小觑。进一步而言&#xff0c;当高达七成的安全漏洞直接溯源于开…

作者头像 李华
网站建设 2026/4/28 1:24:56

为什么栈不需要垃圾回收,堆需要垃圾回收?一文详解

目录 一.栈内存 你的办公桌&#xff08;用完即走&#xff09; 二.堆内存 公共大仓库&#xff08;需要管理员&#xff09; 核心区别在哪里&#xff1f; 总结 大白话 一.栈内存 你的办公桌&#xff08;用完即走&#xff09; 想象你在办公桌上工作&#xff1a; 动作&#…

作者头像 李华
网站建设 2026/4/28 1:24:53

基于Topcoder MCP与Hugging Face构建AI Agent实践指南

1. 项目概述 这个项目标题包含了几个关键信息点&#xff1a;AI学习、AI Agent构建与部署、Topcoder MCP平台以及Hugging Face。简单来说&#xff0c;这是一个关于如何利用Topcoder MCP平台在Hugging Face上构建和部署AI Agent的实践指南。 作为一名长期从事AI开发的工程师&…

作者头像 李华