Skip to main content

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 存储消息并通过 PostgresChatMessageHistory 类提供对话。

¥This guide provides a quick overview of how to use Cloud SQL for PostgreSQL to store messages and provide conversation with the PostgresChatMessageHistory class.

概述

¥Overview

开始之前

¥Before you begin

为了使用此软件包,你首先需要执行以下步骤:

¥In order to use this package, you first need to go through the following steps:

  1. 选择或创建一个云平台项目。

    ¥Select or create a Cloud Platform project.

  2. 为你的项目启用计费功能。

    ¥Enable billing for your project.

  3. 启用 Cloud SQL Admin API。

    ¥Enable the Cloud SQL Admin API.

  4. 设置身份验证。

    ¥Setup Authentication.

  5. 创建 CloudSQL 实例

    ¥Create a CloudSQL instance

  6. 创建 CloudSQL 数据库

    ¥Create a CloudSQL database

  7. 将用户添加到数据库

    ¥Add a user to the database

身份验证

¥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.

设置 PostgresChatMessageHistory 实例

¥Setting up a PostgresChatMessageHistory instance

要使用 PostgresChatMessageHistory 类,你需要安装 @langchain/google-cloud-sql-pg 包,然后按照以下步骤操作。

¥To use the PostgresChatMessageHistory 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

要实例化 PostgresChatMessageHistory,你首先需要通过 PostgresEngine 创建数据库连接,然后初始化聊天历史记录表,最后调用 .initialize() 方法实例化聊天消息历史记录。

¥To instantiate a PostgresChatMessageHistory, you'll first need to create a database connection through the PostgresEngine, then initialize the chat history table and finally call the .initialize() method to instantiate the chat message history.

import {
PostgresChatMessageHistory,
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
);

// Chat history table initialization
await engine.initChatHistoryTable("my_chat_history_table");

// PostgresChatMessageHistory instantiation
const historyInstance: PostgresChatMessageHistory =
await PostgresChatMessageHistory.initialize(
engine,
"test",
"my_chat_history_table"
);

管理聊天消息历史记录

¥Manage Chat Message History

将消息添加到聊天历史记录

¥Add Messages to the chat history

你可以使用 addMessage 方法向聊天历史记录添加消息,也可以使用 addMessages 方法传递消息数组。

¥You can add a message to the chat history by using the addMessage method or you can use the addMessages method to pass an array of messages.

import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";

// Add one message
const msg = new HumanMessage("Hi!");
await historyInstance.addMessage(msg);

// Add an array of messages
const msg1: HumanMessage = new HumanMessage("Hi!");
const msg2: AIMessage = new AIMessage("what's up?");
const msg3: HumanMessage = new HumanMessage("How are you?");
const messages: BaseMessage[] = [msg1, msg2, msg3];
await historyInstance.addMessages(messages);

获取聊天记录中保存的消息

¥Get Messages saved on the chat history

const messagesSaved: BaseMessage[] = await historyInstance.getMessages();
console.log(messagesSaved);

清除聊天历史记录中的消息

¥Clear Messages from the chat history

要从聊天历史记录中删除所有消息,只需调用 PostgresChatMessageHistory 类中的 clear 方法即可。

¥To remove all messages from the chat history, just call the clear method from the PostgresChatMessageHistory class.

await historyInstance.clear();