【RAG】【vector_stores038】Firestore向量存储示例

张开发
2026/4/18 2:59:32 15 分钟阅读

分享文章

【RAG】【vector_stores038】Firestore向量存储示例
案例目标本案例展示如何使用Google Firestore作为向量数据库与LlamaIndex集成实现高效的文档存储和相似性搜索功能。Firestore是Google Cloud提供的无服务器文档数据库可以自动扩展以满足任何需求。通过本示例您将学习如何配置Google Cloud项目和Firestore数据库如何使用FirestoreVectorStore存储和检索向量数据如何执行向量相似性搜索查询如何应用元数据过滤来优化搜索结果技术栈与核心依赖核心依赖依赖包用途llama-index-vector-stores-firestoreLlamaIndex与Firestore的集成包llama-index-embeddings-huggingfaceHuggingFace嵌入模型集成llama-indexLlamaIndex核心框架google-cloud-firestoreGoogle Cloud Firestore客户端库技术栈Google Firestore GCP无服务器文档数据库提供自动扩展和强一致性HuggingFace嵌入使用BAAI/bge-small-en-v1.5模型生成文本向量表示文档处理LlamaIndex的SimpleDirectoryReader用于加载文档向量索引LlamaIndex的VectorStoreIndex用于构建索引和查询环境配置Google Cloud项目设置在运行示例之前需要完成以下步骤创建Google Cloud项目访问Google Cloud控制台创建新项目启用Firestore API在API库中启用Firestore API创建Firestore数据库按照Firestore文档创建数据库安装依赖%pip install --quiet llama-index %pip install --quiet llama-index-vector-stores-firestore llama-index-embeddings-huggingface设置Google Cloud项目ID# 设置您的Google Cloud项目ID PROJECT_ID YOUR_PROJECT_ID # 替换为您的项目ID # 使用gcloud命令行工具设置项目 !gcloud config set project {PROJECT_ID}提示如果您不知道项目ID可以运行以下命令之一gcloud config list- 查看当前配置gcloud projects list- 列出所有项目身份验证在Colab环境中可以使用以下代码进行身份验证from google.colab import auth # 进行身份验证 auth.authenticate_user()注意如果您在Vertex AI Workbench中运行此笔记本请参考设置说明进行身份验证。案例实现1. 导入必要的库from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext, ServiceContext from llama_index.embeddings.huggingface import HuggingFaceEmbedding from llama_index.vector_stores.firestore import FirestoreVectorStore from llama_index.core import Settings2. 加载文档数据# 加载Paul Graham的文章数据 documents SimpleDirectoryReader( ../../examples/data/paul_graham ).load_data()3. 配置嵌入模型# 设置HuggingFace嵌入模型 embed_model HuggingFaceEmbedding(model_nameBAAI/bge-small-en-v1.5)4. 创建Firestore向量存储# 指定集合名称 COLLECTION_NAME test_collection # 创建Firestore向量存储 store FirestoreVectorStore(collection_nameCOLLECTION_NAME)5. 构建向量索引# 创建存储上下文 storage_context StorageContext.from_defaults(vector_storestore) # 创建服务上下文禁用LLM仅使用嵌入模型 service_context ServiceContext.from_defaults( llmNone, embed_modelembed_model ) # 从文档创建向量索引 index VectorStoreIndex.from_documents( documents, storage_contextstorage_context, service_contextservice_context )6. 执行查询# 创建查询引擎 query_engine index.as_query_engine() # 执行查询 res query_engine.query(What did the author do growing up?) # 打印最相关的文档片段 print(str(res.source_nodes[0].text))7. 应用元数据过滤from llama_index.core.vector_stores.types import ( MetadataFilters, ExactMatchFilter, MetadataFilter, ) # 创建元数据过滤器 filters MetadataFilters( filters[MetadataFilter(keyauthor, valuePaul Graham)] ) # 创建带过滤器的查询引擎 query_engine index.as_query_engine(filtersfilters) # 执行带过滤的查询 res query_engine.query(What did the author do growing up?) print(str(res.source_nodes[0].text))Firebase/Firestore特性自动扩展无需管理服务器根据需求自动扩展实时同步数据更改会实时同步到所有客户端离线支持支持离线数据访问和同步安全规则提供细粒度的数据访问控制案例效果本示例展示了Firestore向量存储的完整工作流程包括配置Google Cloud项目和Firestore数据库使用HuggingFace嵌入模型将文档转换为向量将Paul Graham的文章数据索引到Firestore向量存储中执行向量相似性搜索查询获取相关文档片段应用元数据过滤器优化搜索结果预期输出示例What I Worked On February 2021 Before college the two main things I worked on, outside of school, were writing and programming. I didnt 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 districts 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 villains 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.案例实现思路核心实现步骤环境准备创建Google Cloud项目启用Firestore API创建数据库身份验证配置Google Cloud身份验证确保代码可以访问Firestore依赖安装安装必要的Python包包括Firestore集成和嵌入模型数据准备使用SimpleDirectoryReader加载示例文档嵌入模型配置设置HuggingFace嵌入模型用于将文本转换为向量向量存储初始化创建FirestoreVectorStore实例指定集合名称索引构建使用加载的文档和配置的上下文创建VectorStoreIndex查询执行通过查询引擎执行向量相似性搜索过滤应用使用元数据过滤器优化搜索结果关键技术点Firestore集成通过FirestoreVectorStore类将Firestore作为向量数据库实现文档存储和检索HuggingFace嵌入使用BAAI/bge-small-en-v1.5模型生成高质量的文本向量表示向量索引通过VectorStoreIndex将文档转换为向量并存储在Firestore中元数据过滤使用MetadataFilters实现基于文档属性的精确搜索架构流程Firebase Firestore向量存储架构流程图示意图扩展建议功能扩展实现批量操作支持提高大规模数据处理效率添加高级过滤器支持如范围过滤、模糊匹配等集成混合搜索功能结合向量搜索和全文搜索实现实时数据同步支持多客户端协作性能优化优化索引策略提高查询响应速度实现向量压缩技术减少存储成本添加缓存机制减少重复查询开销使用异步操作提高并发处理能力应用场景扩展智能搜索构建理解语义的搜索引擎提供更精准的搜索结果内容推荐基于用户偏好和内容相似性实现个性化内容推荐知识问答构建智能问答系统基于文档内容回答用户问题集成建议与Google Cloud服务集成如Cloud Functions、Cloud Run实现无服务器应用与Firebase Authentication集成实现用户身份验证和授权与Google Cloud Storage集成实现大规模文件存储和处理与Vertex AI集成使用Google的高级AI模型增强应用功能总结本示例展示了如何使用Google Firestore作为向量数据库与LlamaIndex集成实现高效的文档存储和相似性搜索功能。Firestore作为Google Cloud提供的无服务器文档数据库具有自动扩展、实时同步和离线支持等特性非常适合构建现代AI应用。通过本示例我们学习了如何配置Google Cloud项目和Firestore数据库如何使用FirestoreVectorStore存储和检索向量数据如何执行向量相似性搜索查询如何应用元数据过滤来优化搜索结果Firebase Firestore向量存储的主要优势包括无服务器架构无需管理服务器自动扩展以满足需求实时同步数据更改会实时同步到所有客户端离线支持支持离线数据访问和同步安全规则提供细粒度的数据访问控制全球分布数据可以在全球多个区域复制提高访问速度这个示例为构建基于Google Cloud的向量搜索应用提供了基础可以根据具体需求扩展功能实现更复杂的AI应用场景。

更多文章