基于Dify与大模型的text2Sql数据问答工作流实践
2025-03-01 17:14:19

前言

​ 最近项目上有个数字人问数需求,即用户通过自然语言向大模型提问相关数据问题,大模型根据用户提问生成 SQL,查询数据库,根据结果总结回答用户。根据网上相关教程和提示词,在Dify 简单糊一个工作流看看效果。

效果演示

image-20250311161027837

image-20250311161154358

构建知识库

将数据库表结构存入知识库;

知识库的分段和索引模型等设置看着选吧,保证能正确分段和召回即可。

我使用的是 PostgreSQL,表结构如下:(建议表结构有完整的注释)

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
### 事件统计结果表:
CREATE TABLE dwd.dwd_event_statistics (
id SERIAL PRIMARY KEY, -- 主键
event_num INTEGER, -- 事件总数
deal_event_num INTEGER, -- 已办结事件总数
full_date DATE, -- 事件上报日期
date_of_year INTEGER, -- 事件上报年份
date_of_month INTEGER, -- 事件上报月份
date_of_day INTEGER, -- 事件上报日
info_src VARCHAR(100), -- 事件来源
level1_type_name VARCHAR(100), -- 一级事件类别
level2_type_name VARCHAR(100), -- 二级事件类别
level3_type_name VARCHAR(100), -- 三级事件类别
area_name VARCHAR(100), -- 区名称
street_name VARCHAR(100), -- 街镇名称
village_name VARCHAR(100), -- 社区名称
etl_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- 数据入库时间
);

-- 添加注释
COMMENT ON TABLE dwd.dwd_event_statistics IS '事件统计结果表';
COMMENT ON COLUMN dwd.dwd_event_statistics.id IS '主键';
COMMENT ON COLUMN dwd.dwd_event_statistics.event_num IS '事件总数';
COMMENT ON COLUMN dwd.dwd_event_statistics.deal_event_num IS '已办结事件总数';
COMMENT ON COLUMN dwd.dwd_event_statistics.full_date IS '事件上报日期';
COMMENT ON COLUMN dwd.dwd_event_statistics.date_of_year IS '事件上报年份';
COMMENT ON COLUMN dwd.dwd_event_statistics.date_of_month IS '事件上报月份';
COMMENT ON COLUMN dwd.dwd_event_statistics.date_of_day IS '事件上报日';
COMMENT ON COLUMN dwd.dwd_event_statistics.info_src IS '事件来源';
COMMENT ON COLUMN dwd.dwd_event_statistics.level1_type_name IS '一级事件类别';
COMMENT ON COLUMN dwd.dwd_event_statistics.level2_type_name IS '二级事件类别';
COMMENT ON COLUMN dwd.dwd_event_statistics.level3_type_name IS '三级事件类别';
COMMENT ON COLUMN dwd.dwd_event_statistics.area_name IS '区名称';
COMMENT ON COLUMN dwd.dwd_event_statistics.street_name IS '街镇名称';
COMMENT ON COLUMN dwd.dwd_event_statistics.village_name IS '社区名称';
COMMENT ON COLUMN dwd.dwd_event_statistics.etl_time IS '数据入库时间';

构建ChatFlow应用

具体工作流如下:

image-20250311151655681

工作流介绍

1、主要流程:

开始->知识库检索表结构->大模型生成 SQL->提取大模型返回的 SQL->调用接口执行 SQL 获取结果->大模型总结结果->输出结果

2、大模型生成 SQL 节点,提示词如下:

SYSTEM:

1
#角色:你是一位精通SQL语言的数据库专家,精通PostgreSQL,擅长理解用户需求并编写正确的SQL语句

USER:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#任务:你的任务是理解用户的输入{{#sys.query#}}和上下文内容{{#context#}},编写符合用户需求以及可正常执行的SQL语句。 
#关键步骤:
1、对用户输入的内容进行识别和判断,如果内容涉及政治、时事、社会问题以及违背道德和法律法规的情形,一律输出:”您提出的问题超出我应当回答的范围,请询问与业务相关的问题,否则我无法作出回答“,如果内容不涉及上述内容,但是你根据已有信息无法按要求回答,请输出“暂无法根据您的问题查询信息,请尝试换种方式提问“
2、根据用户输入的内容和上下文信息,形成内容分类,根据内容分类按照以下规则从知识库“数据库表结构”中检索数据表结构信息: -内容分类与事件相关,则检索“事件统计结果表” -内容分类与隐患点相关,则检索“隐患点信息表” 注意:务必严格按照上述分类获得对应的检索关键词,不得生成新的检索关键词。如果你认为用户的提问无法匹配到合适的分类,请输出提示:为确保查询获得准确信息,请再把你的需求描述细致一些
3、根据用户输入的内容和上下文信息,形成一个符合用户意图的完整问题,以此作为输入在知识库“sql示例”中检索SQL语句参考示例
4、基于对上下文和对用户提问的理解,按照检索到的数据表结构信息,以及SQL参考示例,编写SQL查询语句。注意,若内容分类与参考示例中的分类不符时,则忽略这个示例。另外,不是所有情况下都有示例参考,没有示例时请按照自己的理解和掌握的知识编写SQL语句
5、去除SQL语句中多余的注释、换行符等无用信息,Markdown语法,输出一个纯净的、可直接执行的SQL语句
#编写SQL时的注意事项:
1. 务必根据上下文提供的数据表结构描述来编写SQL语句,确保仅使用数据表结构描述中提到的表名和字段名,并参考对字段的解释
2. 确保SQL兼容PostgreSQL
3. 只用简体中文
4. 只输出一个完整SQL语句,无注释,无前缀或后缀,不需要格式化,返回输出的时候不要使用代码块,仅输出纯字符串的 SQL 语句,确保返回的 SQL 语句可直接执行并获得预期的结果
5. 对于字符串和长文本类型的字段,除非用户有特别说明,否则都用LIKE操作,而不是等于操作,例如:WHERE 产品型号 LIKE N'%关键词%',而不是WHERE 产品型号='关键词'
6. 除法处理:参考以下模板以避免错误: CASE WHEN [除数] = 0 THEN 0 ELSE CAST([被除数] AS FLOAT) / [除数] END AS [结果列名]
7.如果编写 SQL 语句是需要确定当前时间,请记住当前时间为{{#1741074867787.text#}}

3、大模型总结结果节点,提示词如下:

SYSTEM:

1
#角色:你是一个专业的问题分析师和图表绘制专家,能根据用户的提问及查询结果,回答用户的问题,并可以绘制相关的图表。

USER:

1
2
3
4
5
6
7
#任务:根据用户的提问:{{#sys.query#}},已经生成了可以执行的 SQL:{{#1741074989846.result#}},SQL 执行结果为:{{#1741075031589.body#}},请判断用户的提问和SQL结果是否适合生成图表展示 
1、如果不适合绘制图表,请直接根据 SQL 结果回答用户的问题。输出格式为 json字符串:"{"canDraw":0,"answer":"你的回答内容","data":null,"type":null}"。
2、如果适合绘制图表,请先根据 SQL 结果回答用户的问题,再生成图表,暂时仅支持饼图、线性图表和柱状图,请先根据问题和数据选择合适的图表,图表和问题结果请按以下要求输出:
- 如果适合绘制饼图,请根据 SQL 结果,输出格式为 json字符串:"{"canDraw":1,"answer":"你的回答内容","data":"用于生成饼图的数据,每个数字之间用 ; 分隔",",type":"饼图的分类,每个分类之间用 ; 分隔"}"
- 如果适合绘制线性图表,请根据 SQL 结果,输出格式为 json字符串:"{"canDraw":2,"answer":"你的回答内容","data":"用于生成线性图表的数据,每个数字之间用 ; 分隔",",type":"线性图表的 x 轴,每个文本之间用 ; 分隔"}"
- 如果适合绘制柱状图,请根据 SQL 结果,输出格式为 json字符串:"{"canDraw":3,"answer":"你的回答内容","data":"用于生成柱状图的数据,每个数字之间用 ; 分隔",",type":"柱状图的 x 轴,每个文本之间用 ; 分隔"}"
#输出要求: 请严格按照上述的 JSON 格式输出,不要输出除 JSON 外的多余文字。

4、执行SQL接口,在服务器上写个flask接口,代码如下:

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
from flask import Flask, request, jsonify
import psycopg2
import os
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()

app = Flask(__name__)

# 数据库连接配置
DB_HOST = os.getenv("DB_HOST", "localhost")
DB_NAME = os.getenv("DB_NAME", "postgres")
DB_USER = os.getenv("DB_USER", "postgres")
DB_PASSWORD = os.getenv("DB_PASSWORD", "difyai123456")
DB_PORT = os.getenv("DB_PORT", "5432")

def get_db_connection():
"""创建并返回数据库连接"""
conn = psycopg2.connect(
host=DB_HOST,
database=DB_NAME,
user=DB_USER,
password=DB_PASSWORD,
port=DB_PORT
)
return conn

@app.route('/execute_sql', methods=['POST'])
def execute_sql():
"""执行SQL语句的API接口"""
try:
# 获取请求中的SQL语句
data = request.get_json()
if not data or 'sql' not in data:
return jsonify({"error": "Missing SQL query in request"}), 400

sql_query = data['sql']

# 连接数据库
conn = get_db_connection()
cursor = conn.cursor()

# 执行SQL
cursor.execute(sql_query)

# 检查是否有结果返回
if cursor.description:
# 获取列名
columns = [desc[0] for desc in cursor.description]

# 获取所有行
rows = cursor.fetchall()

# 构建结果
results = []
for row in rows:
result = {}
for i, column in enumerate(columns):
result[column] = row[i]
results.append(result)

response = {
"status": "success",
"count": len(results),
"results": results
}
else:
# 如果是INSERT, UPDATE, DELETE等操作
conn.commit()
affected_rows = cursor.rowcount
response = {
"status": "success",
"message": f"Query executed successfully. Affected rows: {affected_rows}"
}

# 关闭连接
cursor.close()
conn.close()

return jsonify(response)

except Exception as e:
# 如果发生错误,回滚事务
if 'conn' in locals() and conn:
conn.rollback()
conn.close()

return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)

5、注意要点:

大模型生成 SQL 节点,若使用推理模型,其返回的结果会是思考过程+SQL结果,所以需要通过代码正则匹配提取最后的SQL结果。

由于大模型无法知道当前时间,如果提问涉及到与当前时间相关,需要提前获取当前时间,填入提示词中。

6、DSL文件(Version 0.15.3)

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
app:
description: ''
icon: 🤖
icon_background: '#FFEAD5'
mode: advanced-chat
name: 数字人聊天流
use_icon_as_answer_icon: false
kind: app
version: 0.1.5
workflow:
conversation_variables: []
environment_variables: []
features:
file_upload:
allowed_file_extensions:
- .JPG
- .JPEG
- .PNG
- .GIF
- .WEBP
- .SVG
allowed_file_types:
- image
allowed_file_upload_methods:
- local_file
- remote_url
enabled: false
fileUploadConfig:
audio_file_size_limit: 50
batch_count_limit: 5
file_size_limit: 15
image_file_size_limit: 10
video_file_size_limit: 100
workflow_file_upload_limit: 10
image:
enabled: false
number_limits: 3
transfer_methods:
- local_file
- remote_url
number_limits: 3
opening_statement: ''
retriever_resource:
enabled: true
sensitive_word_avoidance:
enabled: false
speech_to_text:
enabled: false
suggested_questions: []
suggested_questions_after_answer:
enabled: false
text_to_speech:
enabled: false
language: ''
voice: ''
graph:
edges:
- data:
isInIteration: false
sourceType: knowledge-retrieval
targetType: llm
id: 1741074832553-source-llm-target
selected: false
source: '1741074832553'
sourceHandle: source
target: llm
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: tool
targetType: knowledge-retrieval
id: 1741074867787-source-1741074832553-target
selected: false
source: '1741074867787'
sourceHandle: source
target: '1741074832553'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: llm
targetType: code
id: llm-source-1741074989846-target
source: llm
sourceHandle: source
target: '1741074989846'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: http-request
targetType: llm
id: 1741075031589-source-1741075092617-target
source: '1741075031589'
sourceHandle: source
target: '1741075092617'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: start
targetType: question-classifier
id: 1741074811356-source-1741075993453-target
source: '1741074811356'
sourceHandle: source
target: '1741075993453'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: question-classifier
targetType: tool
id: 1741075993453-1-1741074867787-target
source: '1741075993453'
sourceHandle: '1'
target: '1741074867787'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: question-classifier
targetType: answer
id: 1741075993453-2-1741076049272-target
source: '1741075993453'
sourceHandle: '2'
target: '1741076049272'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: question-classifier
targetType: answer
id: 1741075993453-1741076958110-1741076983758-target
source: '1741075993453'
sourceHandle: '1741076958110'
target: '1741076983758'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: code
targetType: code
id: 1741074989846-source-1741077295508-target
source: '1741074989846'
sourceHandle: source
target: '1741077295508'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: code
targetType: if-else
id: 1741077295508-source-1741077672910-target
source: '1741077295508'
sourceHandle: source
target: '1741077672910'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: if-else
targetType: http-request
id: 1741077672910-true-1741075031589-target
source: '1741077672910'
sourceHandle: 'true'
target: '1741075031589'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: if-else
targetType: answer
id: 1741077672910-false-1741077713161-target
source: '1741077672910'
sourceHandle: 'false'
target: '1741077713161'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: llm
targetType: code
id: 1741075092617-source-1741080767918-target
source: '1741075092617'
sourceHandle: source
target: '1741080767918'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: code
targetType: if-else
id: 1741080767918-source-1741081080222-target
source: '1741080767918'
sourceHandle: source
target: '1741081080222'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: if-else
targetType: answer
id: 1741081080222-true-answer-target
source: '1741081080222'
sourceHandle: 'true'
target: answer
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: tool
targetType: answer
id: 1741082537467-source-1741081418797-target
source: '1741082537467'
sourceHandle: source
target: '1741081418797'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: if-else
targetType: tool
id: 1741081080222-0042cbd7-7395-457a-aa6c-86a74a9892f0-1741082537467-target
source: '1741081080222'
sourceHandle: 0042cbd7-7395-457a-aa6c-86a74a9892f0
target: '1741082537467'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: if-else
targetType: tool
id: 1741081080222-f6e336df-d32e-4cc3-84f2-18b4f625cb79-1741156091023-target
source: '1741081080222'
sourceHandle: f6e336df-d32e-4cc3-84f2-18b4f625cb79
target: '1741156091023'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: if-else
targetType: tool
id: 1741081080222-61cbc9c9-8893-4de6-879b-f891e4302ccf-1741156101562-target
source: '1741081080222'
sourceHandle: 61cbc9c9-8893-4de6-879b-f891e4302ccf
target: '1741156101562'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: tool
targetType: answer
id: 1741156091023-source-1741081418797-target
source: '1741156091023'
sourceHandle: source
target: '1741081418797'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: tool
targetType: answer
id: 1741156101562-source-1741081418797-target
source: '1741156101562'
sourceHandle: source
target: '1741081418797'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: if-else
targetType: answer
id: 1741081080222-false-answer-target
source: '1741081080222'
sourceHandle: 'false'
target: answer
targetHandle: target
type: custom
zIndex: 0
nodes:
- data:
desc: ''
selected: false
title: 开始
type: start
variables: []
height: 54
id: '1741074811356'
position:
x: 30
y: 288
positionAbsolute:
x: 30
y: 288
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
context:
enabled: true
variable_selector:
- '1741074832553'
- result
desc: ''
memory:
query_prompt_template: '{{#sys.query#}}'
role_prefix:
assistant: ''
user: ''
window:
enabled: false
size: 10
model:
completion_params:
temperature: 0.7
mode: chat
name: deepseek-ai/DeepSeek-V3
provider: siliconflow
prompt_template:
- id: f14db6d7-353e-4bdd-bf28-17e365f9bb45
role: system
text: '#角色:你是一位精通SQL语言的数据库专家,精通Postgres SQL,擅长理解用户需求并编写正确的SQL语句'
- id: 2c6b188f-5624-4f72-a90b-68a90620ca4f
role: user
text: '#任务:你的任务是理解用户的输入{{#sys.query#}}和上下文内容{{#context#}},编写符合用户需求以及可正常执行的SQL语句。

#关键步骤:

1、对用户输入的内容进行识别和判断,如果内容涉及政治、时事、社会问题以及违背道德和法律法规的情形,一律输出:”您提出的问题超出我应当回答的范围,请询问与业务相关的问题,否则我无法作出回答“,如果内容不涉及上述内容,但是你根据已有信息无法按要求回答,请输出“暂无法根据您的问题查询信息,请尝试换种方式提问“

2、根据用户输入的内容和上下文信息,形成内容分类,根据内容分类按照以下规则从知识库“数据库表结构”中检索数据表结构信息:

-内容分类与事件相关,则检索“事件统计结果表”

-内容分类与隐患点相关,则检索“隐患点信息表”

注意:务必严格按照上述分类获得对应的检索关键词,不得生成新的检索关键词。如果你认为用户的提问无法匹配到合适的分类,请输出提示:为确保查询获得准确信息,请再把你的需求描述细致一些

3、根据用户输入的内容和上下文信息,形成一个符合用户意图的完整问题,以此作为输入在知识库“sql示例”中检索SQL语句参考示例

4、基于对上下文和对用户提问的理解,按照检索到的数据表结构信息,以及SQL参考示例,编写SQL查询语句。注意,若内容分类与参考示例中的分类不符时,则忽略这个示例。另外,不是所有情况下都有示例参考,没有示例时请按照自己的理解和掌握的知识编写SQL语句

5、去除SQL语句中多余的注释、换行符等无用信息,Markdown语法,输出一个纯净的、可直接执行的SQL语句


#编写SQL时的注意事项:

1. 务必根据上下文提供的数据表结构描述来编写SQL语句,确保仅使用数据表结构描述中提到的表名和字段名,并参考对字段的解释

2. 确保SQL兼容Postgres SQL

3. 只用简体中文

4. 只输出一个完整SQL语句,无注释,无前缀或后缀,不需要格式化,返回输出的时候不要使用代码块,仅输出纯字符串的 SQL 语句,确保返回的
SQL 语句可直接执行并获得预期的结果

5. 对于字符串和长文本类型的字段,除非用户有特别说明,否则都用LIKE操作,而不是等于操作,例如:WHERE 产品型号 LIKE N''%关键词%'',而不是WHERE
产品型号=''关键词''

6. 除法处理:参考以下模板以避免错误:

CASE WHEN [除数] = 0 THEN 0

ELSE CAST([被除数] AS FLOAT) / [除数]

END AS [结果列名]

7.如果编写 SQL 语句是需要确定当前时间,请记住当前时间为{{#1741074867787.text#}}'
selected: false
title: 生成 SQL
type: llm
variables: []
vision:
enabled: false
height: 98
id: llm
position:
x: 1246
y: 288
positionAbsolute:
x: 1246
y: 288
selected: true
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
answer: '{{#1741080767918.answer#}}'
desc: ''
selected: false
title: 直接回复
type: answer
variables: []
height: 103
id: answer
position:
x: 3952
y: 353.7142857142857
positionAbsolute:
x: 3952
y: 353.7142857142857
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
dataset_ids:
- bb33c628-92b1-4368-b0d4-77dd7b6a35cd
desc: ''
multiple_retrieval_config:
reranking_enable: true
reranking_mode: reranking_model
reranking_model:
model: netease-youdao/bce-reranker-base_v1
provider: siliconflow
top_k: 4
query_variable_selector:
- '1741074811356'
- sys.query
retrieval_mode: multiple
selected: false
title: 知识检索
type: knowledge-retrieval
height: 92
id: '1741074832553'
position:
x: 942
y: 288
positionAbsolute:
x: 942
y: 288
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
desc: ''
provider_id: time
provider_name: time
provider_type: builtin
selected: false
title: 获取当前时间
tool_configurations:
format: '%Y-%m-%d %H:%M:%S'
timezone: UTC
tool_label: 获取当前时间
tool_name: current_time
tool_parameters: {}
type: tool
height: 116
id: '1741074867787'
position:
x: 638
y: 288
positionAbsolute:
x: 638
y: 288
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
code: "import re\ndef main(arg1: str) -> dict:\n # 提取 text 字段\n text\
\ = arg1\n # 使用正则表达式提取 </details> 标签后的内容\n pattern = r'</details>\\\
n\\n(.*)'\n match = re.search(pattern, text)\n if match:\n \
\ # 提取 </details> 标签后的内容\n sql_statement = match.group(1).strip()\n\
\ return {\"result\":sql_statement}\n else:\n return {\"\
result\": text}"
code_language: python3
desc: ''
outputs:
result:
children: null
type: string
selected: false
title: 提取SQL
type: code
variables:
- value_selector:
- llm
- text
variable: arg1
height: 54
id: '1741074989846'
position:
x: 1550
y: 288
positionAbsolute:
x: 1550
y: 288
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
authorization:
config: null
type: no-auth
body:
data:
- id: key-value-566
key: ''
type: text
value: '{"sql":"{{#1741074989846.result#}}"}'
type: json
desc: ''
headers: ''
method: post
params: ''
retry_config:
max_retries: 3
retry_enabled: true
retry_interval: 100
selected: false
timeout:
max_connect_timeout: 0
max_read_timeout: 0
max_write_timeout: 0
title: 执行 SQL
type: http-request
url: http://180.00.00.00:5000/execute_sql
variables: []
height: 135
id: '1741075031589'
position:
x: 2335.428571428571
y: 288
positionAbsolute:
x: 2335.428571428571
y: 288
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
context:
enabled: false
variable_selector: []
desc: ''
model:
completion_params:
temperature: 0.7
mode: chat
name: Qwen/Qwen2.5-32B-Instruct
provider: siliconflow
prompt_template:
- id: 10d9b6b1-a3ee-425f-8b8e-04d37f99e9ec
role: system
text: '#角色:你是一个专业的问题分析师和图表绘制专家,能根据用户的提问及查询结果,回答用户的问题,并可以绘制相关的图表。'
- id: d5077a8b-944e-4529-82ad-01a3bcc36a5b
role: user
text: '#任务:根据用户的提问:{{#sys.query#}},已经生成了可以执行的 SQL:{{#1741074989846.result#}},SQL
执行结果为:{{#1741075031589.body#}},请判断用户的提问和SQL结果是否适合生成图表展示

1、如果不适合绘制图表,请直接根据 SQL 结果回答用户的问题。输出格式为 json字符串:"{"canDraw":0,"answer":"你的回答内容","data":null,"type":null}"。

2、如果适合绘制图表,请先根据 SQL 结果回答用户的问题,再生成图表,暂时仅支持饼图、线性图表和柱状图,请先根据问题和数据选择合适的图表,图表和问题结果请按以下要求输出:

- 如何适合绘制饼图,请根据 SQL 结果,输出格式为 json字符串:"{"canDraw":1,"answer":"你的回答内容","data":"用于生成饼图的数据,每个数字之间用
; 分隔",",type":"饼图的分类,每个分类之间用 ; 分隔"}"

- 如何适合绘制线性图表,请根据 SQL 结果,输出格式为 json字符串:"{"canDraw":2,"answer":"你的回答内容","data":"用于生成线性图表的数据,每个数字之间用
; 分隔",",type":"线性图表的 x 轴,每个文本之间用 ; 分隔"}"

- 如何适合绘制柱状图,请根据 SQL 结果,输出格式为 json字符串:"{"canDraw":3,"answer":"你的回答内容","data":"用于生成柱状图的数据,每个数字之间用
; 分隔",",type":"柱状图的 x 轴,每个文本之间用 ; 分隔"}"

#输出要求:

请严格按照上述的 JSON 格式输出,不要输出除 JSON 外的多余文字。'
selected: false
title: 解析SQL 执行结果
type: llm
variables: []
vision:
enabled: false
height: 98
id: '1741075092617'
position:
x: 2632.2857142857138
y: 288
positionAbsolute:
x: 2632.2857142857138
y: 288
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
classes:
- id: '1'
name: 数据查询问题
- id: '2'
name: 操作指令
- id: '1741076958110'
name: 其他
desc: ''
instruction: ''
instructions: ''
model:
completion_params:
temperature: 0.7
mode: chat
name: Qwen/Qwen2.5-32B-Instruct
provider: siliconflow
query_variable_selector:
- '1741074811356'
- sys.query
selected: false
title: 问题分类器
topics: []
type: question-classifier
vision:
enabled: false
height: 212
id: '1741075993453'
position:
x: 334
y: 288
positionAbsolute:
x: 334
y: 288
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
answer: 指令操作开发中。
desc: ''
selected: false
title: 直接回复 2
type: answer
variables: []
height: 100
id: '1741076049272'
position:
x: 638
y: 444
positionAbsolute:
x: 638
y: 444
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
answer: 暂无法理解您的问题或需求。
desc: ''
selected: false
title: 直接回复 3
type: answer
variables: []
height: 100
id: '1741076983758'
position:
x: 638
y: 583
positionAbsolute:
x: 638
y: 583
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
code: "\ndef main(arg1: str) -> dict:\n if arg1.lower().startswith('select'):\n\
\ return {\n \"is_select_sql\": 1,\n \"result\"\
: arg1\n }\n else :\n return {\n \"is_select_sql\"\
: 0,\n \"result\": arg1\n }\n\n \n"
code_language: python3
desc: ''
outputs:
is_select_sql:
children: null
type: number
result:
children: null
type: string
selected: false
title: 判断是否为 SQL
type: code
variables:
- value_selector:
- '1741074989846'
- result
variable: arg1
height: 54
id: '1741077295508'
position:
x: 1872.5714285714284
y: 288
positionAbsolute:
x: 1872.5714285714284
y: 288
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
cases:
- case_id: 'true'
conditions:
- comparison_operator: '='
id: 34f07303-7757-4f85-bf69-8e260999a284
value: '1'
varType: number
variable_selector:
- '1741077295508'
- is_select_sql
id: 'true'
logical_operator: and
desc: ''
selected: false
title: 条件分支
type: if-else
height: 126
id: '1741077672910'
position:
x: 1986.8571428571427
y: 430.57142857142856
positionAbsolute:
x: 1986.8571428571427
y: 430.57142857142856
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
answer: '{{#1741077295508.result#}}'
desc: ''
selected: false
title: 直接回复 4
type: answer
variables: []
height: 103
id: '1741077713161'
position:
x: 2335.428571428571
y: 462
positionAbsolute:
x: 2335.428571428571
y: 462
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
code: "import json\ndef main(arg1: str) -> dict:\n # 解析 JSON 数据\n data\
\ = json.loads(arg1)\n return data"
code_language: python3
desc: ''
outputs:
answer:
children: null
type: string
canDraw:
children: null
type: number
data:
children: null
type: string
type:
children: null
type: string
selected: false
title: 解析问题结果参数提取
type: code
variables:
- value_selector:
- '1741075092617'
- text
variable: arg1
height: 54
id: '1741080767918'
position:
x: 2956.285714285714
y: 393
positionAbsolute:
x: 2956.285714285714
y: 393
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
cases:
- case_id: 'true'
conditions:
- comparison_operator: '='
id: 3c9f6dea-3f0c-4037-a964-42427b206679
value: '0'
varType: number
variable_selector:
- '1741080767918'
- canDraw
id: 'true'
logical_operator: and
- case_id: 0042cbd7-7395-457a-aa6c-86a74a9892f0
conditions:
- comparison_operator: '='
id: cc2b3f2e-f428-48eb-90bf-bc0466e1e9bf
value: '1'
varType: number
variable_selector:
- '1741080767918'
- canDraw
id: 0042cbd7-7395-457a-aa6c-86a74a9892f0
logical_operator: and
- case_id: f6e336df-d32e-4cc3-84f2-18b4f625cb79
conditions:
- comparison_operator: '='
id: 69a6583e-35c5-4eea-a352-8a3d52b9d5cc
value: '2'
varType: number
variable_selector:
- '1741080767918'
- canDraw
id: f6e336df-d32e-4cc3-84f2-18b4f625cb79
logical_operator: and
- case_id: 61cbc9c9-8893-4de6-879b-f891e4302ccf
conditions:
- comparison_operator: '='
id: 06b02ba1-482e-449e-8a48-18505e1d57d3
value: '3'
varType: number
variable_selector:
- '1741080767918'
- canDraw
id: 61cbc9c9-8893-4de6-879b-f891e4302ccf
logical_operator: and
desc: ''
selected: false
title: 判断是否生成图表
type: if-else
height: 270
id: '1741081080222'
position:
x: 3261.714285714286
y: 393
positionAbsolute:
x: 3261.714285714286
y: 393
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
answer: '{{#1741080767918.answer#}}{{#1741082537467.files#}}{{#1741156091023.files#}}{{#1741156101562.files#}}'
desc: ''
selected: false
title: 直接回复 5
type: answer
variables: []
height: 158
id: '1741081418797'
position:
x: 4252
y: 513.7142857142858
positionAbsolute:
x: 4252
y: 513.7142857142858
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
desc: ''
provider_id: chart
provider_name: chart
provider_type: builtin
selected: false
title: 饼图
tool_configurations: {}
tool_label: 饼图
tool_name: pie_chart
tool_parameters:
categories:
type: mixed
value: '{{#1741080767918.type#}}'
data:
type: mixed
value: '{{#1741080767918.data#}}'
type: tool
height: 54
id: '1741082537467'
position:
x: 3952
y: 513.7142857142858
positionAbsolute:
x: 3952
y: 513.7142857142858
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
desc: ''
provider_id: chart
provider_name: chart
provider_type: builtin
selected: false
title: 线性图表
tool_configurations: {}
tool_label: 线性图表
tool_name: line_chart
tool_parameters:
data:
type: mixed
value: '{{#1741080767918.data#}}'
x_axis:
type: mixed
value: '{{#1741080767918.type#}}'
type: tool
height: 54
id: '1741156091023'
position:
x: 3952
y: 606.7142857142858
positionAbsolute:
x: 3952
y: 606.7142857142858
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
desc: ''
provider_id: chart
provider_name: chart
provider_type: builtin
selected: false
title: 柱状图
tool_configurations: {}
tool_label: 柱状图
tool_name: bar_chart
tool_parameters:
data:
type: mixed
value: '{{#1741080767918.data#}}'
x_axis:
type: mixed
value: '{{#1741080767918.type#}}'
type: tool
height: 54
id: '1741156101562'
position:
x: 3952
y: 699.7142857142858
positionAbsolute:
x: 3952
y: 699.7142857142858
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
viewport:
x: -1044.298931079242
y: -100.79975541473073
zoom: 0.7020879457826513

总结

可以通过Agent方式实现?

可以,修改下提示词,让大模型既能生成SQL,也能分析SQL结果,也能生成图表,添加相关工具,让Agent自动编排,效果如下:

image-20250311170538249

image-20250311170649635

image-20250311170707565

生成SQL的准确率怎么样?

​ 这种基于知识库和提示词的大模型text2Sql生成的SQL并不能保证100%正确,不同类型和参数的大模型生成的 SQL 效果不一,当提问比较复杂或者涉及到多表关联查询时,其生成的SQL正确率也会比较低。

怎么提高Text2SQL正确率?

待研究🧐

相关的开源软件?

1、Supersonic:https://github.com/tencentmusic/supersonic

2、DB-GPT:https://github.com/eosphoros-ai/DB-GPT

等等,还有很多。

上一页
2025-03-01 17:14:19
下一页