dify-调用自定义工具

用户输入问题为离散化数据,在构建流程或agent时,可以给大模型相应提示词以处理为结构化数据,再交由自定义工具调用。

以简单数据增删查改为例

!!!实际使用注意数据安全问题

测试数据表

1
2
3
4
5
6
7
8
9
create table student
(
id int auto_increment
primary key,
name varchar(30) not null comment '姓名',
age int default 0 not null comment '年龄'
);


创建http接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@RestController
@RequestMapping("/outerApi")
public class OuterApiTestController {

@Resource
private StudentMapper studentMapper;

@ApiOperation(value = "查询所有学生")
@GetMapping("/stu/list")
public Result<List<Student>> list() {
List<Student> students = studentMapper.selectList(new QueryWrapper<Student>());
return Result.ok(students);
}

@ApiOperation(value = "新增学生")
@PostMapping("/stu/add")
public Result<Integer> add(@RequestBody Student student) {
studentMapper.insert(student);
return Result.ok(1);
}

@ApiOperation(value = "删除学生")
@PostMapping("/stu/delete")
public Result<Integer> delete(@RequestBody Student student) {
HashMap<String, Object> map = new HashMap<>();
if(student.getId() != null) {
map.put("id", student.getId());
}
if(StringUtils.isNotBlank(student.getName())) {
map.put("name", student.getName());
}
if(student.getAge() != null) {
map.put("age", student.getAge());
}
studentMapper.deleteByMap(map);
return Result.ok(1);
}

}

创建自定义工具

dify自定义工具需要填写工具的openApi schema,接口交给AI生成一下就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
openapi: 3.0.0
info:
title: 学生管理 API
version: 1.0.0
description: 学生管理相关的接口
servers:
- url: http://host.docker.internal:8089/jeecg-boot/outerApi
description: 开发环境
paths:
/stu/list:
get:
tags:
- 学生管理
summary: 查询所有学生
operationId: listStudents
responses:
'200':
description: 成功
content:
application/json:
schema:
$ref: '#/components/schemas/ResultListStudent'
/stu/add:
post:
tags:
- 学生管理
summary: 新增学生
operationId: addStudent
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Student'
responses:
'200':
description: 成功
content:
application/json:
schema:
$ref: '#/components/schemas/ResultInteger'
/stu/delete:
post:
tags:
- 学生管理
summary: 删除学生
operationId: deleteStudent
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Student'
responses:
'200':
description: 成功
content:
application/json:
schema:
$ref: '#/components/schemas/ResultInteger'
components:
schemas:
Student:
type: object
required:
- name
- age
properties:
id:
type: integer
format: int64
description: 学生ID
name:
type: string
description: 学生姓名
age:
type: integer
format: int32
description: 学生年龄
example:
id: 1
name: 张三
age: 20
ResultListStudent:
type: object
properties:
code:
type: integer
format: int32
example: 200
message:
type: string
example: 成功
data:
type: array
items:
$ref: '#/components/schemas/Student'
ResultInteger:
type: object
properties:
code:
type: integer
format: int32
example: 200
message:
type: string
example: 成功
data:
type: integer
format: int32
example: 1

检测出三个可用工具即为正常

1
2
3
listStudents	查询所有学生
addStudent 新增学生
deleteStudent 删除学生

测试一下是否正常

成功返回了

新建一个Agent看看效果吧