Find answers from the community

Updated last year

How can i load and persist dada of a KnowledgeGraphIndex

@kapa.ai How can i load and persist dada of a KnowledgeGraphIndex
k
3 comments
Please stand by @RUPP as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To load and persist data of a KnowledgeGraphIndex in LlamaIndex, you can follow these steps:

  1. Load Data: You can load data from a source like Wikipedia using the download_loader function. Here's an example of how to load data for "Guardians of the Galaxy Vol. 3" from Wikipedia:
Plain Text
from llama_index import download_loader

WikipediaReader = download_loader("WikipediaReader")

loader = WikipediaReader()

documents = loader.load_data(
    pages=["Guardians of the Galaxy Vol. 3"], auto_suggest=False
)

(Source: GitHub ())

  1. Build the KnowledgeGraphIndex: After loading the data, you can create a KnowledgeGraphIndex from the documents. Here's an example:
Plain Text
from llama_index import ServiceContext, KnowledgeGraphIndex
from llama_index.storage.storage_context import StorageContext
from llama_index.llms import OpenAI

# define LLM
llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
service_context = ServiceContext.from_defaults(llm=llm, chunk_size=512)

storage_context = StorageContext.from_defaults(graph_store=graph_store)

index = KnowledgeGraphIndex.from_documents(
    documents,
    max_triplets_per_chunk=2,
    storage_context=storage_context,
    service_context=service_context,
)

(Source: GitHub ())

  1. Persist Data: Once you have your data indexed, you can persist it to disk using the .persist() method. This will write all the data to disk at the location specified. Here's an example:
Plain Text
index.storage_context.persist(persist_dir="<persist_dir>")

(Source: Documentation ())
  1. Load Persisted Data: You can avoid re-loading and re-indexing your data by loading the persisted index like this:
Plain Text
from llama_index import StorageContext, load_index_from_storage

# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir="<persist_dir>")

# load index
index = load_index_from_storage(storage_context)

(Source: Documentation ())

Please replace <persist_dir> with the directory where you want to store or load the persisted data.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord