Skip to main content
Open In Colab在 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 参考:文档

删除向量

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

vector_store.delete(ids)

更新向量

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