MongoDB 演示
本指南向您展示如何直接使用我们基于MongoDB的DocumentStore抽象层。通过将节点存入文档存储库,您可以在同一底层文档存储上定义多个索引,而无需在不同索引间重复存储数据。
如果您在 Colab 上打开这个笔记本,您可能需要安装 LlamaIndex 🦙。
%pip install llama-index-storage-docstore-mongodb%pip install llama-index-storage-index-store-mongodb%pip install llama-index-llms-openai!pip install llama-indeximport nest_asyncio
nest_asyncio.apply()import loggingimport sysimport os
logging.basicConfig(stream=sys.stdout, level=logging.INFO)logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))from llama_index.core import SimpleDirectoryReader, StorageContextfrom llama_index.core import VectorStoreIndex, SimpleKeywordTableIndexfrom llama_index.core import SummaryIndexfrom llama_index.core import ComposableGraphfrom llama_index.llms.openai import OpenAIfrom llama_index.core.response.notebook_utils import display_responsefrom llama_index.core import Settings!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'reader = SimpleDirectoryReader("./data/paul_graham/")documents = reader.load_data()from llama_index.core.node_parser import SentenceSplitter
nodes = SentenceSplitter().get_nodes_from_documents(documents)MONGO_URI = os.environ["MONGO_URI"]from llama_index.storage.docstore.mongodb import MongoDocumentStorefrom llama_index.storage.index_store.mongodb import MongoIndexStorestorage_context = StorageContext.from_defaults( docstore=MongoDocumentStore.from_uri(uri=MONGO_URI), index_store=MongoIndexStore.from_uri(uri=MONGO_URI),)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)# NOTE: the docstore still has the same nodeslen(storage_context.docstore.docs)# NOTE: docstore and index_store is persisted in MongoDB by default# NOTE: here only need to persist simple vector store to diskstorage_context.persist()# note down index IDslist_id = summary_index.index_idvector_id = vector_index.index_idkeyword_id = keyword_table_index.index_idfrom llama_index.core import load_index_from_storage
# re-create storage contextstorage_context = StorageContext.from_defaults( docstore=MongoDocumentStore.from_uri(uri=MONGO_URI), index_store=MongoIndexStore.from_uri(uri=MONGO_URI),)
# load indicessummary_index = load_index_from_storage( storage_context=storage_context, index_id=list_id)vector_index = load_index_from_storage( storage_context=storage_context, index_id=vector_id)keyword_table_index = load_index_from_storage( storage_context=storage_context, index_id=keyword_id)chatgpt = OpenAI(temperature=0, model="gpt-3.5-turbo")
Settings.llm = chatgptSettings.chunk_size = 1024query_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)