【AI大模型应用开发】2.2 Function Calling连接外部世界 - 【实战】查询数据库

大家好,我是【同学小张】。持续学习,关注我,跟我一起学AI大模型应用开发。

上篇文章我们学习了Function Calling的基本用法,本文我们更近一步,学习下怎样利用Function Calling将大模型与数据库打通。

知识背景:我算是对数据库的SQL语句很不熟悉,只会简单的单表操作,还不熟练,每次都得查半天。现在有了大模型应用,有了Function Calling,再也不用查半天资料才写一个SQL了,还能熟练地用多表查询了!
本文实战案例来自知乎的AGI课程。

0. 封装数据库查询接口

还是先本地定义一个查询数据库的接口,该接口接收SQL数据库操作语句,然后调用执行。

python 复制代码
# 创建数据库连接
import sqlite3

conn = sqlite3.connect(':memory:')
cursor = conn.cursor()

def ask_database(query):
    cursor.execute(query)
    records = cursor.fetchall()
    return records

1. 数据库的Functions怎么定义

示例代码:

python 复制代码
#  描述数据库表结构
database_schema_string = """
CREATE TABLE orders (
    id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空
    customer_id INT NOT NULL, -- 客户ID,不允许为空
    product_id STR NOT NULL, -- 产品ID,不允许为空
    price DECIMAL(10,2) NOT NULL, -- 价格,不允许为空
    status INT NOT NULL, -- 订单状态,整数类型,不允许为空。0代表待支付,1代表已支付,2代表已退款
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 创建时间,默认为当前时间
    pay_time TIMESTAMP -- 支付时间,可以为空
);
"""

def get_sql_completion(messages, model="gpt-3.5-turbo-1106"):
    response = openai.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0,  # 模型输出的随机性,0 表示随机性最小
        tools=[{  # 摘自 OpenAI 官方示例 https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb
            "type": "function",
            "function": {
                "name": "ask_database",
                "description": "Use this function to answer user questions about business. \
                            Output should be a fully formed SQL query.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": f"""
                            SQL query extracting info to answer the user's question.
                            SQL should be written using this database schema:
                            {database_schema_string}
                            The query should be returned in plain text, not in JSON.
                            The query should only contain grammars supported by SQLite.
                            """,
                        }
                    },
                    "required": ["query"],
                }
            }
        }],
    )
    return response.choices[0].message

重点看两个description:

(1)function 的 description:Use this function to answer user questions about business. Output should be a fully formed SQL query."

  • 要求输出必须是一个SQL语句

(2)参数的description:

python 复制代码
f"""
SQL query extracting info to answer the user's question.
SQL should be written using this database schema:
{database_schema_string}
The query should be returned in plain text, not in JSON.
The query should only contain grammars supported by SQLite.
""",
  • 将数据库的数据结构database_schema_string放到prompt中,根据此表的数据结构写SQL语句
  • 不能返回json,而是返回字符串
  • 返回的query语句文字必须符合SQL语法

2. 可以使用了

python 复制代码
prompt = "10月的销售额"

messages = [
    {"role": "system", "content": "基于 order 表回答用户问题"},
    {"role": "user", "content": prompt}
]
response = get_sql_completion(messages)
if response.content is None:
    response.content = ""
messages.append(response)
print("====Function Calling====")
print_json(response)

if response.tool_calls is not None:
    tool_call = response.tool_calls[0]
    if tool_call.function.name == "ask_database":
        arguments = tool_call.function.arguments
        args = json.loads(arguments)
        print("====SQL====")
        print(args["query"])
        result = ask_database(args["query"])
        print("====DB Records====")
        print(result)

        messages.append({
            "tool_call_id": tool_call.id,
            "role": "tool",
            "name": "ask_database",
            "content": str(result)
        })
        response = get_sql_completion(messages)
        print("====最终回复====")
        print(response.content)

看下运行过程,可以看到返回了正确的SQL查询语句:

(但是因为我本地没有数据库,所以没有查询到数据,返回"暂时不可用"。为了测试,你可以在本地事先生成一份demo数据,这就涉及到Python操作数据库了,有需要的话我再写个文章介绍下怎么用Python操作数据库)

3. 进阶 - 多表查询

(1)将数据表数据结构改一下,改成多张表即可

python 复制代码
#  描述数据库表结构
database_schema_string = """
CREATE TABLE customers (
    id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空
    customer_name VARCHAR(255) NOT NULL, -- 客户名,不允许为空
    email VARCHAR(255) UNIQUE, -- 邮箱,唯一
    register_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- 注册时间,默认为当前时间
);
CREATE TABLE products (
    id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空
    product_name VARCHAR(255) NOT NULL, -- 产品名称,不允许为空
    price DECIMAL(10,2) NOT NULL -- 价格,不允许为空
);
CREATE TABLE orders (
    id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空
    customer_id INT NOT NULL, -- 客户ID,不允许为空
    product_id INT NOT NULL, -- 产品ID,不允许为空
    price DECIMAL(10,2) NOT NULL, -- 价格,不允许为空
    status INT NOT NULL, -- 订单状态,整数类型,不允许为空。0代表待支付,1代表已支付,2代表已退款
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 创建时间,默认为当前时间
    pay_time TIMESTAMP -- 支付时间,可以为空
);
"""

(2)提问

python 复制代码
prompt = "统计每月每件商品的销售额"
# prompt = "这星期消费最高的用户是谁?他买了哪些商品? 每件商品买了几件?花费多少?"
messages = [
    {"role": "system", "content": "基于 order 表回答用户问题"},
    {"role": "user", "content": prompt}
]
response = get_sql_completion(messages)
print(response.tool_calls[0].function.arguments)

(3)运行结果,生成了正确的多表查询语句

4. 思考 - 利用大模型写数据库查询语句的Prompt

通过上面对function和参数的description描述,我们可以大体看出怎样利用大模型去写正确的SQL查询语句。

Prompt如下:

假如你是一个SQL数据库专家。请按照用户的输入生成一个SQL语句。 SQL should be written using this database schema: """ CREATE TABLE customers ( id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空 customer_name VARCHAR(255) NOT NULL, -- 客户名,不允许为空 email VARCHAR(255) UNIQUE, -- 邮箱,唯一 register_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- 注册时间,默认为当前时间 ); CREATE TABLE products ( id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空 product_name VARCHAR(255) NOT NULL, -- 产品名称,不允许为空 price DECIMAL(10,2) NOT NULL -- 价格,不允许为空 ); CREATE TABLE orders ( id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空 customer_id INT NOT NULL, -- 客户ID,不允许为空 product_id INT NOT NULL, -- 产品ID,不允许为空 price DECIMAL(10,2) NOT NULL, -- 价格,不允许为空 status INT NOT NULL, -- 订单状态,整数类型,不允许为空。0代表待支付,1代表已支付,2代表已退款 create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 创建时 间,默认为当前时间 pay_time TIMESTAMP -- 支付时间,可以为空 ); """

The query should be returned in plain text, not in JSON. The query should only contain grammars supported by SQLite. Output should be a fully formed SQL query.

用户输入:统计每月每件商品的销售额,包含商品名称

结果:

完美。

注意: Function Calling 不仅可以调用读函数,也能调用写函数。但官方强烈建议,在写之前,一定要有人做确认,因为大模型的结果有不确定性。


  • 大家好,我是同学小张
  • 欢迎 点赞 + 关注 👏,促使我持续学习持续干货输出
  • +v: jasper_8017 一起交流💬,一起进步💪。
  • 微信公众号也可搜【同学小张】 🙏
  • ==踩坑不易,感谢关注和围观==

本站文章一览:

相关推荐
Rocky Ding*5 小时前
【三年面试五年模拟】2026-09-08 DeepSeek AI Agent开发岗社招一面:20道问题与系统设计题全解析
论文阅读·人工智能·深度学习·机器学习·aigc·ai-native·deepseek
宝桥南山5 小时前
DeepSeek - 尝试安装和使用一下DeepSeek Harness
人工智能·ai·aigc·ai编程
Coffeeee6 小时前
claude-video 一个让你的Agent拥有看视频能力的Skill
android·人工智能·aigc
狠活科技7 小时前
GPT Image 2.5 发布:AI 生图又进化了
人工智能·gpt·ai作画·aigc·image2.5
刘广睿9 小时前
给素材库加语音转写:Whisper 本地部署与批量字幕生成实践
人工智能·aigc·音视频·语音识别·效率工具
全栈弄潮儿9 小时前
周复盘:这一周最值得保存的 7 条 AI 编程原则
aigc·openai·ai编程
知了一笑9 小时前
产品先上线再开发
人工智能·互联网·aigc
wangruofeng17 小时前
一个 Markdown 文件攒下 37k 星,i-have-adhd 给 AI 输出立了 10 条规矩
github·aigc·ai编程
墨风如雪17 小时前
Grok Bot 从 0 到 1|用云电脑和插件,搭一个能干活的 X 助手
aigc
Csvn18 小时前
第 22 章 安全、合规与治理
人工智能·aigc·agent