Google Cloud SQL for PostgreSQL
Cloud SQL 是一项完全托管的关系数据库服务,提供高性能、无缝集成和卓越的可扩展性,并提供 PostgreSQL 等数据库引擎。
¥Cloud SQL is a fully managed relational database service that offers high performance, seamless integration, and impressive scalability and offers database engines such as PostgreSQL.
本指南提供了如何使用 Cloud SQL for PostgreSQL 通过 PostgresLoader 类加载文档的快速概述。
¥This guide provides a quick overview of how to use Cloud SQL for PostgreSQL to load Documents with the PostgresLoader class.
概述
¥Overview
开始之前
¥Before you begin
为了使用此软件包,你首先需要执行以下步骤:
¥In order to use this package, you first need to go through the following steps:
身份验证
¥Authentication
使用 gcloud auth login 命令在本地验证你的 Google Cloud 账户。
¥Authenticate locally to your Google Cloud account using the gcloud auth login command.
设置你的 Google Cloud 项目
¥Set Your Google Cloud Project
设置你的 Google Cloud 项目 ID 以在本地利用 Google Cloud 资源:
¥Set your Google Cloud project ID to leverage Google Cloud resources locally:
gcloud config set project YOUR-PROJECT-ID
如果你不知道项目 ID,请尝试以下操作:
¥If you don't know your project ID, try the following:
运行
gcloud config list。¥Run
gcloud config list.运行
gcloud projects list。¥Run
gcloud projects list.查看支持页面:查找项目 ID。
¥See the support page: Locate the project ID.
设置 PostgresLoader 实例
¥Setting up a PostgresLoader instance
要使用 PostgresLoader 类,你需要安装 @langchain/google-cloud-sql-pg 包,然后按照以下步骤操作。
¥To use the PostgresLoader class, you'll need to install the @langchain/google-cloud-sql-pg
package and then follow the steps bellow.
首先,你需要登录你的 Google Cloud 账户,并根据你的 Google Cloud 项目设置以下环境变量;这些将根据你想要如何配置 PostgresEngine 实例(fromInstance、fromEngine、fromEngineArgs)来定义:
¥First, you'll need to log in to your Google Cloud account and set the following environment variables based on your Google Cloud project; these will be defined based on how you want to configure (fromInstance, fromEngine, fromEngineArgs) your PostgresEngine instance:
PROJECT_ID="your-project-id"
REGION="your-project-region" // example: "us-central1"
INSTANCE_NAME="your-instance"
DB_NAME="your-database-name"
DB_USER="your-database-user"
PASSWORD="your-database-password"
设置实例
¥Setting up an instance
要实例化 PostgresLoader,你首先需要通过 PostgresEngine 创建数据库连接。
¥To instantiate a PostgresLoader, you'll first need to create a database connection through the PostgresEngine.
import {
PostgresLoader,
PostgresEngine,
PostgresEngineArgs,
} from "@langchain/google-cloud-sql-pg";
import * as dotenv from "dotenv";
dotenv.config();
const peArgs: PostgresEngineArgs = {
user: process.env.DB_USER ?? "",
password: process.env.PASSWORD ?? "",
};
// PostgresEngine instantiation
const engine: PostgresEngine = await PostgresEngine.fromInstance(
process.env.PROJECT_ID ?? "",
process.env.REGION ?? "",
process.env.INSTANCE_NAME ?? "",
process.env.DB_NAME ?? "",
peArgs
);
使用 table_name 参数加载文档
¥Load Documents using the table_name argument
加载器返回表中文档的列表,使用第一列作为 page_content,所有其他列作为元数据。默认表的第一列为 page_content,第二列为元数据 (JSON)。每一行都成为一个文档。
¥The loader returns a list of Documents from the table using the first column as page_content and all other columns as metadata. The default table will have the first column as page_content and the second column as metadata (JSON). Each row becomes a document.
const documentLoaderArgs: PostgresLoaderOptions = {
tableName: "test_table_custom",
contentColumns: ["fruit_name", "variety"],
metadataColumns: [
"fruit_id",
"quantity_in_stock",
"price_per_unit",
"organic",
],
format: "text",
};
const documentLoaderInstance = await PostgresLoader.initialize(
PEInstance,
documentLoaderArgs
);
使用 SQL 查询加载文档
¥Load Documents using a SQL query
查询参数允许用户指定自定义 SQL 查询,其中可以包含过滤器,用于从数据库加载特定文档。
¥The query parameter allows users to specify a custom SQL query which can include filters to load specific documents from a database.
const documentLoaderArgs: PostgresLoaderOptions = {
query: "SELECT * FROM my_fruit_table",
contentColumns: ["fruit_name", "variety"],
metadataColumns: [
"fruit_id",
"quantity_in_stock",
"price_per_unit",
"organic",
],
format: "text",
};
const documentLoaderInstance = await PostgresLoader.initialize(
PEInstance,
docucumetLoaderArgs
);
设置页面内容格式
¥Set page content format
加载器返回文档列表,每行一个文档,页面内容采用指定的字符串格式,例如文本(空格分隔的连接)、JSON、YAML、CSV 等。JSON 和 YAML 格式包含标题,而文本和 CSV 格式不包含字段标题。
¥The loader returns a list of Documents, with one document per row, with page content in specified string format, i.e. text (space separated concatenation), JSON, YAML, CSV, etc. JSON and YAML formats include headers, while text and CSV do not include field headers.