找回密码
 立即注册

2,Ai Agent工具调用

[复制链接]
admin 发表于 2026-6-2 11:00:50 | 显示全部楼层 |阅读模式
1,注解
1.1方法注解
  1. package com.jinhei;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. /**
  7. * 工具方法注解
  8. * 用于标识一个方法为 AI 工具, 使其能够被 AI 助手调用
  9. */
  10. @Target(ElementType.METHOD)
  11. @Retention(RetentionPolicy.RUNTIME)
  12. public @interface Tool {
  13.     /**
  14.      * 工具方法的描述信息
  15.      * 用于向 AI 助手说明该方法的功能
  16.      * @return 方法的描述
  17.      */
  18.     String description();
  19. }
复制代码
1.2 参数注解
  1. package com.jinhei;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. /**
  7. * 工具参数注解
  8. * 用于标识工具的方法的参数, 提供参数的描述信息
  9. * 帮助 AI 助手理解每个参数的用途
  10. */
  11. @Target(ElementType.PARAMETER)
  12. @Retention(RetentionPolicy.RUNTIME)
  13. public @interface ToolParam {
  14.     /**
  15.      * 参数的描述信息
  16.      * 用于向 AI 助手说明该参数的含义和用途
  17.      * @return 参数的描述
  18.      */
  19.     String description();
  20. }
复制代码
1.3 使用
  1. package com.jinhei;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. /**
  7. * 代理工具类 - 为AI提供可调用的功能
  8. */
  9. public class AgentTools {
  10.     // JSON解释器 - 用来读懂JSON格式的数据
  11.     private final ObjectMapper objectMapper = new ObjectMapper();
  12.     /**
  13.      * 将制定内容写入到本地文件中
  14.      * @param jsonInput 包含 'filePath' 和 'content' 的 JSON 字符串
  15.      * @return 执行结果
  16.      */
  17.     @Tool(description = "将指定内容写入本地文件") //这个注解告诉 AI , 这是一个可用的工具
  18.     public String putFile(@ToolParam(description = "包含 'filePath' 和 'content' 的 JSON 字符串") String jsonInput) {
  19.         //第一步: 尝试执行写文件操作(就像尝试完成一项任务)
  20.         try {
  21.             //1.1 解析JSON输入: 读懂"文件保存单"
  22.             //把json字符串解析成树形结构, 方便提取数据
  23.             JsonNode rootNode = objectMapper.readTree(jsonInput);
  24.             
  25.             //提取文件路径: 从"文件保存单"上面找到"保存位置"这一项
  26.             String filePath = rootNode.get("filePath").asText();
  27.             //提取文件内容: 从"文件保存单"上面找到"文件内容"这一项
  28.             String content = rootNode.get("content").asText();
  29.             
  30.             //1.2执行写文件: 真正的把内容写到磁盘上
  31.             try (FileWriter writer = new FileWriter(filePath)) {
  32.                 //把内容写到文件中
  33.                 writer.write(content);
  34.                 //成功!告诉AI任务完成了
  35.                 return String.format("成功将内容写入文件 '%s'", filePath);
  36.             } catch (IOException e) {
  37.                 //写文件失败(比如磁盘没有权限或者磁盘满了等等)
  38.                 return String.format("写入文件 '%s' 时发生错误: '%s'", filePath, e.getMessage());
  39.             }
  40.             
  41.         } catch (Exception e) {
  42.             //解析json失败 (比如: AI给的格式不对, 缺少必要的字段)
  43.             return String.format("解析 Action input 或执行 putFile 工具时出错: %s", e.getMessage());
  44.         }
  45.     }
  46. }
复制代码

学习点:把json字符串解析成树形结构, 方便提取数据
https://www.jinhei.cn/thread-382-1-1.html


invoke()使用
https://www.jinhei.cn/thread-383-1-1.html

QQ|网站地图|Archiver|小黑屋|金黑网络 ( 粤ICP备2021124338号 )

网站建设,微信公众号小程序制作,商城系统开发,高端系统定制,app软件开发,智能物联网开发,直播带货系统等

Powered by Www.Jinhei.Cn

Copyright © 2013-2024 深圳市金黑网络技术有限公司 版权所有

快速回复 返回顶部 返回列表