> ## 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 firestore in datastore mode 集成

> 使用 LangChain Python 集成 Google Firestore in Datastore 模式文档加载器。

> [Firestore in Datastore Mode](https://cloud.google.com/datastore) 是一个为自动扩展、高性能和简化应用开发而构建的 NoSQL 文档数据库。利用 Datastore 的 LangChain 集成扩展您的数据库应用，构建 AI 驱动的体验。

本笔记本介绍了如何使用 [Firestore in Datastore Mode](https://cloud.google.com/datastore) 通过 `DatastoreLoader` 和 `DatastoreSaver` 来 [保存、加载和删除 langchain 文档](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/document_loaders.md)。

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

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

## 开始前

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

* [创建 Google Cloud 项目](https://developers.google.com/workspace/guides/create-project)
* [启用 Datastore API](https://console.cloud.google.com/flows/enableapi?apiid=datastore.googleapis.com)
* [创建 Firestore in Datastore Mode 数据库](https://cloud.google.com/datastore/docs/manage-databases)

在确认此笔记本的运行环境中可以访问数据库后，填写以下值并运行单元格，然后再运行示例脚本。

### 🦜🔗 库安装

集成位于其自己的 `langchain-google-datastore` 包中，因此我们需要安装它。

```python theme={null}
pip install -upgrade --quiet langchain-google-datastore
```

**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)
```

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

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

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

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

```python theme={null}
# @markdown Please fill in the value below with your Google Cloud project ID and then run the cell.

PROJECT_ID = "my-project-id"  # @param {type:"string"}

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

### 🔐 身份验证

以登录到此笔记本的 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()
```

## 基本用法

### 保存文档

使用 `DatastoreSaver.upsert_documents(<documents>)` 保存 langchain 文档。默认情况下，它将尝试从 Document 元数据中的 `key` 提取实体键。

```python theme={null}
from langchain_core.documents import Document
from langchain_google_datastore import DatastoreSaver

saver = DatastoreSaver()

data = [Document(page_content="Hello, World!")]
saver.upsert_documents(data)
```

#### 无键保存文档

如果指定了 `kind`，文档将使用自动生成的 ID 进行存储。

```python theme={null}
saver = DatastoreSaver("MyKind")

saver.upsert_documents(data)
```

### 通过 kind 加载文档

使用 `DatastoreLoader.load()` 或 `DatastoreLoader.lazy_load()` 加载 langchain 文档。`lazy_load` 返回一个生成器，该生成器仅在迭代期间查询数据库。要初始化 `DatastoreLoader` 类，您需要提供：

1. `source` - 加载文档的源。它可以是 Query 的实例或要从中读取的 Datastore kind 的名称。

```python theme={null}
from langchain_google_datastore import DatastoreLoader

loader = DatastoreLoader("MyKind")
data = loader.load()
```

### 通过查询加载文档

除了从 kind 加载文档外，我们还可以选择从查询加载文档。例如：

```python theme={null}
from google.cloud import datastore

client = datastore.Client(database="non-default-db", namespace="custom_namespace")
query_load = client.query(kind="MyKind")
query_load.add_filter("region", "=", "west_coast")

loader_document = DatastoreLoader(query_load)

data = loader_document.load()
```

### 删除文档

使用 `DatastoreSaver.delete_documents(<documents>)` 从 Datastore 删除一组 langchain 文档。

```python theme={null}
saver = DatastoreSaver()

saver.delete_documents(data)

keys_to_delete = [
    ["Kind1", "identifier"],
    ["Kind2", 123],
    ["Kind3", "identifier", "NestedKind", 456],
]
# The Documents will be ignored and only the document ids will be used.
saver.delete_documents(data, keys_to_delete)
```

## 高级用法

### 使用自定义文档页面内容和元数据加载文档

`page_content_properties` 和 `metadata_properties` 的参数将指定要写入 LangChain Document `page_content` 和 `metadata` 的实体属性。

```python theme={null}
loader = DatastoreLoader(
    source="MyKind",
    page_content_fields=["data_field"],
    metadata_fields=["metadata_field"],
)

data = loader.load()
```

### 自定义页面内容格式

当 `page_content` 只包含一个字段时，信息将是该字段的值。否则，`page_content` 将以 JSON 格式显示。

### 自定义连接和身份验证

```python theme={null}
from google.auth import compute_engine
from google.cloud.firestore import Client

client = Client(database="non-default-db", creds=compute_engine.Credentials())
loader = DatastoreLoader(
    source="foo",
    client=client,
)
```

***

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


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