LLM Agent 之数据库应用设计(二)
C3: Zero-shot Text-to-SQL with ChatGPT
C3 (Zero-shot Text-to-SQL with ChatGPT) 通过优化 Schema Description 结合多路投票的解码方案,利用 Zero-shot Prompt 基本追平了 DIN-SQL 的效果。
在 DIN-SQL 提出的 Few-shot 方案基础上,C3 使用 ChatGPT 作为基座模型,探索了 Zero-shot 的方案,进一步降低了推理成本。在生成效果上,它与 DIN-SQL 不相上下。
Clear Prompting
C3 同样通过 Schema Linking 先定位问题相关的数据表和查询字段。不过在指令构建上,论文认为在编写指令时,简洁的文本格式(Clear Layout),以及不引入不相关的表结构(Clear Context),会降低模型理解难度,对模型效果有很大提升。
Clear Layout
后续的 SQL-Palm 也进行了类似的消融实验,对比符合人类自然语言描述的 Table Schema,使用符号表征的 Prompt 效果显著更好,在执行准确率上有 7% 左右的提升。
Clear Context
把整个数据库的全部表结构放入 Schema Linking Context,一方面增加了推理长度,另一方面会使得模型有更大概率定位到无关的查询字段。因此 C3 通过以下两步先召回相关的数据表和表字段,再进行 Schema Linking。
- 数据表召回
C3 使用以下 Zero-shot 指令,让大模型基于数据表 Schema,召回问题相关的数据表。这一步作者采用了 Self-Consistency 来投票得到概率最高的 Top4 数据表。当前的一些开源方案也有采用相似度召回的方案,更适合低延时、面向超大数据库的场景。不过需要先人工对每张表生成一段表描述,描述该表是用来干啥的,然后通过 Query 与 Description 的 Embedding 相似度来筛选 TopK 数据表。
instruction = """
Given the database schema and question, perform the following actions:
1 - Rank all the tables based on the possibility of being used in the SQL according to the question from the most relevant to the least relevant.
2 - Check whether you consider all the tables.
3 - Output a list object in the order of step 2, Your output should contain all the tables.
The format should be like: ["table_1", "table_2", ...]
"""
- 表字段召回
在以上得到问题相关的数据表之后,会继续执行表字段召回的操作,同样使用了 Self-Consistency 多路推理投票得到概率最高的 Top5 字段。这一步同样可以使用相似度召回,尤其在中文场景,以及垂直领域的数据表场景,直接使用字段名并不足够,也需要对表字段名称生成对应的描述,然后使用相似度进行召回。
instruction = '''
Given the database tables and question, perform the following actions:
1 - Rank the columns in each table based on the possibility of being used in the SQL.
Explain why you choose each column.
2 - Output a JSON object that contains all the columns in each table according to your explanation.
The format should be like:
{
"table_1": ["column_1", "column_2"],
"table_2": ["column_1", "column_2"]
}
'''
Self-Consistency
Schema Linking 之后,C3 没有像 DIN 一样去判断问题的难度,而是用统一的 Zero-Prompt 来对所有问题进行推理。不过在推理部分引入了 Self-Consistency 的多路投票方案。
针对每个问题会随机生成多个 SQL,然后去数据库进行执行,过滤无法执行的 SQL,对剩余 SQL 的执行结果进行分组,从答案出现次数最多的分组随机选一个 SQL 作为最终的答案,也就是基于 SQL 执行结果的 Major Vote 方案。


