跳转到主要内容

category

在使用Neo4j和LangChain的RAG应用程序中构建和检索知识图信息的实用指南


编者按:以下是Tomaz Bratanic的客座博客文章,他专注于Neo4j的Graph ML和GenAI研究。Neo4j是一家图形数据库和分析公司,它帮助组织深入、轻松、快速地发现数十亿数据连接中隐藏的关系和模式。


图检索增强生成(Graph RAG)作为传统矢量搜索检索方法的强大补充,正在获得发展势头。这种方法利用了图数据库的结构化特性,将数据组织为节点和关系,以增强检索信息的深度和上下文性。

 

Example knowledge graph.

图形非常擅长以结构化的方式表示和存储异构和互连的信息,可以轻松地捕捉不同数据类型之间的复杂关系和属性。相比之下,矢量数据库往往难以处理此类结构化信息,因为它们的优势在于通过高维矢量处理非结构化数据。在您的RAG应用程序中,您可以将结构化图形数据与通过非结构化文本进行的矢量搜索相结合,以实现两全其美,这正是我们将在本博客文章中所做的。

知识图很好,但你是如何创建知识图的?构建知识图通常是利用基于图的数据表示能力的最具挑战性的步骤。它涉及到数据的收集和结构化,这需要对领域和图形建模有深入的了解。为了简化这个过程,我们一直在试验LLM。LLM凭借其对语言和上下文的深刻理解,可以自动化知识图创建过程的重要部分。通过分析文本数据,这些模型可以识别实体,了解它们之间的关系,并建议如何在图结构中最好地表示它们。作为这些实验的结果,我们在LangChain中添加了图形构建模块的第一个版本,我们将在这篇博客文章中进行演示。

该代码可在GitHub上获得。

Neo4j环境设置


您需要设置一个Neo4j实例,请参阅本文中的示例。最简单的方法是在Neo4j Aura上启动一个免费实例,该实例提供Neo4j数据库的云实例。或者,您也可以通过下载Neo4j Desktop应用程序并创建本地数据库实例来设置Neo4j数据库的本地实例。

os.environ["OPENAI_API_KEY"] = "sk-"
os.environ["NEO4J_URI"] = "bolt://localhost:7687"
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "password"

graph = Neo4jGraph()

此外,您必须提供一个OpenAI密钥,因为我们将在这篇博客文章中使用他们的模型。

数据摄入


在这个演示中,我们将使用伊丽莎白一世的维基百科页面。我们可以使用LangChain加载程序无缝地从维基百科中获取和分割文档。

# Read the wikipedia article
raw_documents = WikipediaLoader(query="Elizabeth I").load()

# Define chunking strategy
text_splitter = TokenTextSplitter(chunk_size=512, chunk_overlap=24)
documents = text_splitter.split_documents(raw_documents[:3])

 

现在是时候根据检索到的文档构建一个图了。为此,我们实现了一个LLMGraphTransformermodule,它大大简化了在图数据库中构建和存储知识图的过程。

llm=ChatOpenAI(temperature=0, model_name="gpt-4-0125-preview")
llm_transformer = LLMGraphTransformer(llm=llm)

# Extract graph data
graph_documents = llm_transformer.convert_to_graph_documents(documents)

# Store to neo4j
graph.add_graph_documents(
  graph_documents, 
  baseEntityLabel=True, 
  include_source=True
)

 

您可以定义知识图生成链要使用的LLM。目前,我们只支持OpenAI和Mistral的函数调用模型。然而,我们计划在未来扩大LLM的选择范围。在本例中,我们使用的是最新的GPT-4。请注意,生成的图的质量在很大程度上取决于所使用的模型。理论上,你总是想用最能干的人。LLM图形转换器返回图形文档,这些文档可以通过add_graph_documents方法导入到Neo4j。baseEntityLabel参数为每个节点分配一个额外的__Entity__标签,从而提高索引和查询性能。include_source参数将节点链接到其原始文档,便于数据跟踪和上下文理解。

您可以在Neo4j浏览器中检查生成的图形。

Part of the generated graph.

请注意,为了清晰起见,此图像仅表示生成的图形的一部分。


RAG的混合检索


在生成图之后,我们将使用一种混合检索方法,该方法将矢量索引和关键字索引与RAG应用程序的图检索相结合。

Combining hybrid (vector + keyword) and graph retrieval methods. Image by author.

该图说明了从用户提出问题开始的检索过程,然后将问题引导到RAG检索器。该检索器使用关键字和矢量搜索来搜索非结构化文本数据,并将其与从知识图中收集的信息相结合。由于Neo4j同时具有关键字索引和矢量索引,因此可以使用单个数据库系统实现所有三种检索选项。从这些来源收集的数据被输入LLM,以生成并提供最终答案。

非结构化数据检索器


您可以使用Neo4jVector.from_existing_graph方法将关键字和矢量检索添加到文档中。此方法为混合搜索方法配置关键字和矢量搜索索引,以标记为Document的节点为目标。此外,如果文本嵌入值丢失,它还会计算这些值。

vector_index = Neo4jVector.from_existing_graph(
    OpenAIEmbeddings(),
    search_type="hybrid",
    node_label="Document",
    text_node_properties=["text"],
    embedding_node_property="embedding"
)


然后可以使用similarity_search方法调用向量索引。

图形检索器


另一方面,配置图检索更为复杂,但提供了更多的自由度。在本例中,我们将使用全文索引来标识相关节点,然后返回它们的直接邻域。

Graph retriever. Image by author.

图形检索器首先识别输入中的相关实体。为了简单起见,我们指示LLM识别人员、组织和地点。为了实现这一点,我们将使用LCEL和新添加的with_structured_output方法来实现这一目标。

# Extract entities from text
class Entities(BaseModel):
    """Identifying information about entities."""

    names: List[str] = Field(
        ...,
        description="All the person, organization, or business entities 
        that " "appear in the text",
    )

prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are extracting organization and person entities from the 
            text.",
        ),
        (
            "human",
            "Use the given format to extract information from the
             following"
            "input: {question}",
        ),
    ]
)

entity_chain = prompt | llm.with_structured_output(Entities)

让我们测试一下:

entity_chain.invoke({"question": "Where was Amelia Earhart born?"}).names
# ['Amelia Earhart']


很好,现在我们可以检测问题中的实体了,让我们使用全文索引将它们映射到知识图。首先,我们需要定义一个全文索引和一个函数,该函数将生成允许拼写错误的全文查询,这里我们不详细介绍。

graph.query(
    "CREATE FULLTEXT INDEX entity IF NOT EXISTS FOR (e:__Entity__) ON EACH [e.id]")

def generate_full_text_query(input: str) -> str:
    """
    Generate a full-text search query for a given input string.

    This function constructs a query string suitable for a full-text
    search. It processes the input string by splitting it into words and 
    appending a similarity threshold (~2 changed characters) to each
    word, then combines them using the AND operator. Useful for mapping
    entities from user questions to database values, and allows for some 
    misspelings.
    """
    full_text_query = ""
    words = [el for el in remove_lucene_chars(input).split() if el]
    for word in words[:-1]:
        full_text_query += f" {word}~2 AND"
    full_text_query += f" {words[-1]}~2"
    return full_text_query.strip()


让我们现在把它们放在一起。

# Fulltext index query
def structured_retriever(question: str) -> str:
    """
    Collects the neighborhood of entities mentioned
    in the question
    """
    result = ""
    entities = entity_chain.invoke({"question": question})
    for entity in entities.names:
        response = graph.query(
            """CALL db.index.fulltext.queryNodes('entity', $query, 
            {limit:2})
            YIELD node,score
            CALL {
              MATCH (node)-[r:!MENTIONS]->(neighbor)
              RETURN node.id + ' - ' + type(r) + ' -> ' + neighbor.id AS 
              output
              UNION
              MATCH (node)<-[r:!MENTIONS]-(neighbor)
              RETURN neighbor.id + ' - ' + type(r) + ' -> ' +  node.id AS 
              output
            }
            RETURN output LIMIT 50
            """,
            {"query": generate_full_text_query(entity)},
        )
        result += "\n".join([el['output'] for el in response])
    return result


structured_retriever函数从检测用户问题中的实体开始。接下来,它对检测到的实体进行迭代,并使用Cypher模板来检索相关节点的邻域。让我们测试一下!

print(structured_retriever("Who is Elizabeth I?"))
# Elizabeth I - BORN_ON -> 7 September 1533
# Elizabeth I - DIED_ON -> 24 March 1603
# Elizabeth I - TITLE_HELD_FROM -> Queen Of England And Ireland
# Elizabeth I - TITLE_HELD_UNTIL -> 17 November 1558
# Elizabeth I - MEMBER_OF -> House Of Tudor
# Elizabeth I - CHILD_OF -> Henry Viii
# and more...


最终检索器


正如我们在开头提到的,我们将结合非结构化和图检索器来创建将传递给LLM的最终上下文。

def retriever(question: str):
    print(f"Search query: {question}")
    structured_data = structured_retriever(question)
    unstructured_data = [el.page_content for el in vector_index.similarity_search(question)]
    final_data = f"""Structured data:
{structured_data}
Unstructured data:
{"#Document ". join(unstructured_data)}
    """
    return final_data


当我们处理Python时,我们可以简单地使用f字符串连接输出。

定义RAG链


我们已经成功地实现了RAG的检索组件。接下来,我们介绍一个提示,它利用集成混合检索器提供的上下文来生成响应,完成了RAG链的实现。

template = """Answer the question based only on the following context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

chain = (
    RunnableParallel(
        {
            "context": _search_query | retriever,
            "question": RunnablePassthrough(),
        }
    )
    | prompt
    | llm
    | StrOutputParser()
)

最后,我们可以继续测试我们的混合RAG实现。

chain.invoke({"question": "Which house did Elizabeth I belong to?"})
# Search query: Which house did Elizabeth I belong to?
# 'Elizabeth I belonged to the House of Tudor.'


我还加入了一个查询重写功能,使RAG链能够适应允许后续问题的对话设置。考虑到我们使用矢量和关键字搜索方法,我们必须重写后续问题,以优化我们的搜索过程。

chain.invoke(
    {
        "question": "When was she born?",
        "chat_history": [("Which house did Elizabeth I belong to?",
        "House Of Tudor")],
    }
)
# Search query: When was Elizabeth I born?
# 'Elizabeth I was born on 7 September 1533.'


你可以观察到她是什么时候出生的?第一次改写为“伊丽莎白一世何时出生?”。然后使用重写后的查询来检索相关上下文并回答问题。

总结


随着LLMGraphTransformer的引入,生成知识图的过程现在应该更顺畅、更容易访问,使任何希望利用知识图提供的深度和上下文来增强其基于RAG的应用程序的人都更容易。这只是一个开始,因为我们有很多改进计划。

如果您对我们使用LLM生成图形有任何见解、建议或问题,请随时联系。

The code is available on GitHub.

文章链接