Skip to main content
Open In ColabOpen on GitHub

如何在图数据库上添加语义层

您可以使用数据库查询从像Neo4j这样的图数据库中检索信息。 一种选择是使用LLMs生成Cypher语句。 虽然这种选择提供了极佳的灵活性,但解决方案可能脆弱且不能始终生成精确的Cypher语句。 与其生成Cypher语句,我们可以在语义层中实现Cypher模板作为工具,LLM代理可以与之交互。

graph_semantic.png

设置

首先,获取所需的包并设置环境变量:

%pip install --upgrade --quiet  langchain langchain-neo4j langchain-openai

在本指南中,我们默认使用OpenAI模型,但您可以将它们替换为您选择的模型提供商。

import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()

# Uncomment the below to use LangSmith. Not required.
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
 ········

接下来,我们需要定义Neo4j的凭证。 按照这些安装步骤来设置Neo4j数据库。

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

下面的示例将创建一个与Neo4j数据库的连接,并用有关电影及其演员的示例数据填充它。

from langchain_neo4j import Neo4jGraph

graph = Neo4jGraph(refresh_schema=False)

# Import movie information

movies_query = """
LOAD CSV WITH HEADERS FROM
'https://raw.githubusercontent.com/tomasonjo/blog-datasets/main/movies/movies_small.csv'
AS row
MERGE (m:Movie {id:row.movieId})
SET m.released = date(row.released),
m.title = row.title,
m.imdbRating = toFloat(row.imdbRating)
FOREACH (director in split(row.director, '|') |
MERGE (p:Person {name:trim(director)})
MERGE (p)-[:DIRECTED]->(m))
FOREACH (actor in split(row.actors, '|') |
MERGE (p:Person {name:trim(actor)})
MERGE (p)-[:ACTED_IN]->(m))
FOREACH (genre in split(row.genres, '|') |
MERGE (g:Genre {name:trim(genre)})
MERGE (m)-[:IN_GENRE]->(g))
"""

graph.query(movies_query)
API Reference:Neo4jGraph
[]

使用Cypher模板的自定义工具

语义层由各种工具组成,这些工具暴露给LLM,使其能够与知识图谱进行交互。它们的复杂性各不相同。你可以将语义层中的每个工具视为一个函数。

我们将实现的函数是检索电影或其演员的信息。

description_query = """
MATCH (m:Movie|Person)
WHERE m.title CONTAINS $candidate OR m.name CONTAINS $candidate
MATCH (m)-[r:ACTED_IN|IN_GENRE]-(t)
WITH m, type(r) as type, collect(coalesce(t.name, t.title)) as names
WITH m, type+": "+reduce(s="", n IN names | s + n + ", ") as types
WITH m, collect(types) as contexts
WITH m, "type:" + labels(m)[0] + "\ntitle: "+ coalesce(m.title, m.name)
+ "\nyear: "+coalesce(m.released,"") +"\n" +
reduce(s="", c in contexts | s + substring(c, 0, size(c)-2) +"\n") as context
RETURN context LIMIT 1
"""


def get_information(entity: str) -> str:
try:
data = graph.query(description_query, params={"candidate": entity})
return data[0]["context"]
except IndexError:
return "No information was found"

你可以观察到我们已经定义了用于检索信息的Cypher语句。 因此,我们可以避免生成Cypher语句,并使用LLM代理仅填充输入参数。 为了向LLM代理提供有关何时使用工具及其输入参数的额外信息,我们将该函数包装为一个工具。

from typing import Optional, Type

from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field


class InformationInput(BaseModel):
entity: str = Field(description="movie or a person mentioned in the question")


class InformationTool(BaseTool):
name: str = "Information"
description: str = (
"useful for when you need to answer questions about various actors or movies"
)
args_schema: Type[BaseModel] = InformationInput

def _run(
self,
entity: str,
) -> str:
"""Use the tool."""
return get_information(entity)

async def _arun(
self,
entity: str,
) -> str:
"""Use the tool asynchronously."""
return get_information(entity)
API Reference:BaseTool

LangGraph 代理

我们将使用LangGraph实现一个简单的ReAct代理。

代理由LLM和工具步骤组成。当我们与代理交互时,我们首先会调用LLM来决定是否应该使用工具。然后我们将运行一个循环:

如果代理说要采取行动(即调用工具),我们将运行工具并将结果传回给代理。 如果代理没有要求运行工具,我们将结束(向用户响应)。

代码实现非常简单。首先我们将工具绑定到LLM并定义助手步骤。

from langchain_core.messages import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import MessagesState

llm = ChatOpenAI(model="gpt-4o")

tools = [InformationTool()]
llm_with_tools = llm.bind_tools(tools)

# System message
sys_msg = SystemMessage(
content="You are a helpful assistant tasked with finding and explaining relevant information about movies."
)


# Node
def assistant(state: MessagesState):
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}

接下来我们定义LangGraph流程。

from IPython.display import Image, display
from langgraph.graph import END, START, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition

# Graph
builder = StateGraph(MessagesState)

# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))

# Define edges: these determine how the control flow moves
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message (result) from assistant is a tool call -> tools_condition routes to tools
# If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END
tools_condition,
)
builder.add_edge("tools", "assistant")
react_graph = builder.compile()

# Show
display(Image(react_graph.get_graph(xray=True).draw_mermaid_png()))

让我们用一个示例问题来测试工作流程。

input_messages = [HumanMessage(content="Who played in the Casino?")]
messages = react_graph.invoke({"messages": input_messages})
for m in messages["messages"]:
m.pretty_print()
================================ Human Message =================================

Who played in the Casino?
================================== Ai Message ==================================
Tool Calls:
Information (call_j4usgFStGtBM16fuguRaeoGc)
Call ID: call_j4usgFStGtBM16fuguRaeoGc
Args:
entity: Casino
================================= Tool Message =================================
Name: Information

type:Movie
title: Casino
year: 1995-11-22
ACTED_IN: Robert De Niro, Joe Pesci, Sharon Stone, James Woods
IN_GENRE: Drama, Crime

================================== Ai Message ==================================

The movie "Casino," released in 1995, features the following actors:

- Robert De Niro
- Joe Pesci
- Sharon Stone
- James Woods

The film is in the Drama and Crime genres.

这个页面有帮助吗?