Google El Carro Oracle
Google Cloud El Carro Oracle 提供了一種在
Kubernetes
中執行Oracle
資料庫的方法,作為可攜式、開放原始碼、社群驅動、無供應商鎖定的容器協調系統。El Carro
為全面且一致的配置和部署,以及即時操作和監控提供了強大的宣告式 API。透過利用El Carro
Langchain 整合,擴展您的Oracle
資料庫功能以建構 AI 驅動的體驗。
本指南將介紹如何使用 El Carro
Langchain 整合,透過 ElCarroChatMessageHistory
類別來儲存聊天訊息歷史記錄。此整合適用於任何 Oracle
資料庫,無論它在哪裡執行。
在 GitHub 上了解更多關於此套件的資訊。
開始之前
若要執行此筆記本,您需要執行以下操作
- 如果您想使用 El Carro 執行 Oracle 資料庫,請完成入門指南章節。
🦜🔗 函式庫安裝
此整合存在於其自己的 langchain-google-el-carro
套件中,因此我們需要安裝它。
%pip install --upgrade --quiet langchain-google-el-carro langchain-google-vertexai langchain
僅限 Colab: 取消註解以下儲存格以重新啟動核心,或使用按鈕重新啟動核心。對於 Vertex AI Workbench,您可以使用頂部的按鈕重新啟動終端機。
# # Automatically restart kernel after installs so that your environment can access the new packages
# import IPython
# app = IPython.Application.instance()
# app.kernel.do_shutdown(True)
🔐 身份驗證
以登入此筆記本的 IAM 使用者身份驗證到 Google Cloud,以便存取您的 Google Cloud 專案。
- 如果您使用 Colab 執行此筆記本,請使用以下儲存格並繼續。
- 如果您使用 Vertex AI Workbench,請查看此處的設定說明。
# from google.colab import auth
# auth.authenticate_user()
☁ 設定您的 Google Cloud 專案
設定您的 Google Cloud 專案,以便您可以在此筆記本中利用 Google Cloud 資源。
如果您不知道您的專案 ID,請嘗試以下操作
- 執行
gcloud config list
。 - 執行
gcloud projects list
。 - 請參閱支援頁面:尋找專案 ID。
# @markdown Please fill in the value below with your Google Cloud project ID and then run the cell.
PROJECT_ID = "my-project-id" # @param {type:"string"}
# Set the project id
!gcloud config set project {PROJECT_ID}
基本用法
設定 Oracle 資料庫連線
使用您的 Oracle 資料庫連線詳細資訊填寫以下變數。
# @title Set Your Values Here { display-mode: "form" }
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: ")
如果您使用 El Carro
,您可以在 El Carro
Kubernetes 實例的狀態中找到主機名稱和連接埠值。使用您為 PDB 建立的使用者密碼。範例
kubectl get -w instances.oracle.db.anthosapis.com -n db NAME DB ENGINE VERSION EDITION ENDPOINT URL DB NAMES BACKUP ID READYSTATUS READYREASON DBREADYSTATUS DBREADYREASON mydb Oracle 18c Express mydb-svc.db 34.71.69.25:6021 False CreateInProgress
ElCarroEngine 連線池
ElCarroEngine
配置到您的 Oracle 資料庫的連線池,從而實現從您的應用程式成功連線並遵循行業最佳實務。
from langchain_google_el_carro import ElCarroEngine
elcarro_engine = ElCarroEngine.from_instance(
db_host=HOST,
db_port=PORT,
db_name=DATABASE,
db_user=USER,
db_password=PASSWORD,
)
初始化表格
ElCarroChatMessageHistory
類別需要具有特定結構描述的資料庫表格,才能儲存聊天訊息歷史記錄。
ElCarroEngine
類別有一個方法 init_chat_history_table()
,可用於為您建立具有正確結構描述的表格。
elcarro_engine.init_chat_history_table(table_name=TABLE_NAME)
ElCarroChatMessageHistory
若要初始化 ElCarroChatMessageHistory
類別,您只需要提供 3 件事
elcarro_engine
-ElCarroEngine
引擎的實例。session_id
- 指定工作階段 ID 的唯一識別字串。table_name
:Oracle 資料庫中用於儲存聊天訊息歷史記錄的表格名稱。
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?")
history.messages
清理
當特定工作階段的歷史記錄過時且可以刪除時,可以透過以下方式完成。
注意: 一旦刪除,資料將不再儲存在您的資料庫中,並且永遠消失。
history.clear()
🔗 串鏈
我們可以輕鬆地將此訊息歷史記錄類別與 LCEL Runnables 結合使用
為此,我們將使用 Google 的 Vertex AI 聊天模型 之一,這需要您在 Google Cloud 專案中啟用 Vertex AI API。
# 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",
)
# This is where we configure the session 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)