Skip to main content
Open In Colab在 GitHub 上打开

Amazon Neptune 与 Cypher

Amazon Neptune 是一种高性能图形分析和无服务器数据库,可实现卓越的可扩展性和可用性。

此示例显示了查询NeptuneGraph 数据库使用openCypher并返回人类可读的响应。

Cypher 是一种声明性图形查询语言,允许在属性图中进行富有表现力和高效的数据查询。

OpenPher 是 Cypher 的开源实现。# Neptune Open Cypher QA 链 此 QA 链使用 OpencyPher 查询 Amazon Neptune 并返回人类可读的响应

LangChain 支持 Neptune 数据库Neptune 分析,其中create_neptune_opencypher_qa_chain.

Neptune 数据库是一种无服务器图形数据库,旨在实现最佳可扩展性和可用性。它为需要扩展到每秒 100000 个查询的图形数据库工作负载、多可用区高可用性和多区域部署提供解决方案。您可以将 Neptune Database 用于社交网络、欺诈警报和 Customer 360 应用程序。

Neptune Analytics 是一种分析数据库引擎,可以快速分析内存中的大量图形数据,以获得见解并发现趋势。Neptune Analytics 是一种用于快速分析存储在数据湖中的现有图形数据库或图形数据集的解决方案。它使用流行的图形分析算法和低延迟分析查询。

使用 Neptune 数据库

from langchain_aws.graphs import NeptuneGraph

host = "<neptune-host>"
port = 8182
use_https = True

graph = NeptuneGraph(host=host, port=port, use_https=use_https)
API 参考:NeptuneGraph

使用 Neptune Analytics

from langchain_aws.graphs import NeptuneAnalyticsGraph

graph = NeptuneAnalyticsGraph(graph_identifier="<neptune-analytics-graph-id>")

使用 Neptune OpencyPher QA 链

此 QA 链使用 OpencyPher 查询 Neptune 图形数据库并返回人类可读的响应。

from langchain_aws import ChatBedrockConverse
from langchain_aws.chains import create_neptune_opencypher_qa_chain

MODEL_ID = "anthropic.claude-3-5-sonnet-20241022-v2:0"
llm = ChatBedrockConverse(
model=MODEL_ID,
temperature=0,
)

chain = create_neptune_opencypher_qa_chain(llm=llm, graph=graph)

result = chain.invoke("How many outgoing routes does the Austin airport have?")
print(result["result"].content)
Austin airport has 98 outgoing routes.

添加消息历史记录

新的 Neptune OpencyPher QA 链能够被RunnableWithMessageHistory.这会将消息历史记录添加到链中,从而允许我们创建一个聊天机器人,该机器人在多个调用中保留对话状态。

首先,我们需要一种方法来存储和加载消息历史记录。为此,每个线程都将创建为InMemoryChatMessageHistory,并存储到字典中以供重复访问。

(另请参阅:https://python.langchain.com/docs/versions/migrating_memory/chat_history/#chatmessagehistory)

from langchain_core.chat_history import InMemoryChatMessageHistory

chats_by_session_id = {}


def get_chat_history(session_id: str) -> InMemoryChatMessageHistory:
chat_history = chats_by_session_id.get(session_id)
if chat_history is None:
chat_history = InMemoryChatMessageHistory()
chats_by_session_id[session_id] = chat_history
return chat_history

现在,QA 链和消息历史记录存储可用于创建新的RunnableWithMessageHistory.请注意,我们必须设置query作为 input 键来匹配基础链所需的格式。

from langchain_core.runnables.history import RunnableWithMessageHistory

runnable_with_history = RunnableWithMessageHistory(
chain,
get_chat_history,
input_messages_key="query",
)

在调用链之前,唯一的session_id需要为新的InMemoryChatMessageHistory会记住的。

import uuid

session_id = uuid.uuid4()

最后,使用session_id.

result = runnable_with_history.invoke(
{"query": "How many destinations can I fly to directly from Austin airport?"},
config={"configurable": {"session_id": session_id}},
)
print(result["result"].content)
You can fly directly to 98 destinations from Austin airport.

当链继续使用相同的session_id,响应将在对话中先前查询的上下文中返回。

result = runnable_with_history.invoke(
{"query": "Out of those destinations, how many are in Europe?"},
config={"configurable": {"session_id": session_id}},
)
print(result["result"].content)
You can fly directly to 4 destinations in Europe from Austin airport.
result = runnable_with_history.invoke(
{"query": "Give me the codes and names of those airports."},
config={"configurable": {"session_id": session_id}},
)
print(result["result"].content)
The four European destinations you can fly to directly from Austin airport are:
- AMS (Amsterdam Airport Schiphol)
- FRA (Frankfurt am Main)
- LGW (London Gatwick)
- LHR (London Heathrow)