How to write sql query using chatGTP function calling, code attached!

When working with databases, querying for specific data is crucial. Instead of manually writing SQL queries, what if you could simply ask a natural language question to get the data you need? With ChatGPT’s function calling capabilities, we can do this!

Let’s see how to set up a system that uses ChatGPT to generate SQL queries for us, using the Chinook sample database as an example.

1. Connect to the database

First, we need to establish a connection to the SQLite database.

import sqlite3

conn = sqlite3.connect("data/Chinook.db")
print("成功打开数据库")

2. Extract database schema

In order to make informed queries, ChatGPT needs to understand the structure of our database. We can do this by creating utility functions to extract table names, column names, and overall database information.

def get_table_names(conn):
    table_names = []
    tables = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
    for table in tables.fetchall():
        table_names.append(table[0])
    return table_names

def get_column_names(conn, table_name):
    column_names = []
    columns = conn.execute(f"PRAGMA table_info('{table_name}');").fetchall()
    for col in columns:
        column_names.append(col[1])
    return column_names

def get_database_info(conn):
    table_dicts = []
    for table_name in get_table_names(conn):
        columns_names = get_column_names(conn, table_name)
        table_dicts.append({"table_name": table_name, "column_names": columns_names})
    return table_dicts

Using these functions, you can now generate a schema representation for your database.

3. Define function specifications for ChatGPT

With the database schema in place, we can define a function specification for ChatGPT. This will give the model context about the structure of our database and tell it how to generate SQL queries.

database_schema_dict = get_database_info(conn)
database_schema_string = "\n".join(
    [
        f"Table: {table['table_name']}\nColumns: {', '.join(table['column_names'])}"
        for table in database_schema_dict
    ]
)

functions = [
    {
        "name": "ask_database",
        "description": "使用此功能回答用户关于音乐的问题。输入应该是一个完整的SQL查询。",
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": f"使用此数据库架构提取信息的SQL查询:{database_schema_string}。查询应以纯文本返回,而不是JSON。"
                }
            },
            "required": ["query"],
        },
    }
]

4. Execute SQL query

Next, we need a function to execute the generated SQL query against our database.

def ask_database(conn, query):
    try:
        results = str(conn.execute(query).fetchall())
    except Exception as e:
        results = f"查询失败,错误为:{e}"
    return results

5. ChatGPT interaction and SQL query execution

Now, using the ChatGPT API, we can interact with the model. When we get a response from the model to a function call, we can execute the SQL query and return the results.

messages = []
messages.append({"role": "system", "content": "通过生成针对Chinook音乐数据库的SQL查询来回答用户问题。"})
messages.append({"role": "user", "content": "你好,哪5位艺术家的歌曲数量最多?"})

chat_response = chat_completion_request(messages, functions)
assistant_message = chat_response.json()["choices"][0]["message"]
messages.append(assistant_message)

if assistant_message.get("function_call"):
    results = execute_function_call(assistant_message)
    messages.append({"role": "function", "name": assistant_message["function_call"]["name"], "content": results})

in conclusion

By combining ChatGPT with database interaction, we show how to generate SQL queries using natural language questions. This approach allows for more intuitive retrieval of data, especially for users who may not be familiar with SQL syntax. However, always make sure to validate and clean generated queries, especially when used in a production environment, to maintain data integrity and security.

code

See the github code ink at the end of the original text.

Recommended AI books

AI is changing with each passing day, but tall buildings cannot be built without a good foundation. Are you interested in learning about the principles and practices of artificial intelligence? Look no further! Our book on AI principles and practices is the perfect resource for anyone who wants to learn more about the world of AI. Written by leading experts in the field, this comprehensive guide covers everything from the basics of machine learning to advanced techniques for building intelligent systems. Whether you are a beginner or an experienced AI practitioner, this book has something for you. So why wait?

Principles and Practices of Artificial Intelligence Comprehensive coverage of classics on all important systems of artificial intelligence and data science

Peking University Press, Artificial Intelligence Principles and Practices Artificial Intelligence and Data Science from Beginner to Mastery Detailed explanation of the principles of machine learning and deep learning algorithms

Guess you like

Origin blog.csdn.net/robot_learner/article/details/133341404