Google Firestore(原生模式)
Firestore is a serverless document-oriented database that scales to meet any demand. Extend your database application to build AI-powered experiences leveraging Firestore's Langchain integrations.
本笔记本介绍了如何使用 Firestore 存储向量并使用 FirestoreVectorStore 类进行查询。
开始之前
要运行此笔记本,您需要执行以下操作:
在确认此笔记本的运行时环境已具备数据库访问权限后,请填写以下值并运行该单元格,然后再运行示例脚本。
# @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)
作为一种简写,您可以使用 from_texts 和 from_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,
)