Skip to main content

LlamaIndex

LlamaIndex can consume documents retrieved from an indexed ApertureDB descriptor class. Use the same embedding model to encode stored documents and the search query. This example reads a class whose descriptors have a text property and converts the matches to LlamaIndex documents.

from aperturedb.CommonLibrary import create_connector
from aperturedb.Descriptors import Descriptors
from llama_index.core import Document, VectorStoreIndex

descriptors = Descriptors(create_connector())
descriptors.find_similar("Documents", query_embedding, 5, results={"list": ["text"]})
documents = [Document(text=row["text"]) for row in descriptors.response]
index = VectorStoreIndex.from_documents(documents)
answer = index.as_query_engine().query(question)

Configure LlamaIndex's embedding model and LLM before building the local index.

Graph database​

Aperturedb can also be used as a PropertyGraph in LlamaIndex. There is an implementation in the github respository

An example to auto build a Knowledge Graph from a unstructured (text form) data using an LLM can be implemented as follows:

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'

Ingesting the nodes

from llama_index.graph_stores.ApertureDB import ApertureDBGraphStore
from llama_index.core import SimpleDirectoryReader, PropertyGraphIndex
from llama_index.core.indices.property_graph import SchemaLLMPathExtractor
from llama_index.llms.openai import OpenAI

graph_store = ApertureDBGraphStore()
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()

kg_extractor = SchemaLLMPathExtractor(
llm=OpenAI(model="gpt-3.5-turbo", temperature=0.0),
strict=True
)

index = PropertyGraphIndex.from_documents(
documents,
embed_kg_nodes=False,
kg_extractors=[kg_extractor],
property_graph_store=graph_store,
show_progress=True,
)

Querying the nodes.

from llama_index.graph_stores.ApertureDB import ApertureDBGraphStore
from llama_index.core import SimpleDirectoryReader, PropertyGraphIndex
from llama_index.core.indices.property_graph import SchemaLLMPathExtractor
from llama_index.llms.openai import OpenAI

graph_store = ApertureDBGraphStore()

kg_extractor = SchemaLLMPathExtractor(
llm=OpenAI(model="gpt-3.5-turbo", temperature=0.0),
strict=True,
)

index = PropertyGraphIndex.from_existing(
embed_kg_nodes=False,
kg_extractors=[kg_extractor],
property_graph_store=graph_store,
show_progress=True
)

def run_queries(query_engine):
query_str = [
"What has Paul graham worked on?",
"Who has Paul Graham worked with?"
]
for qs in query_str:
response = query_engine.query(qs)
print(f"{qs=}")
print(f"{response.response=}")
print("=" * 50)

run_queries(query_engine)