找回密码
 立即注册
首页 业界区 业界 Solon AI 开发学习13 - chat - Tool的输入输出架构及生 ...

Solon AI 开发学习13 - chat - Tool的输入输出架构及生成类

劳怡月 3 天前
1、Tool输出给 llm 的描述形态

这个形态下 parameters 属性是一个 jsonSchema 规范的结构。也就是工具的“输入架构”(mcp 里的叫法)
  1. {
  2.     "type": "function",
  3.     "function": {
  4.         "name": "get_weather",
  5.         "description": "获取指定城市的天气情况",
  6.         "parameters": {
  7.             "type": "object",
  8.             "properties": {
  9.                 "location": {
  10.                     "type": "string",
  11.                     "description": "根据用户提到的地点推测城市"
  12.                 }
  13.             },
  14.             "required": [
  15.                 "location"
  16.             ],
  17.             "additionalProperties": False
  18.         },
  19.         "strict": True
  20.     }
  21. }
复制代码
2、Tool注册给 mcp 的描述形态

这个形态下多了 outputSchema (符合 jsonSchema 规范的输出架构)属性,且 parameters 属性变成了 inputSchema(可以与 outputSchema 呼应上)。
  1. {
  2.     "name": "get_weather",
  3.     "description": "获取指定城市的天气情况",
  4.     "inputSchema": {
  5.         "type": "object",
  6.         "properties": {
  7.             "location": {
  8.                 "type": "string",
  9.                 "description": "根据用户提到的地点推测城市"
  10.             }
  11.         },
  12.         "required": [
  13.             "location"
  14.         ],
  15.         "additionalProperties": False
  16.     },
  17.     "outputSchema": {
  18.         "type": "string"
  19.     }
  20. }
复制代码
3、构建Tool的 outputSchema(输出架构)定义

(v3.2.2 后支持)
使用 FunctionToolDesc 描述工具时(即手动构建),通过 returnType 声明
  1. import org.noear.solon.ai.chat.tool.FunctionToolDesc;
  2. FunctionToolDesc toolDesc = new FunctionToolDesc("get_weather")
  3.     .description("获取指定城市的天气情况")
  4.     .stringParamAdd("location", "根据用户提到的地点推测城市")
  5.     .returnType(String.class)
  6.     .doHandle(args -> {
  7.         return "晴,24度"; // + weatherService.get(location);
  8.     })
复制代码
使用 MethodFunctionTool 描述工具时(即 @ToolMapping 注解函数构建),通过方法返回类型自动声明
  1. import org.noear.solon.annotation.Param;
  2. import org.noear.solon.ai.annotation.ToolMapping;
  3. import org.noear.solon.ai.mcp.server.annotation.McpServerEndpoint;
  4. @McpServerEndpoint(sseEndpoint = "/mcp/sse")
  5. public class Tools {
  6.     @ToolMapping(description = "获取指定城市的天气情况")
  7.     public String get_weather(@Param(name = "location", description = "根据用户提到的地点推测城市") String location) {
  8.         return "晴,24度"; // + weatherService.get(location);
  9.     }
  10. }
复制代码
如果返回的是实体结果时,还可以通过 @Param 注解增加描述
  1. import org.noear.solon.annotation.Param;
  2. import org.noear.solon.ai.annotation.ToolMapping;
  3. import org.noear.solon.ai.mcp.server.annotation.McpServerEndpoint;
  4. public class UserInfo {
  5.     @Param(description = "用户名")
  6.     private String name;
  7.     @Param(description = "年龄")
  8.     private Integer age;
  9.     @Param(description = "性别。0表示女,1表示男")
  10.     private Integer gender;
  11. }
  12. @McpServerEndpoint(sseEndpoint = "/mcp/sse")
  13. public class Tools {
  14.     @Inject
  15.     UserService userService;
  16.     @ToolMapping(description = "获取用户信息")
  17.     public UserInfo getUserInfo(@Param(description = "用户ID") Long userId) {
  18.         return userService.getUser(userId);
  19.     }
  20. }
复制代码
4、Tool 的 JsonSchema 生成类 ToolSchemaUtil

现有的Tool架构是由 ToolSchemaUtil 提供支持
方法描述备注buildInputParams构建 tool 的输出参数描述支持 @Body 实体自动分解字段buildInputSchema构建 tool 输入架构buildOutputSchema构建 tool 输出架构isIgnoreOutputSchema检测 tool 需要乎略的输出架构比如单值类型createSchema生成一个类型的 JsonSchema通用方法addBodyDetector添加主体注解探测器第三方框架使用时,可用它扩展addParamResolver添加参数注解分析器同上addNodeDescribe添加节点描述处理同上
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册