{"id":354,"date":"2025-10-21T07:00:00","date_gmt":"2025-10-20T23:00:00","guid":{"rendered":"https:\/\/smartaiunion.com\/?p=354"},"modified":"2025-10-20T14:01:24","modified_gmt":"2025-10-20T06:01:24","slug":"%e4%bd%bf%e7%94%a8-openai-fastapi-sqlite-%e6%9e%84%e5%bb%ba%e6%96%87%e6%9c%ac%e8%bd%ac-sql-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/smartaiunion.com\/index.php\/2025\/10\/21\/354\/","title":{"rendered":"\u4f7f\u7528 OpenAI + FastAPI + SQLite \u6784\u5efa\u6587\u672c\u8f6c SQL \u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5f15\u8a00<\/h2>\n\n\n\n<p>\u6570\u636e\u5df2\u6210\u4e3a\u4efb\u4f55\u6210\u529f\u4f01\u4e1a\u4e0d\u53ef\u6216\u7f3a\u7684\u8d44\u6e90\uff0c\u5b83\u80fd\u4e3a\u660e\u667a\u7684\u51b3\u7b56\u5236\u5b9a\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u89c1\u89e3\u3002\u9274\u4e8e\u6570\u636e\u7684\u91cd\u8981\u6027\uff0c\u8bb8\u591a\u516c\u53f8\u90fd\u5728\u6784\u5efa\u7528\u4e8e\u5b58\u50a8\u548c\u5206\u6790\u6570\u636e\u7684\u7cfb\u7edf\u3002\u7136\u800c\uff0c\u5f88\u591a\u65f6\u5019\u83b7\u53d6\u548c\u5206\u6790\u6240\u9700\u6570\u636e\u5e76\u975e\u6613\u4e8b\uff0c\u5c24\u5176\u662f\u968f\u7740\u6570\u636e\u7cfb\u7edf\u7684\u65e5\u76ca\u590d\u6742\u3002<\/p>\n\n\n\n<p>\u968f\u7740\u751f\u6210\u5f0f\u4eba\u5de5\u667a\u80fd\u7684\u51fa\u73b0\uff0c\u6570\u636e\u5904\u7406\u5de5\u4f5c\u53d8\u5f97\u8f7b\u677e\u4e86\u8bb8\u591a \u2014\u2014 \u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u81ea\u7136\u8bed\u8a00\uff0c\u5c31\u80fd\u83b7\u5f97\u57fa\u672c\u51c6\u786e\u4e14\u4e0e\u8f93\u5165\u9ad8\u5ea6\u5951\u5408\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u8fd9\u4e00\u70b9\u4e5f\u9002\u7528\u4e8e\u4f7f\u7528 SQL \u8fdb\u884c\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u81ea\u7136\u8bed\u8a00\u6765\u8bf7\u6c42\u751f\u6210\u67e5\u8be2\u8bed\u53e5\u3002<\/p>\n\n\n\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5f00\u53d1\u4e00\u4e2a\u7b80\u6613\u7684 API \u5e94\u7528\u7a0b\u5e8f\uff0c\u5b83\u80fd\u5c06\u81ea\u7136\u8bed\u8a00\u8f6c\u6362\u4e3a\u6570\u636e\u5e93\u53ef\u7406\u89e3\u7684 SQL \u67e5\u8be2\u8bed\u53e5\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u4e09\u4e2a\u4e3b\u8981\u5de5\u5177\uff1aOpenAI\u3001FastAPI \u548c SQLite\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5177\u4f53\u89c4\u5212\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6587\u672c\u8f6c SQL \u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1<\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u9879\u76ee\u6240\u9700\u7684\u6240\u6709\u5185\u5bb9\u3002\u4f60\u53ea\u9700\u63d0\u4f9b OpenAI API \u5bc6\u94a5\uff0c\u6211\u4eec\u5c06\u7528\u5b83\u6765\u8bbf\u95ee\u751f\u6210\u5f0f\u6a21\u578b\u3002\u4e3a\u4e86\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u5bb9\u5668\u5316\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Docker\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 Docker Desktop \u5728\u672c\u5730\u73af\u5883\u4e2d\u83b7\u53d6\u5b83\u3002<\/p>\n\n\n\n<p>\u5176\u4ed6\u7ec4\u4ef6\uff08\u5982 SQLite\uff09\u5728\u5b89\u88c5 Python \u65f6\u4f1a\u81ea\u52a8\u53ef\u7528\uff0c\u800c FastAPI \u5c06\u5728\u540e\u7eed\u6b65\u9aa4\u4e2d\u5b89\u88c5\u3002<\/p>\n\n\n\n<p>\u9879\u76ee\u7684\u6574\u4f53\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text_to_sql_app\/\n\u251c\u2500\u2500 app\/\n\u2502   \u251c\u2500\u2500 __init__.py          \n\u2502   \u251c\u2500\u2500 database.py           \n\u2502   \u251c\u2500\u2500 openai_utils.py       \n\u2502   \u2514\u2500\u2500 main.py               \n\u251c\u2500\u2500 demo.db                   \n\u251c\u2500\u2500 init_db.sql               \n\u251c\u2500\u2500 requirements.txt          \n\u251c\u2500\u2500 Dockerfile                \n\u251c\u2500\u2500 docker-compose.yml        \n\u251c\u2500\u2500 .env\n<\/code><\/pre>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u6309\u7167\u4e0a\u8ff0\u7ed3\u6784\u521b\u5efa\u9879\u76ee\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u4ed3\u5e93\u7b80\u5316\u64cd\u4f5c\u3002\u6211\u4eec\u4ecd\u4f1a\u9010\u4e00\u8bb2\u89e3\u6bcf\u4e2a\u6587\u4ef6\uff0c\u5e2e\u52a9\u4f60\u7406\u89e3\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u5728<code>.env<\/code>\u6587\u4ef6\u4e2d\u586b\u5165\u4e4b\u524d\u83b7\u53d6\u7684 OpenAI API \u5bc6\u94a5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OPENAI_API_KEY=\u4f60\u7684API\u5bc6\u94a5\n<\/code><\/pre>\n\n\n\n<p>\u63a5\u7740\uff0c\u5728<code>requirements.txt<\/code>\u4e2d\u586b\u5199\u9879\u76ee\u6240\u9700\u7684\u5e93\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fastapi\nuvicorn\nsqlalchemy\nopenai\npydantic\npython-dotenv\n<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u7f16\u5199<code>__init__.py<\/code>\u6587\u4ef6\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pathlib import Path\nfrom dotenv import load_dotenv\n\nload_dotenv(dotenv_path=Path(__file__).resolve().parent.parent \/ \".env\", override=False)\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u786e\u4fdd\u73af\u5883\u4e2d\u5305\u542b\u6240\u6709\u5fc5\u9700\u7684\u5bc6\u94a5\u3002<\/p>\n\n\n\n<p>\u4e4b\u540e\uff0c\u5728<code>database.py<\/code>\u6587\u4ef6\u4e2d\u7f16\u5199 Python \u4ee3\u7801\uff0c\u7528\u4e8e\u8fde\u63a5\u540e\u7eed\u521b\u5efa\u7684 SQLite \u6570\u636e\u5e93\uff08\u540d\u4e3a<code>demo.db<\/code>\uff09\u5e76\u63d0\u4f9b\u6267\u884c SQL \u67e5\u8be2\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sqlalchemy import create_engine, text\nfrom sqlalchemy.orm import Session\n\nENGINE = create_engine(\"sqlite:\/\/\/demo.db\", future=True, echo=False)\n\ndef run_query(sql: str) -&gt; list&#91;dict]:\n    with Session(ENGINE) as session:\n        rows = session.execute(text(sql)).mappings().all()\n    return &#91;dict(r) for r in rows]\n<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u51c6\u5907<code>openai_utils.py<\/code>\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5c06\u63a5\u6536\u6570\u636e\u5e93\u6a21\u5f0f\u548c\u8f93\u5165\u7684\u95ee\u9898\uff0c\u8f93\u51fa\u5305\u542b SQL \u67e5\u8be2\u7684 JSON\uff08\u5e76\u6dfb\u52a0\u9632\u62a4\u673a\u5236\u4ee5\u9632\u6b62\u4efb\u4f55\u5199\u5165\u64cd\u4f5c\uff09\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport json\nfrom openai import OpenAI        \n\nclient = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n\n_SYSTEM_PROMPT = \"\"\"\n\u4f60\u9700\u8981\u5c06\u81ea\u7136\u8bed\u8a00\u95ee\u9898\u8f6c\u6362\u4e3a\u53ea\u8bfb\u7684SQLite SQL\u8bed\u53e5\u3002\n\u7981\u6b62\u8f93\u51faINSERT \/ UPDATE \/ DELETE\u8bed\u53e5\u3002\n\u8fd4\u56de\u683c\u5f0f\u4e3aJSON\uff1a{ \"sql\": \"...\" }\u3002\n\"\"\"\n\ndef text_to_sql(question: str, schema: str) -&gt; str:\n    response = client.chat.completions.create(\n        model=\"gpt-4o-mini\",        \n        temperature=0.1,\n        response_format={\"type\": \"json_object\"},\n        messages=&#91;\n            {\"role\": \"system\", \"content\": _SYSTEM_PROMPT},\n            {\"role\": \"user\",\n             \"content\": f\"\u6570\u636e\u5e93\u6a21\u5f0f:\\n{schema}\\n\\n\u95ee\u9898: {question}\"}\n        ]\n    )\n    payload = json.loads(response.choices&#91;0].message.content)\n    return payload&#91;\"sql\"]\n<\/code><\/pre>\n\n\n\n<p>\u5b8c\u6210\u4ee3\u7801\u7f16\u5199\u548c\u8fde\u63a5\u914d\u7f6e\u540e\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 FastAPI \u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u3002\u8be5\u5e94\u7528\u7a0b\u5e8f\u5c06\u63a5\u6536\u81ea\u7136\u8bed\u8a00\u95ee\u9898\u548c\u6570\u636e\u5e93\u6a21\u5f0f\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a SQL \u7684<code>SELECT<\/code>\u67e5\u8be2\u8bed\u53e5\uff0c\u5728 SQLite \u6570\u636e\u5e93\u4e2d\u6267\u884c\u8be5\u67e5\u8be2\uff0c\u5e76\u4ee5 JSON \u683c\u5f0f\u8fd4\u56de\u7ed3\u679c\u3002\u8fd9\u662f\u4e00\u4e2a\u53ef\u901a\u8fc7\u547d\u4ee4\u884c\u754c\u9762\uff08CLI\uff09\u8bbf\u95ee\u7684 API\u3002<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from fastapi import FastAPI, HTTPException\nfrom pydantic import BaseModel\nfrom sqlalchemy import inspect\nfrom .database import ENGINE, run_query\nfrom .openai_utils import text_to_sql\n\napp = FastAPI(title=\"\u6587\u672c\u8f6cSQL\u6f14\u793a\")\n\nclass NLRequest(BaseModel):\n    question: str\n\n@app.on_event(\"startup\")\ndef capture_schema() -&gt; None:\n    insp = inspect(ENGINE)\n    global SCHEMA_STR\n    SCHEMA_STR = \"\\n\".join(\n        f\"CREATE TABLE {t} ({', '.join(c&#91;'name'] for c in insp.get_columns(t))});\"\n        for t in insp.get_table_names()\n    )\n\n@app.post(\"\/query\")\ndef query(req: NLRequest):\n    try:\n        sql = text_to_sql(req.question, SCHEMA_STR)\n        if not sql.lstrip().lower().startswith(\"select\"):\n            raise ValueError(\"\u4ec5\u5141\u8bb8SELECT\u8bed\u53e5\")\n        return {\"sql\": sql, \"result\": run_query(sql)}\n    except Exception as e:\n        raise HTTPException(status_code=400, detail=str(e))\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e3b\u5e94\u7528\u7a0b\u5e8f\u7684\u5168\u90e8\u5185\u5bb9\u3002\u63a5\u4e0b\u6765\u9700\u8981\u51c6\u5907\u6570\u636e\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u4f5c\u4e3a<code>init_db.sql<\/code>\u6587\u4ef6\u7684\u793a\u4f8b\uff08\u4e5f\u53ef\u6839\u636e\u9700\u6c42\u81ea\u884c\u4fee\u6539\uff09\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP TABLE IF EXISTS order_items;\nDROP TABLE IF EXISTS orders;\nDROP TABLE IF EXISTS payments;\nDROP TABLE IF EXISTS products;\nDROP TABLE IF EXISTS customers;\n\nCREATE TABLE customers (\n    id INTEGER PRIMARY KEY,\n    name TEXT NOT NULL,\n    country TEXT,\n    signup_date DATE\n);\n\nCREATE TABLE products (\n    id INTEGER PRIMARY KEY,\n    name TEXT NOT NULL,\n    category TEXT,\n    price REAL\n);\n\nCREATE TABLE orders (\n    id INTEGER PRIMARY KEY,\n    customer_id INTEGER,\n    order_date DATE,\n    total REAL,\n    FOREIGN KEY (customer_id) REFERENCES customers(id)\n);\n\nCREATE TABLE order_items (\n    order_id INTEGER,\n    product_id INTEGER,\n    quantity INTEGER,\n    unit_price REAL,\n    PRIMARY KEY (order_id, product_id),\n    FOREIGN KEY (order_id) REFERENCES orders(id),\n    FOREIGN KEY (product_id) REFERENCES products(id)\n);\n\nCREATE TABLE payments (\n    id INTEGER PRIMARY KEY,\n    order_id INTEGER,\n    payment_date DATE,\n    amount REAL,\n    method TEXT,\n    FOREIGN KEY (order_id) REFERENCES orders(id)\n);\n\nINSERT INTO customers (id, name, country, signup_date) VALUES\n (1,'\u7231\u4e3d\u4e1d','\u7f8e\u56fd','2024-01-05'),\n (2,'\u9c8d\u52c3','\u82f1\u56fd','2024-03-10'),\n (3,'\u5d14','\u97e9\u56fd','2024-06-22'),\n (4,'\u8fbe\u62c9','\u5370\u5ea6\u5c3c\u897f\u4e9a','2025-01-15');\n\nINSERT INTO products (id, name, category, price) VALUES\n (1,'\u4e13\u4e1a\u7b14\u8bb0\u672c\u7535\u8111','\u7535\u5b50\u4ea7\u54c1',1500.00),\n (2,'\u964d\u566a\u8033\u673a','\u7535\u5b50\u4ea7\u54c1',300.00),\n (3,'\u7ad9\u7acb\u5f0f\u529e\u516c\u684c','\u5bb6\u5177',450.00),\n (4,'\u4eba\u4f53\u5de5\u5b66\u6905','\u5bb6\u5177',250.00),\n (5,'27\u82f1\u5bf8\u663e\u793a\u5668','\u7535\u5b50\u4ea7\u54c1',350.00);\n\nINSERT INTO orders (id, customer_id, order_date, total) VALUES\n (1,1,'2025-02-01',1850.00),\n (2,2,'2025-02-03',600.00),\n (3,3,'2025-02-05',350.00),\n (4,1,'2025-02-07',450.00);\n\nINSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES\n (1,1,1,1500.00),\n (1,2,1,300.00),\n (1,5,1,350.00),\n (2,3,1,450.00),\n (2,4,1,250.00),\n (3,5,1,350.00),\n (4,3,1,450.00);\n\nINSERT INTO payments (id, order_id, payment_date, amount, method) VALUES\n (1,1,'2025-02-01',1850.00,'\u4fe1\u7528\u5361'),\n (2,2,'2025-02-03',600.00,'\u8d1d\u5b9d'),\n (3,3,'2025-02-05',350.00,'\u4fe1\u7528\u5361'),\n (4,4,'2025-02-07',450.00,'\u94f6\u884c\u8f6c\u8d26');\n<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4e3a\u9879\u76ee\u521b\u5efa SQLite \u6570\u636e\u5e93\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sqlite3 demo.db &lt; init_db.sql\n<\/code><\/pre>\n\n\n\n<p>\u6570\u636e\u5e93\u51c6\u5907\u5c31\u7eea\u540e\uff0c\u521b\u5efa<code>Dockerfile<\/code>\u4ee5\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u5bb9\u5668\u5316\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM python:3.12-slim\nWORKDIR \/code\n\nCOPY requirements.txt .\nRUN pip install --no-cache-dir -r requirements.txt\n\nCOPY . .\n\nCMD &#91;\"uvicorn\", \"app.main:app\", \"--host\", \"0.0.0.0\", \"--port\", \"8000\"]\n<\/code><\/pre>\n\n\n\n<p>\u540c\u65f6\uff0c\u521b\u5efa<code>docker-compose.yml<\/code>\u6587\u4ef6\uff0c\u4ee5\u4fbf\u66f4\u987a\u7545\u5730\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>services:\n  text2sql:\n    build: .\n    env_file: .env    \n    ports:\n      - \"8000:8000\"    \n    restart: unless-stopped\n    volumes:\n      - .\/demo.db:\/code\/demo.db\n<\/code><\/pre>\n\n\n\n<p>\u4e00\u5207\u51c6\u5907\u5c31\u7eea\u540e\uff0c\u542f\u52a8 Docker Desktop \u5e76\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker compose build --no-cache   \ndocker compose up -d \n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u6240\u6709\u6b65\u9aa4\u90fd\u987a\u5229\u5b8c\u6210\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u6570\u636e\u4e2d\u6709\u591a\u5c11\u4f4d\u5ba2\u6237\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X POST \"http:\/\/localhost:8000\/query\" -H \"Content-Type: application\/json\" -d \"{\\\"question\\\":\\\"\u6709\u591a\u5c11\u4f4d\u5ba2\u6237\uff1f\\\"}\"\n<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"sql\":\"SELECT COUNT(*) AS customer_count FROM customers;\",\"result\":&#91;{\"customer_count\":4}]}\n<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c1d\u8bd5\u66f4\u590d\u6742\u7684\u67e5\u8be2\uff0c\u6bd4\u5982\u6bcf\u4f4d\u5ba2\u6237\u7684\u8ba2\u5355\u6570\u91cf\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X POST \"http:\/\/localhost:8000\/query\" -H \"Content-Type: application\/json\" -d \"{\\\"question\\\":\\\"\u6bcf\u4f4d\u5ba2\u6237\u4e0b\u4e86\u591a\u5c11\u8ba2\u5355\uff1f\\\"}\"\n<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"sql\":\"SELECT customer_id, COUNT(*) AS number_of_orders FROM orders GROUP BY customer_id;\",\"result\":&#91;{\"customer_id\":1,\"number_of_orders\":2},{\"customer_id\":2,\"number_of_orders\":1},{\"customer_id\":3,\"number_of_orders\":1}]}\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u6784\u5efa\u57fa\u7840\u6587\u672c\u8f6c SQL \u5e94\u7528\u7a0b\u5e8f\u7684\u5168\u90e8\u6b65\u9aa4\u3002\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4e3a\u5176\u6dfb\u52a0\u524d\u7aef\u754c\u9762\uff0c\u6216\u6839\u636e\u81ea\u8eab\u9700\u6c42\u6784\u5efa\u66f4\u590d\u6742\u7684\u7cfb\u7edf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u6570\u636e\u662f\u6240\u6709\u6570\u636e\u5de5\u4f5c\u7684\u6838\u5fc3\uff0c\u4f01\u4e1a\u4f9d\u9760\u6570\u636e\u5236\u5b9a\u51b3\u7b56\u3002\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u6240\u4f7f\u7528\u7684\u7cfb\u7edf\u8fc7\u4e8e\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u751f\u6210\u5f0f\u4eba\u5de5\u667a\u80fd\u6765\u8f85\u52a9\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528 OpenAI \u6a21\u578b\u3001FastAPI \u548c SQLite \u5f00\u53d1\u7b80\u6613\u7684\u6587\u672c\u8f6c SQL \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/www.kdnuggets.com\/creating-a-text-to-sql-app-with-openai-fastapi-sqlite\">https:\/\/www.kdnuggets.com\/creating-a-text-to-sql-app-with-openai-fastapi-sqlite<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u6570\u636e\u5df2\u6210\u4e3a\u4efb\u4f55\u6210\u529f\u4f01\u4e1a\u4e0d\u53ef\u6216\u7f3a\u7684\u8d44\u6e90\uff0c\u5b83\u80fd\u4e3a\u660e\u667a\u7684\u51b3\u7b56\u5236\u5b9a\u63d0\u4f9b&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":356,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[48,47],"class_list":["post-354","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tools","tag-api","tag-sql"],"_links":{"self":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/comments?post=354"}],"version-history":[{"count":1,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":357,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/posts\/354\/revisions\/357"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/media\/356"}],"wp:attachment":[{"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/media?parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/categories?post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartaiunion.com\/index.php\/wp-json\/wp\/v2\/tags?post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}