通过 ChatGPT 的 Function Call 查询数据库

用 Function Calling 的方式实现手机流量包智能客服的例子。

python 复制代码
def get_sql_completion(messages, model="gpt-3.5-turbo"):
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=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 packages. \
                            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
python 复制代码
#  描述数据库表结构
database_schema_string = """
CREATE TABLE packages (
    id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空
    package_name STR NOT NULL, -- 套餐名称,不允许为空
    monthly_fee INT NOT NULL, -- 月费,单位元,不允许为空
    flow_size INT NOT NULL, -- 流量大小,单位G,不允许为空
    condition STR -- 购买的限制条件,允许为空
);
"""
python 复制代码
import sqlite3

# 创建数据库连接
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()

# 创建orders表
cursor.execute(database_schema_string)

# 插入4条明确的模拟记录
mock_data = [
    (1, '经济套餐', 50, 10, None),
    (2, '畅游套餐', 180, 100, None),
    (3, '无限套餐', 300, 1000, None),
    (4, '校园套餐', 150, 200, '仅限在校生'),
]

for record in mock_data:
    cursor.execute('''
    INSERT INTO packages (id, package_name, monthly_fee, flow_size, condition)
    VALUES (?, ?, ?, ?, ?)
    ''', record)

# 提交事务
conn.commit()
python 复制代码
def ask_database(query):
    cursor.execute(query)
    records = cursor.fetchall()
    return records


prompt = "请问流量最大的套餐是哪个?"
# prompt = "统计每月每件商品的销售额"
# prompt = "哪个用户消费最高?消费多少?"

messages = [
    {"role": "system", "content": "基于 packages 表回答用户问题"},
    {"role": "user", "content": prompt}
]
print("====messages====")
print_json(messages)

response = get_sql_completion(messages)
# print("====first Function Calling====")
# print_json(response)

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)
相关推荐
智算菩萨36 分钟前
从对话演示到智能工作平台:ChatGPT的三年演进史(2022-2025)
人工智能·chatgpt
我送炭你添花8 小时前
Pelco KBD300A 模拟器:03.Pelco-P 协议 8 字节完整拆解 + 与 Pelco-D 一一对应终极对照表
python·测试工具·运维开发
R.lin8 小时前
Java 8日期时间API完全指南
java·开发语言·python
TGITCIC8 小时前
RAG中的语义理解与语义检索:别再混为一谈
llm·rag·ai agent·ai智能体·ai产品·大模型ai·rag增强检索
智算菩萨8 小时前
AIGC Bar中的API站最新使用全指南(2025/12/12)
aigc
西南胶带の池上桜8 小时前
1.Pytorch模型应用(线性与非线性预测)
人工智能·pytorch·python
杀生丸学AI9 小时前
【无标题】VGGT4D:用于4D场景重建的视觉Transformer运动线索挖掘
人工智能·深度学习·3d·aigc·transformer·三维重建·视觉大模型
墨风如雪9 小时前
告别AI塑料感:阿里Qwen3-Omni-Flash要把大模型做成真人
aigc
小和尚同志9 小时前
还在手动配置?这款开源软件让你一键配置 Claude Code 和 Codex
人工智能·aigc
致Great9 小时前
Ollama 进阶指南
人工智能·gpt·chatgpt·agent·智能体