----------------------
To ask the same query to all documents in the docstore, you can use the query engine of LlamaIndex. Here's a step-by-step guide:
- First, you need to import the necessary modules and set up your environment. This includes setting up logging and applying
nest_asyncio
.
import nest_asyncio
import logging
import sys
import os
from llama_index import (
SimpleDirectoryReader,
ServiceContext,
LLMPredictor,
StorageContext,
)
from llama_index import VectorStoreIndex, SummaryIndex, SimpleKeywordTableIndex
from llama_index.composability import ComposableGraph
from llama_index.llms import OpenAI
from llama_index.response.notebook_utils import display_response
nest_asyncio.apply()
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
- Load your documents and parse them into nodes.
reader = SimpleDirectoryReader("../paul_graham_essay/data")
documents = reader.load_data()
from llama_index.node_parser import SimpleNodeParser
nodes = SimpleNodeParser.from_defaults().get_nodes_from_documents(documents)
- Add your documents to the docstore. In this example, we're using a MongoDB docstore.
MONGO_URI = os.environ["MONGO_URI"]
from llama_index.storage.docstore import MongoDocumentStore
from llama_index.storage.index_store.mongo_index_store import MongoIndexStore
storage_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)
- Define your indexes. Each index uses the same underlying Node.