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

# SQLToolkit 集成

> 通过LangChain JavaScript与SQLToolkit工具集成。

这将帮助你开始使用 `SqlToolkit` [工具包](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md#预构建工具)。如需更多信息，你也可以查看 Python [`SQLDatabaseToolkit`](https://github.com/langchain-ai/langchain-community/blob/main/libs/community/langchain_community/agent_toolkits/sql/toolkit.py)。

此工具包包含以下工具：

| 名称 | 描述 |
| - | - |
| `query-sql` | 此工具的输入是一个详细且正确的 SQL 查询，输出是来自数据库的结果。如果查询不正确，将返回错误消息。如果返回错误，请重写查询、检查查询并再次尝试。 |
| `info-sql` | 此工具的输入是一个逗号分隔的表列表，输出是这些表的架构和示例行。务必先调用 list-tables-sql 来确保表实际存在！示例输入： "table1, table2, table3"。 |
| `list-tables-sql` | 输入是一个空字符串，输出是数据库中表的逗号分隔列表。 |
| `query-checker` | 在执行查询之前，使用此工具来双重检查你的查询是否正确。在执行 query-sql 查询之前，始终使用此工具！ |

此工具包适用于在 SQL 数据库上提问、执行查询、验证查询等。

## 设置

此示例使用 Chinook 数据库，这是一个适用于 SQL Server、Oracle、MySQL 等的示例数据库。要设置它，请按照 [这些说明](https://database.guide/2-sample-databases-sqlite/) 操作，将 `.db` 文件放在你的代码所在的目录中。

如果你想在单个工具的运行中获得自动跟踪，你也可以通过取消注释以下内容来设置你的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```typescript theme={null}
process.env.LANGSMITH_TRACING="true"
process.env.LANGSMITH_API_KEY="your-api-key"
```

### 安装

此工具包位于 `langchain` 包中。你还需要安装 `typeorm` 依赖项。

```bash npm theme={null}
npm install langchain @langchain/core typeorm
```

```bash yarn theme={null}
yarn add langchain @langchain/core typeorm
```

```bash pnpm theme={null}
pnpm add langchain @langchain/core typeorm
```

## 实例化

首先，我们需要定义要在工具包中使用的 LLM。

```typescript theme={null}
// @lc-docs-hide-cell

import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "gpt-5.4-mini",
  temperature: 0,
})
```

```typescript theme={null}
import { SqlToolkit } from "@langchain/classic/agents/toolkits/sql"
import { DataSource } from "typeorm";
import { SqlDatabase } from "@langchain/classic/sql_db";

const datasource = new DataSource({
  type: "sqlite",
  database: "../../../../../../Chinook.db", // Replace with the link to your database
});
const db = await SqlDatabase.fromDataSourceParams({
  appDataSource: datasource,
});

const toolkit = new SqlToolkit(db, llm);
```

## 工具

查看可用工具：

```typescript theme={null}
const tools = toolkit.getTools();

console.log(tools.map((tool) => ({
  name: tool.name,
  description: tool.description,
})))
```

```text theme={null}
[
  {
    name: 'query-sql',
    description: 'Input to this tool is a detailed and correct SQL query, output is a result from the database.\n' +
      '  If the query is not correct, an error message will be returned.\n' +
      '  If an error is returned, rewrite the query, check the query, and try again.'
  },
  {
    name: 'info-sql',
    description: 'Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\n' +
      '    Be sure that the tables actually exist by calling list-tables-sql first!\n' +
      '\n' +
      '    Example Input: "table1, table2, table3.'
  },
  {
    name: 'list-tables-sql',
    description: 'Input is an empty string, output is a comma-separated list of tables in the database.'
  },
  {
    name: 'query-checker',
    description: 'Use this tool to double check if your query is correct before executing it.\n' +
      '    Always use this tool before executing a query with query-sql!'
  }
]
```

## 在智能体中使用

首先，确保你已经安装了 LangGraph：

```bash npm theme={null}
npm install @langchain/langgraph
```

```bash yarn theme={null}
yarn add @langchain/langgraph
```

```bash pnpm theme={null}
pnpm add @langchain/langgraph
```

```typescript theme={null}
import { createAgent } from "@langchain/classic"

const agentExecutor = createAgent({ llm, tools });
```

```typescript theme={null}
const exampleQuery = "Can you list 10 artists from my database?"

const stream = await agentExecutor.streamEvents(
  { messages: [["user", exampleQuery]] },
  { version: "v3" },
);

for await (const snapshot of stream.values) {
  const lastMsg = snapshot.messages[snapshot.messages.length - 1];
  if (lastMsg.tool_calls?.length) {
    console.dir(lastMsg.tool_calls, { depth: null });
  } else if (lastMsg.content) {
    console.log(lastMsg.content);
  }
}
```

```json theme={null}
[
  {
    name: 'list-tables-sql',
    args: {},
    type: 'tool_call',
    id: 'call_LqsRA86SsKmzhRfSRekIQtff'
  }
]
Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track
[
  {
    name: 'query-checker',
    args: { input: 'SELECT * FROM Artist LIMIT 10;' },
    type: 'tool_call',
    id: 'call_MKBCjt4gKhl5UpnjsMHmDrBH'
  }
]
The SQL query you provided is:

\`\`\`sql
SELECT * FROM Artist LIMIT 10;
\`\`\`

This query is straightforward and does not contain any of the common mistakes listed. It simply selects all columns from the `Artist` table and limits the result to 10 rows.

Therefore, there are no mistakes to correct, and the original query can be reproduced as is:

\`\`\`sql
SELECT * FROM Artist LIMIT 10;
\`\`\`
[
  {
    name: 'query-sql',
    args: { input: 'SELECT * FROM Artist LIMIT 10;' },
    type: 'tool_call',
    id: 'call_a8MPiqXPMaN6yjN9i7rJctJo'
  }
]
[{"ArtistId":1,"Name":"AC/DC"},{"ArtistId":2,"Name":"Accept"},{"ArtistId":3,"Name":"Aerosmith"},{"ArtistId":4,"Name":"Alanis Morissette"},{"ArtistId":5,"Name":"Alice In Chains"},{"ArtistId":6,"Name":"Antônio Carlos Jobim"},{"ArtistId":7,"Name":"Apocalyptica"},{"ArtistId":8,"Name":"Audioslave"},{"ArtistId":9,"Name":"BackBeat"},{"ArtistId":10,"Name":"Billy Cobham"}]
Here are 10 artists from your database:

1. AC/DC
2. Accept
3. Aerosmith
4. Alanis Morissette
5. Alice In Chains
6. Antônio Carlos Jobim
7. Apocalyptica
8. Audioslave
9. BackBeat
10. Billy Cobham
```

***


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.