Query NebulaGraph Knowledge Graphs with LlamaIndex
LlamaPack that creates a NebulaGraph query engine with multiple retrieval modes-vector, keyword, hybrid, and custom combo-for knowledge graph entity search.
Why it matters
Leverage your NebulaGraph knowledge graph to answer complex questions by integrating with LlamaIndex. This pack enables various query strategies, from vector-based retrieval to hybrid approaches, for efficient data exploration.
Outcomes
What it gets done
Create diverse NebulaGraph query engines (vector, keyword, hybrid, combo).
Retrieve information from NebulaGraph using natural language queries.
Index and query knowledge graphs for entity and relationship extraction.
Integrate with Wikipedia data for knowledge graph population.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-pack-packs-nebulagraph-query-engine | bash Steps
Steps in the chain
Download the NebulaGraphQueryEnginePack using llamaindex-cli or the download_llama_pack function. Use `llamaindex-cli download-llamapack NebulaGraphQueryEnginePack --download-dir ./nebulagraph_pack` or `NebulaGraphQueryEnginePack = download_llama_pack("NebulaGraphQueryEnginePack", "./nebulagraph_pack")`
Load documents from a data source. Example: Use WikipediaReader to load data from Wikipedia pages: `loader = WikipediaReader()` and `docs = loader.load_data(pages=["Paleolithic diet"], auto_suggest=False)`
Retrieve NebulaGraph connection parameters (username, password, ip_and_port) from credentials.json or your configuration source.
Set up configuration parameters including space_name, edge_types, rel_prop_names, tags, and max_triplets_per_chunk. Example: space_name="paleo_diet", edge_types=["relationship"], tags=["entity"], max_triplets_per_chunk=10
Instantiate NebulaGraphQueryEnginePack with credentials, configuration parameters, and documents. Optionally specify query_engine_type from NebulaGraphQueryEngineType (KG_KEYWORD, KG_HYBRID, RAW_VECTOR, RAW_VECTOR_KG_COMBO, KG_QE, or KG_RAG_RETRIEVER). Defaults to vector-based entity retrieval if not specified.
Run a query using the pack's run() function or directly access the query_engine. Example: `response = nebulagraph_pack.run("Tell me about the benefits of paleo diet.")` or `response = query_engine.query("query_str")`
Overview
NebulaGraph Query Engine Pack
What it does
A LlamaPack that creates and configures a NebulaGraph query engine for natural language querying of knowledge graphs, supporting multiple retrieval strategies including vector-based, keyword-based, and hybrid entity retrieval.
How it connects
Use this pack when you need to query knowledge graphs stored in NebulaGraph with natural language, retrieve entities and relationships through various search methods, or build applications that combine vector similarity with knowledge graph traversal.
Source README
NebulaGraph Query Engine Pack
This LlamaPack creates a NebulaGraph query engine, and executes its query function. This pack offers the option of creating multiple types of query engines, namely:
- Knowledge graph vector-based entity retrieval (default if no query engine type option is provided)
- Knowledge graph keyword-based entity retrieval
- Knowledge graph hybrid entity retrieval
- Raw vector index retrieval
- Custom combo query engine (vector similarity + KG entity retrieval)
- KnowledgeGraphQueryEngine
- KnowledgeGraphRAGRetriever
CLI Usage
You can download llamapacks directly using llamaindex-cli, which comes installed with the llama-index python package:
llamaindex-cli download-llamapack NebulaGraphQueryEnginePack --download-dir ./nebulagraph_pack
You can then inspect the files at ./nebulagraph_pack and use them as a template for your own project!
Code Usage
You can download the pack to a ./nebulagraph_pack directory:
from llama_index.core.llama_pack import download_llama_pack
### download and install dependencies
NebulaGraphQueryEnginePack = download_llama_pack(
"NebulaGraphQueryEnginePack", "./nebulagraph_pack"
)
From here, you can use the pack, or inspect and modify the pack in ./nebulagraph_pack.
Then, you can set up the pack like so:
pip install llama-index-readers-wikipedia
### Load the docs (example of Paleo diet from Wikipedia)
from llama_index.readers.wikipedia import WikipediaReader
loader = WikipediaReader()
docs = loader.load_data(pages=["Paleolithic diet"], auto_suggest=False)
print(f"Loaded {len(docs)} documents")
### get NebulaGraph credentials (assume it's stored in credentials.json)
with open("credentials.json") as f:
nebulagraph_connection_params = json.load(f)
username = nebulagraph_connection_params["username"]
password = nebulagraph_connection_params["password"]
ip_and_port = nebulagraph_connection_params["ip_and_port"]
space_name = "paleo_diet"
edge_types, rel_prop_names = ["relationship"], ["relationship"]
tags = ["entity"]
max_triplets_per_chunk = 10
### create the pack
nebulagraph_pack = NebulaGraphQueryEnginePack(
username=username,
password=password,
ip_and_port=ip_and_port,
space_name=space_name,
edge_types=edge_types,
rel_prop_names=rel_prop_names,
tags=tags,
max_triplets_per_chunk=max_triplets_per_chunk,
docs=docs,
)
Optionally, you can pass in the query_engine_type from NebulaGraphQueryEngineType to construct NebulaGraphQueryEnginePack. If query_engine_type is not defined, it defaults to Knowledge Graph vector based entity retrieval.
from llama_index.core.packs.nebulagraph_query_engine.base import (
NebulaGraphQueryEngineType,
)
### create the pack
nebulagraph_pack = NebulaGraphQueryEnginePack(
username=username,
password=password,
ip_and_port=ip_and_port,
space_name=space_name,
edge_types=edge_types,
rel_prop_names=rel_prop_names,
tags=tags,
max_triplets_per_chunk=max_triplets_per_chunk,
docs=docs,
query_engine_type=NebulaGraphQueryEngineType.KG_HYBRID,
)
NebulaGraphQueryEnginePack is a enum defined as follows:
class NebulaGraphQueryEngineType(str, Enum):
"""NebulaGraph query engine type"""
KG_KEYWORD = "keyword"
KG_HYBRID = "hybrid"
RAW_VECTOR = "vector"
RAW_VECTOR_KG_COMBO = "vector_kg"
KG_QE = "KnowledgeGraphQueryEngine"
KG_RAG_RETRIEVER = "KnowledgeGraphRAGRetriever"
The run() function is a light wrapper around query_engine.query(), see a sample query below.
response = nebulagraph_pack.run("Tell me about the benefits of paleo diet.")
You can also use modules individually.
### call the query_engine.query()
query_engine = nebulagraph_pack.query_engine
response = query_engine.query("query_str")
Step 1: Download NebulaGraphQueryEnginePack
Download the NebulaGraphQueryEnginePack using llamaindex-cli or the download_llama_pack function. Use `llamaindex-cli download-llamapack NebulaGraphQueryEnginePack --download-dir ./nebulagraph_pack` or `NebulaGraphQueryEnginePack = download_llama_pack("NebulaGraphQueryEnginePack", "./nebulagraph_pack")`Step 2: Load documents
Load documents from a data source. Example: Use WikipediaReader to load data from Wikipedia pages: `loader = WikipediaReader()` and `docs = loader.load_data(pages=["Paleolithic diet"], auto_suggest=False)`
Step 3: Get NebulaGraph credentials
Retrieve NebulaGraph connection parameters (username, password, ip_and_port) from credentials.json or your configuration source.
Step 4: Configure pack parameters
Set up configuration parameters including space_name, edge_types, rel_prop_names, tags, and max_triplets_per_chunk. Example: space_name="paleo_diet", edge_types=["relationship"], tags=["entity"], max_triplets_per_chunk=10
Step 5: Create the NebulaGraphQueryEnginePack
Instantiate NebulaGraphQueryEnginePack with credentials, configuration parameters, and documents. Optionally specify query_engine_type from NebulaGraphQueryEngineType (KG_KEYWORD, KG_HYBRID, RAW_VECTOR, RAW_VECTOR_KG_COMBO, KG_QE, or KG_RAG_RETRIEVER). Defaults to vector-based entity retrieval if not specified.
Step 6: Execute query
Run a query using the pack's run() function or directly access the query_engine. Example: `response = nebulagraph_pack.run("Tell me about the benefits of paleo diet.")` or `response = query_engine.query("query_str")`Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.