【RAG】【vector_stores】【docstore09】Tablestore文档存储示例分析

张开发
2026/4/6 5:50:44 15 分钟阅读

分享文章

【RAG】【vector_stores】【docstore09】Tablestore文档存储示例分析
1. 案例目标本示例展示了如何使用阿里云Tablestore作为LlamaIndex的文档存储、索引存储和向量存储后端实现文档的持久化存储和多种索引类型的创建与查询。2. 技术栈与核心依赖llama-index-storage-docstore-tablestorellama-index-embeddings-dashscopellama-index-llms-dashscopedashscopetablestore核心组件TablestoreDocumentStore - 文档存储TablestoreIndexStore - 索引存储TablestoreVectorStore - 向量存储DashScopeEmbedding - 文本嵌入DashScope - 大语言模型3. 环境配置环境变量配置# Tablestore配置 os.environ[tablestore_end_point] your_endpoint os.environ[tablestore_instance_name] your_instance_name os.environ[tablestore_access_key_id] your_access_key_id os.environ[tablestore_access_key_secret] your_access_key_secret # DashScope API密钥 os.environ[DASHSCOPE_API_KEY] your_dashscope_api_key4. 案例实现步骤1: 安装依赖!pip install llama-index-storage-docstore-tablestore !pip install llama-index-embeddings-dashscope !pip install llama-index-llms-dashscope !pip install dashscope !pip install tablestore步骤2: 下载和准备数据# 下载Paul Graham的文章数据 !mkdir -p data/paul_graham/ !wget https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt -O data/paul_graham/paul_graham_essay.txt # 加载文档 documents SimpleDirectoryReader(./data/paul_graham/).load_data() # 解析为节点 parser SentenceSplitter() nodes parser.get_nodes_from_documents(documents)步骤3: 配置嵌入模型和LLMembedder DashScopeEmbedding( model_nameDashScopeTextEmbeddingModels.TEXT_EMBEDDING_V3, # 默认维度为1024 text_typeDashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT, ) dashscope_llm DashScope( model_nameDashScopeGenerationModels.QWEN_MAX, api_keyos.environ[DASHSCOPE_API_KEY], ) Settings.llm dashscope_llm步骤4: 初始化Tablestore存储组件# 初始化文档存储 docstore TablestoreDocumentStore.from_config( endpointos.getenv(tablestore_end_point), instance_nameos.getenv(tablestore_instance_name), access_key_idos.getenv(tablestore_access_key_id), access_key_secretos.getenv(tablestore_access_key_secret), ) # 初始化索引存储 index_store TablestoreIndexStore.from_config( endpointos.getenv(tablestore_end_point), instance_nameos.getenv(tablestore_instance_name), access_key_idos.getenv(tablestore_access_key_id), access_key_secretos.getenv(tablestore_access_key_secret), ) # 初始化向量存储 vector_store TablestoreVectorStore( endpointos.getenv(tablestore_end_point), instance_nameos.getenv(tablestore_instance_name), access_key_idos.getenv(tablestore_access_key_id), access_key_secretos.getenv(tablestore_access_key_secret), vector_dimension1024, # 嵌入维度为1024 ) vector_store.create_table_if_not_exist() vector_store.create_search_index_if_not_exist() # 创建存储上下文 storage_context StorageContext.from_defaults( docstoredocstore, index_storeindex_store, vector_storevector_store )步骤5: 添加文档并创建多种索引# 添加文档到存储 storage_context.docstore.add_documents(nodes) # 创建摘要索引 summary_index SummaryIndex(nodes, storage_contextstorage_context) # 创建向量索引 vector_index VectorStoreIndex( nodes, insert_batch_size20, embed_modelembedder, storage_contextstorage_context, ) # 创建关键词表索引 keyword_table_index SimpleKeywordTableIndex( nodesnodes, storage_contextstorage_context, llmdashscope_llm, )步骤6: 持久化存储# 注意文档存储和索引存储默认持久化到Tablestore # 这里只需要持久化向量存储到磁盘 storage_context.persist() # 记录索引ID list_id summary_index.index_id vector_id vector_index.index_id keyword_id keyword_table_index.index_id步骤7: 从存储加载索引# 重新创建存储上下文 storage_context StorageContext.from_defaults( docstoredocstore, index_storeindex_store, vector_storevector_store ) # 加载摘要索引 summary_index load_index_from_storage( storage_contextstorage_context, index_idlist_id, ) # 加载关键词表索引 keyword_table_index load_index_from_storage( llmdashscope_llm, storage_contextstorage_context, index_idkeyword_id, ) # 加载向量索引需要指定vector_store vector_index load_index_from_storage( insert_batch_size20, embed_modelembedder, storage_contextstorage_context, index_idvector_id, )步骤8: 测试查询功能# 配置查询参数 Settings.llm dashscope_llm Settings.chunk_size 1024 # 摘要查询 query_engine summary_index.as_query_engine() list_response query_engine.query(What is a summary of this document?) # 向量查询 query_engine vector_index.as_query_engine() vector_response query_engine.query(What did the author do growing up?) # 关键词查询 query_engine keyword_table_index.as_query_engine() keyword_response query_engine.query( What did the author do after his time at YC? )5. 案例效果查询结果示例摘要查询结果对整个文档的摘要回答向量查询结果作者在成长过程中做了什么作者在成长过程中参与了写作和编程。最初他写短篇故事虽然现在认为这些故事不是很好因为它们缺乏情节更多关注角色的情感。在编程方面作者在初中时从IBM 1401开始尝试用打孔卡编写Fortran程序。后来在得到TRS-80微型计算机后作者更深入地研究编程创建了简单的游戏、一个预测模型火箭飞行高度的程序甚至一个被父亲用于写作的文字处理器。关键词查询结果作者在离开YC后做了什么在离开YC后作者决定投身绘画致力于看看自己能变得多好。他在2014年大部分时间都专注于这件事。然而到11月他失去了兴趣并停止了。之后他回归写文章甚至涉足创业公司以外的话题。2015年3月他还重新开始研究Lisp。6. 案例实现思路核心设计思路统一存储后端使用Tablestore作为文档、索引和向量的统一存储后端实现数据的集中管理多种索引类型创建摘要索引、向量索引和关键词表索引满足不同的查询需求持久化策略文档和索引存储默认持久化到Tablestore向量存储也配置到Tablestore阿里云生态集成使用DashScope提供的嵌入模型和大语言模型与Tablestore形成完整的阿里云AI解决方案7. 扩展建议可能的扩展方向添加更多索引类型如树索引、知识图谱索引等实现增量更新机制支持文档的动态添加和删除添加访问控制和权限管理确保数据安全实现查询缓存机制提高常用查询的响应速度添加性能监控和日志记录功能实现多租户支持为不同用户提供隔离的存储空间集成更多阿里云服务如函数计算、消息队列等8. 总结本示例展示了如何使用阿里云Tablestore作为LlamaIndex的存储后端实现文档的持久化存储和多种索引类型的创建与查询。通过TablestoreDocumentStore、TablestoreIndexStore和TablestoreVectorStore的组合可以构建一个完整的、基于阿里云生态的文档检索系统。该方案特别适合已经在使用阿里云服务的企业可以无缝集成到现有系统中实现高效的文档存储和检索功能。

更多文章