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

# Google alloydb for postgresql 集成

> 使用 LangChain Python 集成 Google AlloyDB for PostgreSQL 文档加载器。

> [AlloyDB](https://cloud.google.com/alloydb) 是一种完全托管的关联回归数据库服务，提供高性能、无缝集成和令人印象深刻的可扩展性。AlloyDB 与 PostgreSQL 完全兼容。扩展您的数据库应用程序，利用 AlloyDB 的 LangChain 集成功能构建 AI 驱动的体验。

本笔记本介绍了如何使用 `AlloyDB for PostgreSQL` 通过 `AlloyDBLoader` 类加载文档。

在 [GitHub](https://github.com/googleapis/langchain-google-alloydb-pg-python/) 上了解更多关于该软件包的信息。

[![在 Colab 中打开](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/googleapis/langchain-google-alloydb-pg-python/blob/main/docs/document_loader.ipynb)

## 开始前

要运行此笔记本，您需要执行以下操作：

* [创建 Google Cloud 项目](https://developers.google.com/workspace/guides/create-project)
* [启用 AlloyDB API](https://console.cloud.google.com/flows/enableapi?apiid=alloydb.googleapis.com)
* [创建 AlloyDB 集群和实例。](https://cloud.google.com/alloydb/docs/cluster-create)
* [创建 AlloyDB 数据库。](https://cloud.google.com/alloydb/docs/quickstart/create-and-connect)
* [向数据库添加用户。](https://cloud.google.com/alloydb/docs/database-users/about)

### 🦜🔗 库安装

安装集成库 `langchain-google-alloydb-pg`。

```python theme={null}
pip install -qU  langchain-google-alloydb-pg
```

\*\*Colab 仅限：\*\*取消注释以下单元格以重新启动内核，或使用按钮重新启动内核。对于 Gemini Enterprise Agent Platform Workbench，您可以使用顶部的按钮重新启动终端。

```python theme={null}
# # Automatically restart kernel after installs so that your environment can access the new packages
# import IPython

# app = IPython.Application.instance()
# app.kernel.do_shutdown(True)
```

### 🔐 身份验证

以登录到此笔记本的 IAM 用户身份验证 Google Cloud，以便访问您的 Google Cloud 项目。

* 如果您使用 Colab 运行此笔记本，请使用以下单元格并继续。
* 如果您使用 Gemini Enterprise Agent Platform Workbench，请查看 [Gemini Enterprise Agent Platform Workbench 设置说明](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env)。

```python theme={null}
from google.colab import auth

auth.authenticate_user()
```

### ☁ 设置您的 Google 云项目

设置您的 Google Cloud 项目，以便您可以在本笔记本中利用 Google Cloud 资源。

如果您不知道您的项目 ID，请尝试以下操作：

* 运行 `gcloud config list`。
* 运行 `gcloud projects list`。
* 查看支持页面：[定位项目 ID](https://support.google.com/googleapi/answer/7014113)。

```python theme={null}
# @title Project { display-mode: "form" }
PROJECT_ID = "gcp_project_id"  # @param {type:"string"}

# Set the project id
! gcloud config set project {PROJECT_ID}
```

## 基本用法

### 设置 AlloyDB 数据库变量

在 [AlloyDB 实例页面](https://console.cloud.google.com/alloydb/clusters) 找到您的数据库值。

```python theme={null}
# @title Set Your Values Here { display-mode: "form" }
REGION = "us-central1"  # @param {type: "string"}
CLUSTER = "my-cluster"  # @param {type: "string"}
INSTANCE = "my-primary"  # @param {type: "string"}
DATABASE = "my-database"  # @param {type: "string"}
TABLE_NAME = "vector_store"  # @param {type: "string"}
```

### AlloyDBEngine 连接池

将 AlloyDB 作为向量存储之一的要求和参数是 `AlloyDBEngine` 对象。`AlloyDBEngine` 配置与 AlloyDB 数据库的连接池，使您的应用程序能够成功连接并遵循行业最佳实践。

使用 `AlloyDBEngine.from_instance()` 创建 `AlloyDBEngine` 需要 5 个东西：

1. `project_id`：AlloyDB 实例所在的 Google Cloud 项目的项目 ID。
2. `region`：AlloyDB 实例所在的区域。
3. `cluster`：AlloyDB 集群名称。
4. `instance`：AlloyDB 实例名称。
5. `database`：要连接的 AlloyDB 实例上的数据库名称。

默认情况下，将使用 [IAM 数据库身份验证](https://cloud.google.com/alloydb/docs/connect-iam) 作为数据库身份验证方法。此库使用来自环境的 [应用程序默认凭证 (ADC)](https://cloud.google.com/docs/authentication/application-default-credentials) 所属的 IAM 主机进行身份验证。

也可以使用用户名和密码访问 AlloyDB 数据库的 [内置数据库身份验证](https://cloud.google.com/alloydb/docs/database-users/about)。只需向 `AlloyDBEngine.from_instance()` 提供可选的 `user` 和 `password` 参数：

* `user`：用于内置数据库身份验证和登录的数据库用户。
* `password`：用于内置数据库身份验证和登录的数据库密码。

**注意**：本教程演示了异步接口。所有异步方法都有相应的同步方法。

```python theme={null}
from langchain_google_alloydb_pg import AlloyDBEngine

engine = await AlloyDBEngine.afrom_instance(
    project_id=PROJECT_ID,
    region=REGION,
    cluster=CLUSTER,
    instance=INSTANCE,
    database=DATABASE,
)
```

### 创建 AlloyDBLoader

```python theme={null}
from langchain_google_alloydb_pg import AlloyDBLoader

# Creating a basic AlloyDBLoader object
loader = await AlloyDBLoader.create(engine, table_name=TABLE_NAME)
```

### 通过默认表加载文档

加载器返回来自表的文档列表，使用第一列作为 page\_content，所有其他列作为元数据。默认表将第一列作为 page\_content，第二列作为元数据（JSON）。每一行成为一个文档。

```python theme={null}
docs = await loader.aload()
print(docs)
```

### 通过自定义表/元数据或自定义页面内容列加载文档

```python theme={null}
loader = await AlloyDBLoader.create(
    engine,
    table_name=TABLE_NAME,
    content_columns=["product_name"],  # Optional
    metadata_columns=["id"],  # Optional
)
docs = await loader.aload()
print(docs)
```

### 设置页面内容格式

加载器返回文档列表，每行一个文档，页面内容以指定的字符串格式返回，即文本（空格分隔的连接）、JSON、YAML、CSV 等。JSON 和 YAML 格式包括标题，而文本和 CSV 不包括字段标题。

```python theme={null}
loader = AlloyDBLoader.create(
    engine,
    table_name="products",
    content_columns=["product_name", "description"],
    format="YAML",
)
docs = await loader.aload()
print(docs)
```

***

> 官方原文：[src/oss/python/integrations/document\_loaders/google\_alloydb.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/document_loaders/google_alloydb.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


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