跳到主要內容
Open In ColabOpen on GitHub

Google Firestore (原生模式)

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

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

Open In Colab

開始之前

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

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

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

🦜🔗 函式庫安裝

整合功能位於其自己的 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 專案

設定您的 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}

🔐 身份驗證

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

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

auth.authenticate_user()

基本用法

初始化 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)
API 參考:VertexAIEmbeddings

作為簡寫,您可以使用 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 方法從資料庫中刪除包含向量的文件。您需要提供要刪除的向量的文件 ID。這將從資料庫中移除整個文件,包括它可能擁有的任何其他欄位。

vector_store.delete(ids)

更新向量

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

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")
)

自訂連線與身份驗證

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,
)

此頁面是否對您有幫助?