Skip to content
LlamaIndex 文档版本 跟随上游 源快照 1cbcc712 更新于 2026-10-18

Google Firestore(原生模式)

Firestore 是一个无服务器的面向文档数据库,可弹性扩展以满足任何需求。扩展你的数据库应用,利用 Firestore 的 Langchain 集成构建 AI 驱动的体验。

本 notebook 介绍如何使用 Firestore 存储向量,并使用 FirestoreVectorStore 类查询它们。

开始之前

要运行此 notebook,你需要执行以下操作:

确认此 notebook 的运行环境可以访问数据库后,填写以下值并在运行示例脚本前执行该单元格。

安装库

如果你在 colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙。对于本 notebook,我们还将安装 langchain-google-genai,以使用 Google Generative AI 嵌入。

%pip install --quiet llama-index
%pip install --quiet llama-index-vector-stores-firestore llama-index-embeddings-huggingface

☁ 设置你的 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 = "YOUR_PROJECT_ID"  # @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

FirestoreVectroStore 允许你将数据加载到 Firestore 中并进行查询。

# @markdown Please specify a source for demo purpose.
COLLECTION_NAME = "test_collection"
from llama_index.core import SimpleDirectoryReader

# Load documents and build index
documents = SimpleDirectoryReader(
    "../../examples/data/paul_graham"
).load_data()
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core import Settings

# Set the embedding model, this is a local model
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
from llama_index.core import VectorStoreIndex
from llama_index.core import StorageContext, ServiceContext

from llama_index.vector_stores.firestore import FirestoreVectorStore

# Create a Firestore vector store
store = FirestoreVectorStore(collection_name=COLLECTION_NAME)

storage_context = StorageContext.from_defaults(vector_store=store)
service_context = ServiceContext.from_defaults(
    llm=None, embed_model=embed_model
)

index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context, service_context=service_context
)
/var/folders/mh/cqn7wzgs3j79rbg243_gfcx80000gn/T/ipykernel_29666/1668628626.py:10: DeprecationWarning: Call to deprecated class method from_defaults. (ServiceContext is deprecated, please use `llama_index.settings.Settings` instead.) -- Deprecated since version 0.10.0.
  service_context = ServiceContext.from_defaults(llm=None, embed_model=embed_model)

LLM is explicitly disabled. Using MockLLM.

执行搜索

你可以使用 FirestoreVectorStore 对已存储的向量执行相似度搜索。这对于查找相似的文档或文本很有用。

query_engine = index.as_query_engine()
res = query_engine.query("What did the author do growing up?")
print(str(res.source_nodes[0].text))
None
What I Worked On

February 2021

Before college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep.

The first programs I tried writing were on the IBM 1401 that our school district used for what was then called "data processing." This was in 9th grade, so I was 13 or 14. The school district's 1401 happened to be in the basement of our junior high school, and my friend Rich Draves and I got permission to use it. It was like a mini Bond villain's lair down there, with all these alien-looking machines — CPU, disk drives, printer, card reader — sitting up on a raised floor under bright fluorescent lights.

The language we used was an early version of Fortran. You had to type programs on punch cards, then stack them in the card reader and press a button to load the program into memory and run it. The result would ordinarily be to print something on the spectacularly loud printer.

I was puzzled by the 1401. I couldn't figure out what to do with it. And in retrospect there's not much I could have done with it. The only form of input to programs was data stored on punched cards, and I didn't have any data stored on punched cards. The only other option was to do things that didn't rely on any input, like calculate approximations of pi, but I didn't know enough math to do anything interesting of that type. So I'm not surprised I can't remember any programs I wrote, because they can't have done much. My clearest memory is of the moment I learned it was possible for programs not to terminate, when one of mine didn't. On a machine without time-sharing, this was a social as well as a technical error, as the data center manager's expression made clear.

With microcomputers, everything changed. Now you could have a computer sitting right in front of you, on a desk, that could respond to your keystrokes as it was running instead of just churning through a stack of punch cards and then stopping. [1]

The first of my friends to get a microcomputer built it himself. It was sold as a kit by Heathkit. I remember vividly how impressed and envious I felt watching him sitting in front of it, typing programs right into the computer.

Computers were expensive in those days and it took me years of nagging before I convinced my father to buy one, a TRS-80, in about 1980. The gold standard then was the Apple II, but a TRS-80 was good enough. This was when I really started programming. I wrote simple games, a program to predict how high my model rockets would fly, and a word processor that my father used to write at least one book. There was only room in memory for about 2 pages of text, so he'd write 2 pages at a time and then print them out, but it was a lot better than a typewriter.

Though I liked programming, I didn't plan to study it in college. In college I was going to study philosophy, which sounded much more powerful. It seemed, to my naive high school self, to be the study of the ultimate truths, compared to which the things studied in other fields would be mere domain knowledge. What I discovered when I got to college was that the other fields took up so much of the space of ideas that there wasn't much left for these supposed ultimate truths. All that seemed left for philosophy were edge cases that people in other fields felt could safely be ignored.

I couldn't have put this into words when I was 18. All I knew at the time was that I kept taking philosophy courses and they kept being boring. So I decided to switch to AI.

AI was in the air in the mid 1980s, but there were two things especially that made me want to work on it: a novel by Heinlein called The Moon is a Harsh Mistress, which featured an intelligent computer called Mike, and a PBS documentary that showed Terry Winograd using SHRDLU. I haven't tried rereading The Moon is a Harsh Mistress, so I don't know how well it has aged, but when I read it I was drawn entirely into its world.

你可以通过指定 filters 参数对搜索结果应用预过滤。

from llama_index.core.vector_stores.types import (
    MetadataFilters,
    ExactMatchFilter,
    MetadataFilter,
)

filters = MetadataFilters(
    filters=[MetadataFilter(key="author", value="Paul Graham")]
)
query_engine = index.as_query_engine(filters=filters)
res = query_engine.query("What did the author do growing up?")
print(str(res.source_nodes[0].text))

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。