当前位置: 首页 > news >正文

AgentScope Harness

0.环境

0.1 依赖

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.6</version><relativePath/></parent><properties><java.version>17</java.version><agentscope.version>1.1.0-RC2</agentscope.version></properties><dependency><groupId>io.agentscope</groupId><artifactId>agentscope-harness</artifactId><version>${agentscope.version}</version></dependency><dependency><groupId>io.agentscope</groupId><artifactId>agentscope-core</artifactId><version>${agentscope.version}</version></dependency>

0.2 QuickStart

public class QuickstartExample {static final String BASE_URL = "https://api.minimaxi.com/anthropic";static final String MODEL_NAME = "MiniMax-M2.5";public static void main(String[] args) throws Exception {// 1. 准备工作区:第一次运行生成 AGENTS.md,后续运行复用Path workspace = Paths.get(".agentscope/workspace");initWorkspaceIfAbsent(workspace);// 2. 构建模型Model model = AnthropicChatModel.builder().apiKey(API_KEY).modelName(MODEL_NAME).baseUrl(BASE_URL).stream(true).build();// 3. 构建 HarnessAgent:工作区注入、会话持久化、追踪日志默认开启;//    这里显式启用对话压缩// name纬度区别文件HarnessAgent agent = HarnessAgent.builder().name("quickstart-agent").sysPrompt("你是一个帮助用户做笔记的助手。").model(model).workspace(workspace).compaction(CompactionConfig.builder().triggerMessages(30).keepMessages(10).flushBeforeCompact(true)   // 压缩前把事实提取到日流水账.build()).build();// 4. 同一个 RuntimeContext 发起两轮对话//    sessionId 相同 → 第二轮自动从 Session 恢复第一轮的状态RuntimeContext ctx = RuntimeContext.builder().sessionId("demo-session").userId("alice").build();Msg turn1 = agent.call(Msg.builder().role(MsgRole.USER).textContent("我叫天宇,今天准备一个关于 ReAct 的技术分享。").build(),ctx).block();System.out.println("[turn1] " + turn1.getTextContent());Msg turn2 = agent.call(Msg.builder().role(MsgRole.USER).textContent("我叫什么?我今天要干什么?").build(),ctx).block();System.out.println("[turn2] " + turn2.getTextContent());}private static void initWorkspaceIfAbsent(Path workspace) throws Exception {Files.createDirectories(workspace);Path agentsMd = workspace.resolve("AGENTS.md");if (Files.exists(agentsMd)) return;Files.writeString(agentsMd, """# 笔记助手你是一个帮助用户整理笔记和知识的助手。## 行为约定- 主动记录用户提到的关键事实(姓名、计划、偏好等)- 回答用简洁中文,必要时给出要点列表- 对不确定的内容要主动说明,不要臆造""");}
}
  • 工作区生成的文件
    image

2.架构

3.Skill

3.1 workspace自动注入

3.2 显示注入

  • Windows 无 sh 需要 disableShellTool()
public static void main(String[] args) throws Exception {String pythonDir = "D:\\Code\\Python";String currentPath = System.getenv("Path");if (currentPath == null || !currentPath.contains(pythonDir)) {ProcessBuilder pb = new ProcessBuilder();pb.environment().put("Path", pythonDir + ";" + pythonDir + "\\Scripts;" + (currentPath != null ? currentPath : ""));System.out.println("[Env] 已将 Python 加入进程 PATH: " + pythonDir);}Path workspace = Paths.get(".agentscope/workspace").toAbsolutePath();Files.createDirectories(workspace);Model model = AnthropicChatModel.builder().apiKey(API_KEY).modelName(MODEL_NAME).baseUrl(BASE_URL).stream(true).build();Toolkit toolkit = new Toolkit();SkillBox skillBox = new SkillBox(toolkit);Path skillsPath = workspace.resolve("skills");if (Files.exists(skillsPath) && Files.isDirectory(skillsPath)) {AgentSkillRepository skillRepo = new FileSystemSkillRepository(skillsPath);for (AgentSkill skill : skillRepo.getAllSkills()) {skillBox.registerSkill(skill);System.out.println("[Skill] 已注册: " + skill.getName() + " - " + skill.getDescription());}} else {System.out.println("[Skill] workspace/skills/ 目录不存在,跳过技能加载");}ShellCommandTool shellTool = new ShellCommandTool(null,Set.of("python", "py", "pip", "jshell", "java", "javac", "dir", "type", "echo", "where", "cmd"),command -> true);skillBox.codeExecution().workDir(workspace.toString()).withShell(shellTool).withWrite().enable();System.out.println("[CodeExecution] 已启用, workDir=" + skillBox.getCodeExecutionWorkDir());HarnessAgent agent = HarnessAgent.builder().name("demo-agent").sysPrompt("你是一个功能全面的智能助手,能够帮助用户完成各种任务。使用简洁中文回答。当需要执行计算或脚本时,使用 python-runner 技能。当前环境有 Python 3.9 可用,执行命令为 python,脚本后缀为 .py。重要:写脚本请用 write_text_file 工具,执行脚本请用 execute_shell_command 工具,两者共享同一工作目录。").model(model).workspace(workspace).toolkit(toolkit).skillRepository(new FileSystemSkillRepository(skillsPath)).disableShellTool().disableFilesystemTools().compaction(CompactionConfig.builder().triggerMessages(50).keepMessages(20).build()).build();System.out.println("=== HarnessAgent Python Runner Demo ===\n");RuntimeContext ctx = RuntimeContext.builder().sessionId("python-demo-session").userId("demo-user").build();System.out.println("--- 第1轮: 用Python执行计算 ---");Msg msg1 = Msg.builder().role(MsgRole.USER).textContent("执行一下lwx-test").build();Msg reply1 = agent.call(msg1, ctx).block();System.out.println("[助手] " + (reply1 != null ? reply1.getTextContent() : "无响应"));System.out.println("\n=== Demo 完成 ===");}
  • SKILL.md
---
name: lwx-test
description: 当用户指明使用lwx-test SKILL时,才使用该SKILL
---# 功能描述执行 write_log.py
``
python D:\WorkSpace\AI\MyHarness\.agentscope\workspace\skills\lwx-test\write_log.py
``
http://www.jsqmd.com/news/868557/

相关文章:

  • 2026年4月牛市坎推荐,牛市坎,牛市坎有前景吗 - 品牌推荐师
  • CANN内存优化实战:为什么HBM带宽总是第一个打满的
  • RIS辅助MA系统的近场DM设计与优化
  • 2026新疆导线厂家推荐:新疆钢绞线厂家+架空绝缘导线厂家+钢芯铝绞线厂家推荐 - 栗子测评
  • AXI总线协议详解:从核心特性到工程实践
  • 8051单片机Keil C51浮点数输入优化问题解析
  • CTF流量分析入门:10种数字犯罪现场建模与逆向思维框架
  • Keil调试中局部变量修改限制的解决方案
  • Agent热潮下的冷思考 用友付建华:大模型的落地,远没有想象中的快 | 数据猿专访
  • 量子纠错码与硬件定制逻辑门的优化实现
  • 机器人视觉修复与动作映射技术解析
  • OAuthlib错误诊断实战:从invalid_grant到temporarily_unavailable根因定位
  • ARMv8硬件翻译表更新(HTTU)原理与性能优化实践
  • Spring Boot 集成阿里云 OSS 实现文件上传下载的完整指南(从概念到代码)
  • 联想集团第一季营收216亿美元:净利5.9亿美元 股价上涨19% 市值近2000亿港元
  • 分布式锁与事务配合:为什么锁要在事务提交后释放
  • OAuthlib错误排查实战:从invalid_grant到server_error的根因定位
  • 面试:如果让你设计一个客服 Agent,你会如何划分四大组件的职责?
  • Keil µVision TAB显示异常问题分析与解决方案
  • Agentic o3调度器与Gemma/Nemotron-H推理范式演进
  • 量子退火与模拟退火在组合优化中的应用对比
  • 加拿大AI公共咨询:以人为本的政府技术治理实践
  • NXP MX芯片EMOV指令周期分析与优化
  • 解锁Linux无线网卡配置:RTL8821CU驱动实战深度指南
  • Frida-ps -U 连接失败的五层排查法
  • 量子纠错码与逻辑门优化实现技术解析
  • GE图引擎架构剖析:怎么做到“代码零修改,性能最大化“
  • 用 PS 抠公章最详细步骤|零基础一键抠取透明公章
  • 量子态相似性度量:迹距离与保真度的工程应用
  • 8051串口通信:Keil µVision输入失效问题解析