网站首页 > java教程 正文
思考
本地使用springboot开发了很多接口,dify如何快速引入这些接口,作为aget的function call工具来使用,最近一直在相关学习,给大家做个分享。最终效果如下
Java引入swagger
在我们本地的java项目中引入pom配置jar包
<!-- 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
添加自定义工具
成功调用工具
猜你喜欢
- 2025-05-02 dify案例分享-API文档生成接口代码
- 2025-05-02 从 0 到 1 实战 Spring Boot 3:手把手教你构建高效 RESTful 接口
- 2025-05-02 原来大牛们口中的接口Mock测试是长这样的,今天我终于搞懂了
- 2025-05-02 什么是函数式接口?(函数式接口使用场景)
- 2025-05-02 请求合并的三种方式,大大提高接口性能
- 2025-05-02 【开源推荐】PhalApi2.13(致敬版) 发布,PHP 轻量级开源接口框架
- 2025-05-02 Java:在Java中使用私有接口方法(java中如何调用私有成员变量)
- 2025-05-02 Java接口与抽象类:核心区别、使用场景与最佳实践
- 2025-05-02 实现AI API Tokens计费产品方案和Java技术实现设计(含源码分享)
- 2025-05-02 Spring Security 接口认证鉴权入门实践指南
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)