跳到主要内容
Firestore 是一个无服务器的面向文档的数据库,可根据任何需求进行扩展。通过利用 Firestore 的 LangChain 集成,扩展您的数据库应用程序以构建 AI 驱动的体验。
本笔记本介绍了如何使用 Firestore 存储向量并使用 FirestoreVectorStore 类进行查询。 在 Colab 中打开

开始之前

要运行此 notebook,您需要执行以下操作 在本 notebook 的运行时环境中确认数据库访问权限后,填写以下值并运行该单元格,然后再运行示例脚本。
# @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 项目,以便您可以在此 notebook 中利用 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}

🔐 身份验证

以登录到此 notebook 的 IAM 用户身份向 Google Cloud 进行身份验证,以访问您的 Google Cloud 项目。
  • 如果您正在使用 Colab 运行此 notebook,请使用下面的单元格并继续。
  • 如果您正在使用 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)
作为简写,您可以使用 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,
)

删除向量

您可以使用 delete 方法从数据库中删除带有向量的文档。您需要提供要删除的向量的文档 ID。这将从数据库中删除整个文档,包括它可能拥有的任何其他字段。
vector_store.delete(ids)

更新向量

更新向量与添加向量类似。您可以使用 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")
)

自定义连接和认证

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

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.