Find answers from the community

Updated last year

it is possible from llama_index.storage.storage_context import StorageContextgraph_store

@kapa.ai it is possible from llama_index.storage.storage_context import StorageContext

graph_store = SimpleGraphStore()
storage_context = StorageContext.from_defaults(graph_store=graph_store)

NOTE: can take a while!

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

in Neo4j using embeddoings?
k
R
6 comments
Please stand by @RUPP as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Yes, it is possible to use KnowledgeGraphIndex.from_documents with Neo4jGraphStore and embeddings in LlamaIndex. Here is an example of how you can do it:

Plain Text
from llama_index import KnowledgeGraphIndex, ServiceContext
from llama_index.storage.storage_context import StorageContext
from llama_index.graph_stores import Neo4jGraphStore

# Define your Neo4jGraphStore with your specific parameters
graph_store = Neo4jGraphStore(
    username=username,
    password=password,
    url=url,
    database=database,
)

storage_context = StorageContext.from_defaults(graph_store=graph_store)

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

# Load your documents
documents = SimpleDirectoryReader(
    "../../../../examples/paul_graham_essay/data"
).load_data()

# Build the KnowledgeGraphIndex
index = KnowledgeGraphIndex.from_documents(
    documents,
    storage_context=storage_context,
    max_triplets_per_chunk=2,
    service_context=service_context,
    include_embeddings=True,
)


Please replace username, password, url, and database with your actual Neo4j credentials and database information. Also, ensure that the path to your documents is correct.

This code will create a KnowledgeGraphIndex from your documents using a Neo4jGraphStore as the graph store, and it will include embeddings in the index.

Sources:
  • Neo4jGraphStore example ()
  • StorageContext import ()
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai how use embeddings in this case/
Let me check the knowledge base for you. One moment! :books:
----------------------
To use embeddings in this case, you need to set include_embeddings=True when creating the KnowledgeGraphIndex. This will include embeddings in the index which can be used for querying later. Here is an example:

Plain Text
from llama_index import KnowledgeGraphIndex, ServiceContext
from llama_index.storage.storage_context import StorageContext
from llama_index.graph_stores import Neo4jGraphStore

# Define your Neo4jGraphStore with your specific parameters
graph_store = Neo4jGraphStore(
    username=username,
    password=password,
    url=url,
    database=database,
)

storage_context = StorageContext.from_defaults(graph_store=graph_store)

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

# Load your documents
documents = SimpleDirectoryReader(
    "../../../../examples/paul_graham_essay/data"
).load_data()

# Build the KnowledgeGraphIndex with embeddings
index = KnowledgeGraphIndex.from_documents(
    documents,
    storage_context=storage_context,
    max_triplets_per_chunk=2,
    service_context=service_context,
    include_embeddings=True,  # Include embeddings
)


After creating the index with embeddings, you can use the as_query_engine method to create a query engine that uses these embeddings. Here is an example:

Plain Text
query_engine = index.as_query_engine(
    include_text=True,
    response_mode="tree_summarize",
    embedding_mode="hybrid",
    similarity_top_k=5,
)


In this example, embedding_mode="hybrid" means that the query engine will use both text and embeddings for querying.
Sources:
  • Neo4jGraphStore example ()
  • StorageContext import ()
----------------------
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