> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL Agent

本文展示了如何构建一个文本到 SQL 系统，该系统：

1. 使用 Agentic RAG 搜索表元数据、示例查询和编写更好 SQL 查询的规则。
2. 使用动态少样本示例和规则来改进查询构建。
3. 提供交互式 Streamlit UI，供用户查询数据库。

我们将以 F1 数据集为例，但您可以轻松将其扩展到其他数据集。

### 主要功能

* 自然语言到 SQL 的转换
* 使用 Agentic RAG 检索表元数据、示例查询和规则
* 在动态少样本示例和规则的帮助下改进查询构建
* 交互式 Streamlit UI

<video autoPlay muted controls className="w-full aspect-video" src="https://mintcdn.com/ikun/fXmeJ7wKTC-4Xav-/videos/sql_agent.mp4?fit=max&auto=format&n=fXmeJ7wKTC-4Xav-&q=85&s=e40f8175964accc27affe9972945a717" data-path="videos/sql_agent.mp4" />

### 简单的查询尝试

* "谁是获得最多比赛胜利的前 5 名车手？"
* "比较梅赛德斯 vs 法拉利在车队总冠军赛中的表现"
* "展示一下蒙扎赛道最快单圈时间的进展"
* "哪些车手曾效力于多个车队并赢得过总冠军？"
* "哪些赛道举办的比赛最多？"
* "展示一下刘易斯·汉密尔顿每个赛季的获胜百分比"

### 高级查询与表连接

* "冠军车手每年赢得了多少场比赛？"
* "比较 2019 年车队的比赛胜利数与车队总冠军数"
* "按年度展示刘易斯·汉密尔顿的比赛胜利数和车队总冠军排名"
* "哪些车手在摩纳哥既赢得过比赛又创造过最快圈速？"
* "展示一下法拉利从 2015 年到 2020 年的比赛胜利数和车队总冠军排名"

## 代码

完整代码可在 [Agno 仓库](https://github.com/agno-agi/agno) 中找到。

## 使用方法

<Steps>
  <Step title="克隆仓库">
    ```bash theme={null}
    git clone https://github.com/agno-agi/agno.git
    cd agno
    ```
  </Step>

  <Step title="创建虚拟环境">
    ```bash theme={null}
    python3 -m venv .venv
    source .venv/bin/activate
    ```
  </Step>

  <Step title="安装依赖">
    ```bash theme={null}
    pip install -r cookbook/examples/streamlit_apps/sql_agent/requirements.txt
    ```
  </Step>

  <Step title="运行 PgVector">
    首先，安装 [Docker Desktop](https://docs.docker.com/desktop/install/mac-install/)。

    然后使用辅助脚本运行：

    ```bash theme={null}
    ./cookbook/scripts/run_pgvector.sh
    ```

    或者直接使用 Docker:

    ```bash theme={null}
    docker run -d \
      -e POSTGRES_DB=ai \
      -e POSTGRES_USER=ai \
      -e POSTGRES_PASSWORD=ai \
      -e PGDATA=/var/lib/postgresql/data/pgdata \
      -v pgvolume:/var/lib/postgresql/data \
      -p 5532:5432 \
      --name pgvector \
      agnohq/pgvector:16
    ```
  </Step>

  <Step title="加载 F1 数据">
    ```bash theme={null}
    python cookbook/examples/streamlit_apps/sql_agent/load_f1_data.py
    ```
  </Step>

  <Step title="加载知识库">
    知识库包含表元数据、规则和示例查询，这些有助于 Agent 生成更好的响应。

    ```bash theme={null}
    python cookbook/examples/streamlit_apps/sql_agent/load_knowledge.py
    ```

    为增强知识库的专业技巧：

    * 添加 `table_rules` 和 `column_rules` 来指导 Agent 的查询格式
    * 将示例查询添加到 `cookbook/examples/apps/sql_agent/knowledge_base/sample_queries.sql`
  </Step>

  <Step title="设置 API 密钥">
    ```bash theme={null}
    # Required
    export OPENAI_API_KEY=***

    # Optional
    export ANTHROPIC_API_KEY=***
    export GOOGLE_API_KEY=***
    export GROQ_API_KEY=***
    ```

    我们推荐使用 gpt-4o 以获得最佳性能。
  </Step>

  <Step title="启动应用">
    ```bash theme={null}
    streamlit run cookbook/examples/streamlit_apps/sql_agent/app.py
    ```

    打开 [localhost:8501](http://localhost:8501) 开始使用 SQL Agent。
  </Step>
</Steps>

需要帮助？加入我们的 [Discourse 社区](https://community.agno.com) 获取支持！
