当节点不是来自 __Entity__ 类型时,如何找到邻居节点? (Neo4J)

问题描述 投票:0回答:1

我是学习 Neo4J 和 Cypher 查询的初学者,并按照此 GitHub 使用知识图构建混合 RAG。我已使用 Cypher 查询向 Neo4j 工作区添加了一个图表。命令示例如下:

MERGE (nizhny_novgorod:birthPlace {id: "Nizhny Novgorod"})
MERGE (large_language_models_like_chatgpt:characteristics {id: "large language models like ChatGPT"})
MERGE (richard_zemel:person {id: "Richard Zemel"})
MERGE (mixtures_of_experts:invention {id: "mixtures of experts"})
MERGE (december_6_1947:date {id: "December 6, 1947"})
MERGE (nobel_prize_in_physics:award {id: "Nobel Prize in Physics"})

MERGE (geoffrey_hinton)-[:bornIn]->(london)
MERGE (geoffrey_hinton)-[:bornAt]->(december_6_1947)
MERGE (geoffrey_hinton)-[:worksAt]->(university_of_toronto)
MERGE (geoffrey_hinton)-[:hasNationality]->(britishcanadian)
MERGE (geoffrey_hinton)-[:hasRole]->(cognitive_psychologist_and_computer_scientist)
MERGE (geoffrey_hinton)-[:coAuthored]->(david_rumelhart)
MERGE (geoffrey_hinton)-[:coAuthored]->(ronald_j_williams)

问题是,当我运行

 __Entity__
命令时,
MATCH (n:__Entity__) RETURN n LIMIT 25;
是空的,并且它不返回任何节点。

我在使用以下函数检索知识图时尝试查找邻居节点时遇到了这个问题。

# Fulltext index query
def structured_retriever(question: str) -> str:
    """
    Or Graph Retriever
    Collects the neighborhood of entities mentioned in the question
    """
    result = ""
    entities = entity_chain.invoke({"question": question})
    print(f"Extracted entities: {entities.names}")
    for entity in entities.names:
        query = generate_full_text_query(entity)
        print(f"Generated query: {query}")
        response = graph.query(
            """CALL db.index.fulltext.queryNodes('entity', $query, {limit:2})
            YIELD node, score
            CALL {
              WITH node
              MATCH (node)-[r]->(neighbor)
              RETURN node.id + ' - ' + type(r) + ' -> ' + neighbor.id AS output
              UNION ALL
              WITH node
              MATCH (node)<-[r]-(neighbor)
              RETURN neighbor.id + ' - ' + type(r) + ' -> ' +  node.id AS output
            }
            RETURN output LIMIT 50
            """,
            {"query": query},
        )
        print(f"Query response: {response}")
        if response:
            result += "\n".join([el['output'] for el in response])
        else:
            print(f"No results found for entity: {entity}")
    return result

上面的查询不返回任何内容。例如,对于以下问题:

print(structured_retriever("Where was Geoffrey Hinton born"))

输出:

Extracted entities: ['Geoffrey Hinton']
Generated query: Geoffrey~2 AND Hinton~2
Query response: []
No results found for entity: Geoffrey Hinton
python neo4j knowledge-graph rag
1个回答
0
投票

使用以下 Cypher 查询将实体(或任何其他名称)索引添加到所有节点类型:

CREATE FULLTEXT INDEX entity FOR (e:award | birthPlace | characteristics | date | degree | Document | institution | invention | nationality | person | relation | role) ON EACH [e.id]

您可以使用以下命令进行检查:

SHOW INDEXES
© www.soinside.com 2019 - 2024. All rights reserved.