run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.47k stars 5.21k forks source link

[Question]: I already have a graph knowledge stored in neo4j. is there a way to directly use it for Property Graph index? #15005

Open justinzyw opened 3 months ago

justinzyw commented 3 months ago

Question Validation

Question

I already have a graph knowledge stored in neo4j. is there a way to directly use it for Property Graph index?

dosubot[bot] commented 3 months ago

Yes, you can directly use your existing graph knowledge stored in Neo4j for a Property Graph index. Here is how you can connect to and use an existing graph:

from llama_index.graph_stores.neo4j import Neo4jPropertyGraphStore
from llama_index.core import PropertyGraphIndex
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI

graph_store = Neo4jPropertyGraphStore(
    username="neo4j",
    password="neo4j",
    url="bolt://localhost:7687",
    database="neo4j"
)

index = PropertyGraphIndex.from_existing(
    property_graph_store=graph_store,
    llm=OpenAI(model="gpt-3.5-turbo", temperature=0.3),
    embed_model=OpenAIEmbedding(model_name="text-embedding-3-small"),
)

From here, you can still insert more documents:

from llama_index.core import Document

document = Document(text="LlamaIndex is great!")

index.insert(document)

And retrieve nodes:

nodes = index.as_retriever(include_text=False).retrieve("LlamaIndex")

print(nodes[0].text)

For full details on construction, retrieval, and querying of a property graph, see the full docs page [1][2][3][4].

To continue talking to Dosu, mention @dosu.

logan-markewich commented 3 months ago

Yes*

But you are restricted to

justinzyw commented 3 months ago

many thanks!

I am not familiar with these retrievers and will look into them.

it would be great that llama index could read an existing external graph data as document and index it as property graph

logan-markewich commented 3 months ago

yea thats pretty ambitious/difficult to do actually

justinzyw commented 3 months ago

by reading the documents, I found that there is a way to manually insert entities and relations as below. I can live with it too as I can somehow transform my knowledge graph into insert statements.. The only problem is that, as I already have the id of each entity, would it be possible to directly set the id of entity? @dosu

from llama_index.core.graph_stores.types import EntityNode, ChunkNode, Relation

Create a two entity nodes

entity1 = EntityNode(label="PERSON", name="Logan", properties={"age": 28}) entity2 = EntityNode(label="ORGANIZATION", name="LlamaIndex")

Create a relation

relation = Relation( label="WORKS_FOR", source_id=entity1.id, target_id=entity2.id, properties={"since": 2023}, )

dosubot[bot] commented 3 months ago

Yes, it is possible to directly set the ID of an entity when creating EntityNode instances. You can do this by providing the id parameter during the creation of the EntityNode. Here is an example:

from llama_index.core.graph_stores.types import EntityNode, Relation

# Create two entity nodes with custom IDs
entity1 = EntityNode(id="custom_id_1", label="PERSON", name="Logan", properties={"age": 28})
entity2 = EntityNode(id="custom_id_2", label="ORGANIZATION", name="LlamaIndex")

# Create a relation
relation = Relation(
    label="WORKS_FOR",
    source_id=entity1.id,
    target_id=entity2.id,
    properties={"since": 2023},
)

This allows you to use your existing IDs directly when creating the entities [1][2].

To continue talking to Dosu, mention @dosu.