要运行此 Notebook,您需要完成以下操作:
创建一个 Google Cloud 项目
安装集成库 llama-index-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)
以登录此 Notebook 的 IAM 用户身份向 Google Cloud 进行身份验证,以访问您的 Google Cloud 项目。
如果您正在使用 Colab 运行此 Notebook,请使用以下单元格并继续。
- 如果您正在使用 Vertex AI Workbench,请查看此处的设置说明here。
- from google.colab import auth auth.authenticate_user()
from google.colab import auth
auth.authenticate_user()
设置您的 Google Cloud 项目,以便您可以在此 Notebook 中利用 Google Cloud 资源。
如果您不知道您的项目 ID,请尝试以下操作:
运行 gcloud config list
。
- 运行
gcloud projects list
。 - 查看支持页面:查找项目 ID。
- # @markdown 请在下方填写您的 Google Cloud 项目 ID,然后运行单元格。 PROJECT_ID = "my-project-id" # @param {type:"string"} # 设置项目 ID !gcloud config set project {PROJECT_ID}
# @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}
设置 AlloyDB 数据库值¶
在AlloyDB 实例页面中查找您的数据库值。
# @title 在此处设置您的值 { 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 = "document_store" # @param {type: "string"} USER = "postgres" # @param {type: "string"} PASSWORD = "my-password" # @param {type: "string"}
# @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 = "document_store" # @param {type: "string"}
USER = "postgres" # @param {type: "string"}
PASSWORD = "my-password" # @param {type: "string"}
建立 AlloyDB 读取器的要求和参数之一是 AlloyDBEngine
对象。AlloyDBEngine
为您的 AlloyDB 数据库配置连接池,从而实现应用程序的成功连接并遵循行业最佳实践。
使用 AlloyDBEngine.from_instance()
创建 AlloyDBEngine
时,您只需要提供 5 项信息:
project_id
: AlloyDB 实例所在的 Google Cloud 项目的项目 ID。
region
: AlloyDB 实例所在的区域。cluster
: AlloyDB 集群的名称。instance
: AlloyDB 实例的名称。database
: 要连接的 AlloyDB 实例上的数据库名称。- 默认情况下,IAM 数据库身份验证将使用 IAM 数据库身份验证作为数据库身份验证方法。此库使用来自环境的 Application Default Credentials (ADC) 所属的 IAM 主体。
(可选)也可以使用内置数据库身份验证,即使用用户名和密码访问 AlloyDB 数据库。只需向 AlloyDBEngine.from_instance()
提供可选的 user
和 password
参数即可。
user
: 用于内置数据库身份验证和登录的数据库用户。
password
: 用于内置数据库身份验证和登录的数据库密码。- 注意:本教程演示了异步接口。所有异步方法都有相应的同步方法。
from llama_index_alloydb_pg import AlloyDBEngine engine = await AlloyDBEngine.afrom_instance( project_id=PROJECT_ID, region=REGION, cluster=CLUSTER, instance=INSTANCE, database=DATABASE, user=USER, password=PASSWORD, )
from llama_index_alloydb_pg import AlloyDBEngine
engine = await AlloyDBEngine.afrom_instance(
project_id=PROJECT_ID,
region=REGION,
cluster=CLUSTER,
instance=INSTANCE,
database=DATABASE,
user=USER,
password=PASSWORD,
)
创建用于从 AlloyDB 获取数据的 AlloyDBReader
时,您有两种主要选项来指定要加载的数据:
使用 table_name 参数 - 当您指定 table_name 参数时,您告诉读取器从给定表中获取所有数据。
- 使用 query 参数 - 当您指定 query 参数时,您可以提供自定义 SQL 查询来获取数据。这使您可以完全控制 SQL 查询,包括选择特定列、应用过滤器、排序、连接表等。
- 使用
table_name
参数加载文档¶
通过默认表加载文档¶
读取器从表中返回文档列表,其中第一列用作文本,所有其他列用作元数据。默认表的第一列将是文本,第二列将是元数据 (JSON)。每行都成为一个文档。
from llama_index_alloydb_pg import AlloyDBReader # 创建一个基本的 AlloyDBReader 对象 reader = await AlloyDBReader.create( engine, table_name=TABLE_NAME, # schema_name=SCHEMA_NAME, )
from llama_index_alloydb_pg import AlloyDBReader
# Creating a basic AlloyDBReader object
reader = await AlloyDBReader.create(
engine,
table_name=TABLE_NAME,
# schema_name=SCHEMA_NAME,
)
reader = await AlloyDBReader.create( engine, table_name=TABLE_NAME, # schema_name=SCHEMA_NAME, content_columns=["product_name"], # 可选 metadata_columns=["id"], # 可选 )
reader = await AlloyDBReader.create(
engine,
table_name=TABLE_NAME,
# schema_name=SCHEMA_NAME,
content_columns=["product_name"], # Optional
metadata_columns=["id"], # Optional
)
query 参数允许用户指定自定义 SQL 查询,该查询可以包含过滤器以从数据库加载特定文档。
table_name = "products" content_columns = ["product_name", "description"] metadata_columns = ["id", "content"] reader = AlloyDBReader.create( engine=engine, query=f"SELECT * FROM {table_name};", content_columns=content_columns, metadata_columns=metadata_columns, )
table_name = "products"
content_columns = ["product_name", "description"]
metadata_columns = ["id", "content"]
reader = AlloyDBReader.create(
engine=engine,
query=f"SELECT * FROM {table_name};",
content_columns=content_columns,
metadata_columns=metadata_columns,
)
content_columns
和 metadata_columns
,读取器将自动将返回的第一列视为文档的 text
,并将所有后续列视为 metadata
。设置页面内容格式¶
读取器返回一个文档列表,每行一个文档,页面内容采用指定的字符串格式,例如文本(空格分隔的连接)、JSON、YAML、CSV 等。JSON 和 YAML 格式包含标题,而文本和 CSV 不包含字段标题。
reader = await AlloyDBReader.create( engine, table_name=TABLE_NAME, # schema_name=SCHEMA_NAME, content_columns=["product_name", "description"], format="YAML", )
reader = await AlloyDBReader.create(
engine,
table_name=TABLE_NAME,
# schema_name=SCHEMA_NAME,
content_columns=["product_name", "description"],
format="YAML",
)
一次性加载所有数据¶
docs = await reader.aload_data()
print(docs)
惰性加载数据¶
docs_iterable = reader.alazy_load_data()
docs = []
async for doc in docs_iterable:
docs.append(doc)
print(docs)