> ## 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.

# IBM watsonx.ai SQL 集成

> 使用 LangChain Python 集成 IBM watsonx.ai SQL 工具。

本示例展示了如何使用 `langchain-ibm` `watsonx.ai` SQL 数据库工具包，该工具包基于 Flight 服务。

> [!WARNING]
>
> 构建 SQL 数据库问答系统需要执行模型生成的 SQL 查询，这存在固有安全风险。请确保您的数据库连接权限始终根据智能体的需求尽可能严格限制。这将缓解，但无法完全消除，模型驱动型系统的风险。

## 概述

### 集成细节

| 类 | 包 | 可序列化 | [JS 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/javascript/integrations/tools/ibm.md) | 下载量 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`WatsonxSQLDatabaseToolkit`](https://reference.langchain.com/python/langchain-ibm/WatsonxToolkit) | [`langchain-ibm`](https://reference.langchain.com/python/langchain-ibm) | ❌ | ❌ | ![PyPI - 下载量](https://img.shields.io/pypi/dm/langchain-ibm?style=flat-square\&label=%20) | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain-ibm?style=flat-square\&label=%20) |

## 安装

要访问 `langchain-ibm` SQL 数据库工具包，您需要创建 IBM watsonx.ai 账户、获取 API 密钥，并安装 `langchain-ibm` 集成包。

### 凭据

此单元定义了与 watsonx SQL 数据库工具包交互所需的 WML凭据。

**操作步骤：** 提供 IBM Cloud 用户 API 密钥。详情请参阅
[文档](https://cloud.ibm.com/docs/account?topic=account-userapikey\&interface=ui)。

```python theme={null}
import os
from getpass import getpass

watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key
```

此外，您还可以通过环境变量传递额外的密钥。

```python theme={null}
import os

os.environ["WATSONX_URL"] = "your service instance url"
os.environ["WATSONX_TOKEN"] = "your token for accessing the CLOUD or CPD cluster"
os.environ["WATSONX_PASSWORD"] = "your password for accessing the CPD cluster"
os.environ["WATSONX_USERNAME"] = "your username for accessing the CPD cluster"
os.environ["WATSONX_INSTANCE_ID"] = "your instance_id for accessing the CPD cluster"
```

### 安装

LangChain IBM 集成位于 `langchain-ibm` 包中：

```python theme={null}
pip install -qU "langchain-ibm[sql-toolkit]"
```

## 初始化

要设置 SQL 数据库工具包，您必须首先实例化 `WatsonxSQLDatabase` 类，该类通过 Flight SQL 客户端从 IBM watsonx.ai 数据库连接资产中检索必要信息。如需了解如何通过编程创建数据库连接资产，请参阅 watsonx.ai Python SDK [文档](https://ibm.github.io/watsonx-ai-python-sdk/v1.4.7/autoai_working_with_dataconnection.html#connection-asset-databaselocation)。

```python theme={null}
from langchain_ibm.utilities.sql_database import WatsonxSQLDatabase

wx_sql_database = WatsonxSQLDatabase(
    connection_id=postgres_sql_connection_id,
    url="https://us-south.ml.cloud.ibm.com",
    schema="public"
    )
```

另一种方式是使用 Cloud Pak for Data凭据。详情请参阅 [watsonx.ai 软件安装](https://ibm.github.io/watsonx-ai-python-sdk/setup_cpd.html)。

对于特定需求，您可以将 IBM 的 [`APIClient`](https://ibm.github.io/watsonx-ai-python-sdk/base.html#apiclient) 对象传递给 `WatsonxSQLDatabase` 类。

```python theme={null}
from ibm_watsonx_ai import APIClient

api_client = APIClient(...)

wx_sql_database = WatsonxSQLDatabse(
    watsonx_client=api_client,
    connection_id=postgres_sql_connection_id,
    schema="public"
)
```

最后初始化 `WatsonxSQLDatabaseToolkit`。

```python theme={null}
from langchain_ibm import ChatWatsonx

llm = ChatWatsonx(...)

from langchain_ibm.agent_toolkits.sql import WatsonxSQLDatabaseToolkit

wx_sql_toolkit = WatsonxSQLDatabaseToolkit(
    db=wx_sql_database,
    llm=llm,
)
```

## 工具

### 获取所有工具

运行 `.get_tools` 方法，可以获取所有可用工具的列表。

```python theme={null}
wx_sql_toolkit.get_tools()
```

```text theme={null}
[QuerySQLDatabaseTool(description="Input to this tool is a detailed and correct SQL query, output is a result from the database. If the query is not correct, an error message will be returned. If an error is returned, rewrite the query, check the query, and try again. If you encounter an issue with Unknown column 'xxxx' in 'field list', use sql_db_schema to query the correct table fields.", db=<langchain_ibm.utilities.sql_database.WatsonxSQLDatabase object at 0x111c03810>),
 InfoSQLDatabaseTool(description='Input to this tool is a comma-separated list of tables, output is the SQL statement with table metadata. Be sure that the tables actually exist by calling sql_db_list_tables first! Example Input: table1, table2, table3', db=<langchain_ibm.utilities.sql_database.WatsonxSQLDatabase object at 0x111c03810>),
 ListSQLDatabaseTool(db=<langchain_ibm.utilities.sql_database.WatsonxSQLDatabase object at 0x111c03810>),
 QuerySQLCheckerTool(description='Use this tool to double check if your query is correct before executing it. Always use this tool before executing a query with sql_db_query!', db=<langchain_ibm.utilities.sql_database.WatsonxSQLDatabase object at 0x111c03810>, llm=ChatWatsonx(model_id='ibm/granite-3-3-8b-instruct', apikey=SecretStr('**********'), params={}, watsonx_model=<ibm_watsonx_ai.foundation_models.inference.model_inference.ModelInference object at 0x12aa12ed0>, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x12aa12ad0>), llm_chain=PromptTemplate(input_variables=['query', 'schema'], input_types={}, partial_variables={}, template='\n{query}\nDouble check the query above for common mistakes, including:\n- Using NOT IN with NULL values\n- Using UNION when UNION ALL should have been used\n- Using BETWEEN for exclusive ranges\n- Data type mismatch in predicates\n- Properly quoting identifiers\n- Using the correct number of arguments for functions\n- Casting to the correct data type\n- Using the proper columns for joins\n- Make sure that schema name `{schema}` is added to the table name, e.g. {schema}.table1\n\nIf there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.\n\nOutput the final SQL query only.\n\nSQL Query: ')
 | ChatWatsonx(model_id='ibm/granite-3-3-8b-instruct', apikey=SecretStr('**********'), params={}, watsonx_model=<ibm_watsonx_ai.foundation_models.inference.model_inference.ModelInference object at 0x12aa12ed0>, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x12aa12ad0>))]
```

您可以直接使用单个工具：

```python theme={null}
from langchain_ibm.agent_toolkits.sql.tool import (
    InfoSQLDatabaseTool,
    ListSQLDatabaseTool,
    QuerySQLDatabaseTool,
    QuerySQLCheckerTool
)
```

## 在智能体中使用

```python theme={null}
from langchain_ibm import ChatWatsonx

llm = ChatWatsonx(
    model_id="meta-llama/llama-3-3-70b-instruct",
    url="https://us-south.ml.cloud.ibm.com",
    project_id="PASTE YOUR PROJECT_ID HERE",
)
```

```python theme={null}
from langchain.agents import create_agent

# Use one of the prompt from langchain hub
from langchain_classic import hub

prompt_template = hub.pull("langchain-ai/sql-agent-system-prompt")
system_message = prompt_template.format(dialect="PostgreSQL", top_k=5)

agent_executor = create_agent(llm, wx_sql_toolkit.get_tools(), system_prompt=system_message)
```

```python theme={null}
example_query = "What city has the highest population?"

stream = agent.stream_events(
    {"messages": [("user", example_query)]},
    version="v3",
)
for snapshot in stream.values:
    snapshot["messages"][-1].pretty_print()
```

```text theme={null}
================================[1m Human Message [0m=================================

What city has the largest population?
==================================[1m Ai Message [0m==================================
Tool Calls:
  sql_db_query (chatcmpl-tool-336c8be3bce64f44a7977c3053f36922)
 Call ID: chatcmpl-tool-336c8be3bce64f44a7977c3053f36922
  Args:
    query: SELECT city FROM cities ORDER BY population DESC LIMIT 1
=================================[1m Tool Message [0m=================================
Name: sql_db_query

Error: Flight returned internal error, with message: CDICO2021E: Statement could not be processed: SQL syntax error: [IBM][PostgreSQL JDBC Driver][PostgreSQL]column "city" does not exist. . The statement text is: SELECT city FROM cities ORDER BY population DESC LIMIT 1
==================================[1m Ai Message [0m==================================
Tool Calls:
  sql_db_schema (chatcmpl-tool-2dc38420280c4960b13b0e5533634fd3)
 Call ID: chatcmpl-tool-2dc38420280c4960b13b0e5533634fd3
  Args:
    table_names: cities
=================================[1m Tool Message [0m=================================
Name: sql_db_schema


CREATE TABLE "public"."cities" (
	"id" INTEGER NOT NULL,
	"name" CHARACTER VARYING,
	"population" INTEGER,
	CONSTRAINT primary_key PRIMARY KEY (id)
	)

First 3 rows of table cities:

 id   name  population
  1 Warsaw     1790658
  2 Kraków      803425
  3   Łódź      658444
==================================[1m Ai Message [0m==================================
Tool Calls:
  sql_db_query_checker (chatcmpl-tool-1ecbb02ce7ac47e186edbfff144eacdc)
 Call ID: chatcmpl-tool-1ecbb02ce7ac47e186edbfff144eacdc
  Args:
    query: SELECT name FROM cities ORDER BY population DESC LIMIT 1
=================================[1m Tool Message [0m=================================
Name: sql_db_query_checker

<p>The given query</p><div><div><div>SELECT name FROM cities ORDER BY population DESC LIMIT 1</div></div></div><p>appears to be syntactically correct but lacks schema specification if the table 'cities' is not in the default 'public' schema. Assuming it's under 'public', here's the complete query with schema specification:</p><div><div><div>SELECT name FROM public.cities ORDER BY population DESC LIMIT 1</div></div></div>
==================================[1m Ai Message [0m==================================
Tool Calls:
  sql_db_query (chatcmpl-tool-d8e0eff8b07b40f593dc1b97e6280598)
 Call ID: chatcmpl-tool-d8e0eff8b07b40f593dc1b97e6280598
  Args:
    query: SELECT name FROM public.cities ORDER BY population DESC LIMIT 1
=================================[1m Tool Message [0m=================================
Name: sql_db_query

[('Warsaw',)]
==================================[1m Ai Message [0m==================================

The city with the largest population is Warsaw.
```

***

## API 参考

要查看 `WatsonxSQLDatabaseToolkit` 所有功能和使用配置的详细文档，请访问 [API 参考](https://reference.langchain.com/python/langchain-ibm/WatsonxSQLDatabaseToolkit)。

***

> 官方原文：[src/oss/python/integrations/tools/ibm\_watsonx\_sql.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/tools/ibm_watsonx_sql.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


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