专业的JAVA编程教程与资源

网站首页 > java教程 正文

dify案例:dify导入本地Java接口作为function call工具

temp10 2025-05-02 13:13:42 java教程 4 ℃ 0 评论

思考

本地使用springboot开发了很多接口,dify如何快速引入这些接口,作为aget的function call工具来使用,最近一直在相关学习,给大家做个分享。最终效果如下

Java引入swagger

在我们本地的java项目中引入pom配置jar包

dify案例:dify导入本地Java接口作为function call工具

<!-- SpringDoc OpenAPI UI (用于生成接口文档页面) -->
<dependency>
  <groupId>org.springdoc</groupId>
	<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
	<version>2.1.0</version>
</dependency>

增加配置类OpenApiConfig

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenApiConfig {
  @Bean
  public OpenAPI customOpenAPI() {
    return new OpenAPI()
      .info(new Info()
            .title("我的本地工具包")
            .version("1.0.0")
            .description("本地工具包,使用springdoc-openapi and OpenAPI 3."));
  }
}

在相应的接口上增加对应的swagger注解描述

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/tools")
@Tag(name = "java接口工具", description = "java接口工具")
public class AgentToolsController {
  @Operation(summary = "发送邮件", description = "发送邮件",operationId = "send_mail")
  @GetMapping("/sendMail")
  public String sendMail(@Parameter(description = "The message to send in the email") @RequestParam String msg) {
    return "邮件发送成功" + msg;
  }
}

启动java服务之后,测试结果

Swagger UI界面:
http://localhost:8080/swagger-ui.html

可以看到我们发送邮件的接口

OpenAPI JSON文档:
http://localhost:8080/v3/api-docs

具体的json文件如下,我们要注意把localhost替换为host.docker.internal,我们在dify新建工具会用到这个json

{
  "openapi": "3.0.1",
  "info": {
    "title": "我的本地工具包",
    "description": "本地工具包,使用springdoc-openapi and OpenAPI 3.",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://host.docker.internal:8081",
      "description": "Generated server url"
    }
  ],
  "tags": [
    {
      "name": "java接口工具",
      "description": "java接口工具"
    }
  ],
  "paths": {
    "/tools/sendMail": {
      "get": {
        "tags": [
          "java接口工具"
        ],
        "summary": "发送邮件",
        "description": "发送邮件",
        "operationId": "send_mail",
        "parameters": [
          {
            "name": "msg",
            "in": "query",
            "description": "The message to send in the email",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "*/*": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {}
}


dify新建自定义工具

新建自定义工具,导入上面生成的json

点击测试工具是否能够正常访问

测试接口能够正常返回

保存成功之后,我们就能看到接口被作为工具保存在dify上面了


agent调用工具

新建一个简单的agent

添加自定义工具

成功调用工具

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表