Google Spanner
Spanner 是一個高度可擴展的資料庫,它結合了無限的可擴展性與關係語義,例如二級索引、強一致性、模式和 SQL,在一個簡單的解決方案中提供 99.999% 的可用性。
本筆記本介紹如何使用 Spanner
搭配 SpannerVectorStore
類別進行向量搜尋。
在 GitHub 上了解更多關於套件的資訊。
開始之前
若要執行此筆記本,您需要執行以下操作
🦜🔗 函式庫安裝
此整合位於其自身的 langchain-google-spanner
套件中,因此我們需要安裝它。
%pip install --upgrade --quiet langchain-google-spanner langchain-google-vertexai
Note: you may need to restart the kernel to use updated packages.
僅限 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,請查看此處的設定說明 here。
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}
%env GOOGLE_CLOUD_PROJECT={PROJECT_ID}
💡 API 啟用
langchain-google-spanner
套件要求您在您的 Google Cloud 專案中啟用 Spanner API。
# enable Spanner API
!gcloud services enable spanner.googleapis.com
基本用法
設定 Spanner 資料庫值
在 Spanner 執行個體頁面中尋找您的資料庫值。
# @title Set Your Values Here { display-mode: "form" }
INSTANCE = "my-instance" # @param {type: "string"}
DATABASE = "my-database" # @param {type: "string"}
TABLE_NAME = "vectors_search_data" # @param {type: "string"}
初始化表格
SpannerVectorStore
類別執行個體需要一個具有 id、content 和 embeddings 欄位的資料庫表格。
輔助方法 init_vector_store_table()
可用於為您建立具有適當結構描述的表格。
from langchain_google_spanner import SecondaryIndex, SpannerVectorStore, TableColumn
SpannerVectorStore.init_vector_store_table(
instance_id=INSTANCE,
database_id=DATABASE,
table_name=TABLE_NAME,
# Customize the table creation
# id_column="row_id",
# content_column="content_column",
# metadata_columns=[
# TableColumn(name="metadata", type="JSON", is_null=True),
# TableColumn(name="title", type="STRING(MAX)", is_null=False),
# ],
# secondary_indexes=[
# SecondaryIndex(index_name="row_id_and_title", columns=["row_id", "title"])
# ],
)
建立嵌入類別執行個體
您可以使用任何 LangChain 嵌入模型。您可能需要啟用 Vertex AI API 才能使用 VertexAIEmbeddings
。我們建議為生產環境設定嵌入模型的版本,了解更多關於 文字嵌入模型 的資訊。
# enable Vertex AI API
!gcloud services enable aiplatform.googleapis.com
from langchain_google_vertexai import VertexAIEmbeddings
embeddings = VertexAIEmbeddings(
model_name="textembedding-gecko@latest", project=PROJECT_ID
)
SpannerVectorStore
若要初始化 SpannerVectorStore
類別,您需要提供 4 個必要引數,其他引數為選用引數,只有在與預設引數不同時才需要傳遞
instance_id
- Spanner 執行個體的名稱database_id
- Spanner 資料庫的名稱table_name
- 資料庫中用於儲存文件及其嵌入的表格名稱。embedding_service
- 用於產生嵌入的 Embeddings 實作。
db = SpannerVectorStore(
instance_id=INSTANCE,
database_id=DATABASE,
table_name=TABLE_NAME,
embedding_service=embeddings,
# Connect to a custom vector store table
# id_column="row_id",
# content_column="content",
# metadata_columns=["metadata", "title"],
)
新增文件
在向量儲存中新增文件。
import uuid
from langchain_community.document_loaders import HNLoader
loader = HNLoader("https://news.ycombinator.com/item?id=34817881")
documents = loader.load()
ids = [str(uuid.uuid4()) for _ in range(len(documents))]
db.add_documents(documents, ids)
搜尋文件
使用相似度搜尋在向量儲存中搜尋文件。
db.similarity_search(query="Explain me vector store?", k=3)
搜尋文件
使用最大邊際相關性搜尋在向量儲存中搜尋文件。
db.max_marginal_relevance_search("Testing the langchain integration with spanner", k=3)
刪除文件
若要從向量儲存中移除文件,請使用與初始化 VectorStore 時 `row_id` 欄位中的值相對應的 ID。
db.delete(ids=["id1", "id2"])
刪除文件
若要從向量儲存中移除文件,您可以使用文件本身。在 VectorStore 初始化期間提供的 content 欄位和 metadata 欄位將用於找出與文件相對應的列。然後將刪除任何相符的列。
db.delete(documents=[documents[0], documents[1]])