Google AlloyDB for PostgreSQL
AlloyDB 是一个完全托管的关系数据库服务,提供高性能、无缝集成和出色的可伸缩性。AlloyDB 100% 兼容 PostgreSQL。利用 AlloyDB 的 Langchain 集成扩展您的数据库应用程序,构建基于 AI 的体验。
这个笔记本介绍了如何使用AlloyDB for PostgreSQL通过AlloyDBLoader类加载文档。
Learn more about the package on GitHub.
开始之前
要运行此笔记本,您需要执行以下操作:
🦜🔗 库安装
安装集成库,langchain-google-alloydb-pg。
%pip install --upgrade --quiet langchain-google-alloydb-pg
仅限 Colab:取消以下单元格的注释以重启内核,或使用按钮重启内核。对于 Vertex AI Workbench,您可以使用顶部的按钮重启终端。
# # 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中运行此笔记本,请使用下方单元格继续。
- 如果您正在使用Vertex AI工作区,请参阅设置说明这里。
from google.colab import auth
auth.authenticate_user()
☁ 设置您的Google云项目
设置您的Google Cloud项目,以便在此笔记本中利用Google Cloud资源。
如果您不知道您的项目ID,请尝试以下方法:
- 运行
gcloud config list。 - 运行
gcloud projects list。 - 见支持页面:查找项目ID。
# @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 实例页面中找到您的数据库值。
# @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"}
AlloyDB引擎连接池
作为将AlloyDB建立为向量存储的要求和参数之一,需要一个 AlloyDBEngine 对象。AlloyDBEngine 用于配置到您的AlloyDB数据库的连接池,从而实现从您的应用程序成功连接,并遵循行业最佳实践。
要使用AlloyDBEngine.from_instance()创建一个AlloyDBEngine,您只需提供5个内容项:
project_id: 在包含 AlloyDB 实例的 Google 云项目中的项目 ID。region: Region where the AlloyDB instance is located.cluster: The name of the AlloyDB cluster.instance: AlloyDB 实例的名称。database: 连接到AlloyDB实例时要使用的数据库名称。
默认情况下,IAM数据库身份验证将被用作数据库身份验证的方法。此库使用来自环境的应用默认凭据(ADC)所属的身份主体。
Optionally, 内置数据库认证 可以使用用户名和密码访问AlloyDB数据库。只需向AlloyDBEngine.from_instance()提供可选的user和password参数:
user: 使用内置数据库进行身份验证和登录时要使用的数据库用户password: 使用内置数据库身份验证和登录时的数据库密码。
Note: 此教程演示了异步接口。所有异步方法都有对应的同步方法。
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
from langchain_google_alloydb_pg import AlloyDBLoader
# Creating a basic AlloyDBLoader object
loader = await AlloyDBLoader.create(engine, table_name=TABLE_NAME)
通过默认表格加载文档
加载器将从表格中使用第一列作为 page_content 并将所有其他列作为元数据返回一个 Documents 列表。默认情况下,表格的第一列为 page_content ,第二列为元数据(JSON)。每一行都会变成一个文档。
docs = await loader.aload()
print(docs)
通过自定义表格/元数据或自定义页面内容列加载文档
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 不包括字段表头。
loader = AlloyDBLoader.create(
engine,
table_name="products",
content_columns=["product_name", "description"],
format="YAML",
)
docs = await loader.aload()
print(docs)