mirror of
https://github.com/anthropics/claude-cookbooks.git
synced 2025-10-06 01:00:28 +03:00
257 lines
6.2 KiB
Plaintext
257 lines
6.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# How to make SQL queries with Claude\n",
|
|
"\n",
|
|
"\n",
|
|
"In this notebook, we'll explore how to use Claude to generate SQL queries based on natural language questions. We'll set up a test database, provide the schema to Claude, and demonstrate how it can understand and translate human language into SQL queries."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Setup\n",
|
|
"\n",
|
|
"First, let's install the necessary libraries and setup our Anthropic client with our API key."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Install the necessary libraries\n",
|
|
"%pip install anthropic"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Import the required libraries\n",
|
|
"from anthropic import Anthropic\n",
|
|
"import sqlite3\n",
|
|
"\n",
|
|
"# Set up the Claude API client\n",
|
|
"client = Anthropic()\n",
|
|
"MODEL_NAME = \"claude-3-opus-20240229\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Creating a Test Database\n",
|
|
"\n",
|
|
"We'll create a test database using SQLite and populate it with sample data:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Connect to the test database (or create it if it doesn't exist)\n",
|
|
"conn = sqlite3.connect(\"test_db.db\")\n",
|
|
"cursor = conn.cursor()\n",
|
|
"\n",
|
|
"# Create a sample table\n",
|
|
"cursor.execute(\"\"\"\n",
|
|
" CREATE TABLE IF NOT EXISTS employees (\n",
|
|
" id INTEGER PRIMARY KEY,\n",
|
|
" name TEXT,\n",
|
|
" department TEXT,\n",
|
|
" salary INTEGER\n",
|
|
" )\n",
|
|
"\"\"\")\n",
|
|
"\n",
|
|
"# Insert sample data\n",
|
|
"sample_data = [\n",
|
|
" (1, \"John Doe\", \"Sales\", 50000),\n",
|
|
" (2, \"Jane Smith\", \"Engineering\", 75000),\n",
|
|
" (3, \"Mike Johnson\", \"Sales\", 60000),\n",
|
|
" (4, \"Emily Brown\", \"Engineering\", 80000),\n",
|
|
" (5, \"David Lee\", \"Marketing\", 55000)\n",
|
|
"]\n",
|
|
"cursor.executemany(\"INSERT INTO employees VALUES (?, ?, ?, ?)\", sample_data)\n",
|
|
"conn.commit()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Generating SQL Queries with Claude\n",
|
|
"\n",
|
|
"Now, let's define a function to send a natural language question to Claude and get the generated SQL query:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Define a function to send a query to Claude and get the response\n",
|
|
"def ask_claude(query, schema):\n",
|
|
" prompt = f\"\"\"Here is the schema for a database:\n",
|
|
"\n",
|
|
"{schema}\n",
|
|
"\n",
|
|
"Given this schema, can you output a SQL query to answer the following question? Only output the SQL query and nothing else.\n",
|
|
"\n",
|
|
"Question: {query}\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
" response = client.messages.create(\n",
|
|
" model=MODEL_NAME,\n",
|
|
" max_tokens=2048,\n",
|
|
" messages=[{\n",
|
|
" \"role\": 'user', \"content\": prompt\n",
|
|
" }]\n",
|
|
" )\n",
|
|
" return response.content[0].text"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We'll retrieve the database schema and format it as a string:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"CREATE TABLE EMPLOYEES (\n",
|
|
"id INTEGER\n",
|
|
"name TEXT\n",
|
|
"department TEXT\n",
|
|
"salary INTEGER\n",
|
|
")\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Get the database schema\n",
|
|
"schema = cursor.execute(\"PRAGMA table_info(employees)\").fetchall()\n",
|
|
"schema_str = \"CREATE TABLE EMPLOYEES (\\n\" + \"\\n\".join([f\"{col[1]} {col[2]}\" for col in schema]) + \"\\n)\"\n",
|
|
"print(schema_str)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now, let's provide an example natural language question and send it to Claude:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"SELECT name, salary\n",
|
|
"FROM EMPLOYEES\n",
|
|
"WHERE department = 'Engineering';\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Example natural language question\n",
|
|
"question = \"What are the names and salaries of employees in the Engineering department?\"\n",
|
|
"# Send the question to Claude and get the SQL query\n",
|
|
"sql_query = ask_claude(question, schema_str)\n",
|
|
"print(sql_query)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Executing the Generated SQL Query\n",
|
|
"\n",
|
|
"Finally, we'll execute the generated SQL query on our test database and print the results:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"('Jane Smith', 75000)\n",
|
|
"('Emily Brown', 80000)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Execute the SQL query and print the results\n",
|
|
"results = cursor.execute(sql_query).fetchall()\n",
|
|
"\n",
|
|
"for row in results:\n",
|
|
" print(row)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Don't forget to close the database connection when you're done:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Close the database connection\n",
|
|
"conn.close()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|