跳至主要內容

Google Firestore (原生模式)

Firestore 是一個無伺服器、面向文件的資料庫,可以擴展以滿足任何需求。 擴展您的資料庫應用程式,利用 Firestore 的 Langchain 整合來構建 AI 驅動的體驗。

本筆記本介紹如何使用 Firestore 來儲存向量,並使用 FirestoreVectorStore 類別查詢它們。

Open In Colab

開始之前 (Before You Begin)

要運行此筆記本,您需要執行以下操作:

確認在此筆記本的運行時環境中可以存取資料庫後,填寫以下值並在運行範例腳本之前運行儲存格。

# @markdown Please specify a source for demo purpose.
COLLECTION_NAME = "test" # @param {type:"CollectionReference"|"string"}

🦜🔗 函式庫安裝 (Library Installation)

該整合位於其自己的 langchain-google-firestore 套件中,因此我們需要安裝它。對於此筆記本,我們也將安裝 langchain-google-genai 以使用 Google Generative AI 嵌入。

%pip install -upgrade --quiet langchain-google-firestore langchain-google-vertexai

僅限 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)

☁ 設定您的 Google Cloud 專案 (Set Your Google Cloud Project)

設定您的 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 = "extensions-testing" # @param {type:"string"}

# Set the project id
!gcloud config set project {PROJECT_ID}

🔐 驗證 (Authentication)

以登錄到此筆記本的 IAM 使用者身分驗證到 Google Cloud,以便存取您的 Google Cloud 專案。

  • 如果您使用 Colab 運行此筆記本,請使用下面的儲存格並繼續。
  • 如果您使用 Vertex AI Workbench,請查看此處的設定說明。
from google.colab import auth

auth.authenticate_user()

基本用法 (Basic Usage)

初始化 FirestoreVectorStore (Initialize FirestoreVectorStore)

FirestoreVectorStore 允許您在 Firestore 資料庫中儲存新的向量。 您可以使用它來儲存來自任何模型的嵌入,包括來自 Google Generative AI 的嵌入。

from langchain_google_firestore import FirestoreVectorStore
from langchain_google_vertexai import VertexAIEmbeddings

embedding = VertexAIEmbeddings(
model_name="textembedding-gecko@latest",
project=PROJECT_ID,
)

# Sample data
ids = ["apple", "banana", "orange"]
fruits_texts = ['{"name": "apple"}', '{"name": "banana"}', '{"name": "orange"}']

# Create a vector store
vector_store = FirestoreVectorStore(
collection="fruits",
embedding=embedding,
)

# Add the fruits to the vector store
vector_store.add_texts(fruits_texts, ids=ids)

作為簡寫,您可以使用 from_textsfrom_documents 方法在單個步驟中初始化和添加向量。

vector_store = FirestoreVectorStore.from_texts(
collection="fruits",
texts=fruits_texts,
embedding=embedding,
)
from langchain_core.documents import Document

fruits_docs = [Document(page_content=fruit) for fruit in fruits_texts]

vector_store = FirestoreVectorStore.from_documents(
collection="fruits",
documents=fruits_docs,
embedding=embedding,
)
API 參考:Document

刪除向量 (Delete Vectors)

您可以使用 delete 方法從資料庫中刪除帶有向量的文件。 您需要提供要刪除的向量的文件 ID。 這將從資料庫中刪除整個文件,包括它可能具有的任何其他欄位。

vector_store.delete(ids)

更新向量 (Update Vectors)

更新向量與添加向量類似。 您可以使用 add 方法通過提供文件 ID 和新向量來更新文件的向量。

fruit_to_update = ['{"name": "apple","price": 12}']
apple_id = "apple"

vector_store.add_texts(fruit_to_update, ids=[apple_id])

您可以使用 FirestoreVectorStore 對您儲存的向量執行相似性搜尋。 這對於尋找相似的文件或文字很有用。

vector_store.similarity_search("I like fuji apples", k=3)
vector_store.max_marginal_relevance_search("fuji", 5)

您可以使用 filters 參數將預先篩選器添加到搜尋中。 這對於按特定欄位或值進行篩選很有用。

from google.cloud.firestore_v1.base_query import FieldFilter

vector_store.max_marginal_relevance_search(
"fuji", 5, filters=FieldFilter("content", "==", "apple")
)

自定義連線與驗證 (Customize Connection & Authentication)

from google.api_core.client_options import ClientOptions
from google.cloud import firestore
from langchain_google_firestore import FirestoreVectorStore

client_options = ClientOptions()
client = firestore.Client(client_options=client_options)

# Create a vector store
vector_store = FirestoreVectorStore(
collection="fruits",
embedding=embedding,
client=client,
)

此頁面是否有幫助? (Was this page helpful?)