Google Cloud SQL for PostgreSQL - `PostgresReader`
Cloud SQL is a fully managed relational database service that offers high performance, seamless integration, and impressive scalability. It offers MySQL, PostgreSQL, and SQL Server database engines. Extend your database application to build AI-powered experiences leveraging Cloud SQL’s LlamaIndex integrations.
本笔记本介绍如何使用 Cloud SQL for PostgreSQL 通过 PostgresReader 类将数据检索为文档。
Learn more about the package on GitHub.
要运行此笔记本,您需要执行以下操作:
安装集成库,llama-index-cloud-sql-pg。
Colab only: Uncomment the following cell to restart the kernel or use the button to restart the kernel. For Vertex AI Workbench you can restart the terminal using the button on top.
# # 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 运行此笔记本,请使用下面的单元格并继续。
- If you are using Vertex AI Workbench, check out the setup instructions here.
from google.colab import auth
auth.authenticate_user()设置您的 Google Cloud 项目,以便在此笔记本中利用 Google Cloud 资源。
如果您不知道您的项目ID,请尝试以下方法:
- Run
gcloud config list. - Run
gcloud projects list. - See the support page: Locate the 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}设置 Cloud SQL 数据库值
Section titled “Set Cloud SQL database values”Find your database values, in the Cloud SQL Instances page.
# @title Set Your Values Here { display-mode: "form" }REGION = "us-central1" # @param {type: "string"}INSTANCE = "my-primary" # @param {type: "string"}DATABASE = "my-database" # @param {type: "string"}TABLE_NAME = "reader_table" # @param {type: "string"}USER = "postgres" # @param {type: "string"}PASSWORD = "my-password" # @param {type: "string"}PostgresEngine 连接池
Section titled “PostgresEngine Connection Pool”将 Cloud SQL 设为读取器的一个要求和参数是 PostgresEngine 对象。PostgresEngine 会配置到您 Cloud SQL 数据库的连接池,使您的应用程序能够成功连接并遵循行业最佳实践。
To create a PostgresEngine using PostgresEngine.from_instance() you need to provide only 4 things:
project_idproject_id : 云 SQL 实例所在的 Google Cloud 项目的项目 ID。regionregion : Cloud SQL 实例所在的区域。instanceinstance : Cloud SQL 实例的名称。databasedatabase : 要连接的 Cloud SQL 实例上的数据库名称。
By default, IAM database authentication will be used as the method of database authentication. This library uses the IAM principal belonging to the Application Default Credentials (ADC) sourced from the envionment.
有关IAM数据库认证的更多信息,请参阅:
Optionally, built-in database authentication using a username and password to access the Cloud SQL database can also be used. Just provide the optional user and password arguments to PostgresEngine.from_instance():
useruser : 用于内置数据库认证和登录的数据库用户passwordpassword : 用于内置数据库认证和登录的数据库密码。
Note: This tutorial demonstrates the async interface. All async methods have corresponding sync methods.
from llama_index_cloud_sql_pg import PostgresEngine
engine = await PostgresEngine.afrom_instance( project_id=PROJECT_ID, region=REGION, instance=INSTANCE, database=DATABASE, user=USER, password=PASSWORD,)创建 PostgresReader
Section titled “Create PostgresReader”在创建用于从 Cloud SQL Postgres 获取数据的 PostgresReader 时,您有两个主要选项来指定要加载的数据:
- 使用 table_name 参数 - 当您指定 table_name 参数时,您是在告诉读取器从给定表中获取所有数据。
- 使用查询参数 - 当您指定查询参数时,可以提供自定义SQL查询来获取数据。这使您能够完全控制SQL查询,包括选择特定列、应用筛选条件、排序、连接表等。
使用 table_name 参数加载文档
Section titled “Load Documents using the table_name argument”读取器从表中返回一个文档列表,使用第一列作为文本,其他所有列作为元数据。默认表格将第一列作为文本,第二列作为元数据(JSON)。每一行成为一个文档。
from llama_index_cloud_sql_pg import PostgresReader
# Creating a basic PostgresReader objectreader = await PostgresReader.create( engine, table_name=TABLE_NAME, # schema_name=SCHEMA_NAME,)通过自定义表格/元数据或自定义页面内容列加载文档
Section titled “Load documents via custom table/metadata or custom page content columns”reader = await PostgresReader.create( engine, table_name=TABLE_NAME, # schema_name=SCHEMA_NAME, content_columns=["product_name"], # Optional metadata_columns=["id"], # Optional)查询参数允许用户指定自定义SQL查询,其中可包含筛选条件以从数据库加载特定文档。
table_name = "products"content_columns = ["product_name", "description"]metadata_columns = ["id", "content"]
reader = PostgresReader.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 PostgresReader.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)