跳到主要內容
Open In ColabOpen on GitHub

Amazon Neptune with Cypher

Amazon Neptune 是一個高效能圖形分析和無伺服器資料庫,具有卓越的可擴展性和可用性。

此範例展示了使用 openCypher 查詢 Neptune 圖形資料庫並傳回人類可讀回應的 QA 鏈。

Cypher 是一種宣告式圖形查詢語言,可在屬性圖形中進行表達力強且有效率的資料查詢。

openCypher 是 Cypher 的開放原始碼實作。# Neptune Open Cypher QA 鏈 這個 QA 鏈使用 openCypher 查詢 Amazon Neptune,並傳回人類可讀的回應

LangChain 透過 create_neptune_opencypher_qa_chain 支援 Neptune 資料庫Neptune Analytics

Neptune 資料庫是一個無伺服器圖形資料庫,專為最佳的可擴展性和可用性而設計。它為需要擴展到每秒 100,000 個查詢、多可用區高可用性和多區域部署的圖形資料庫工作負載提供了解決方案。您可以將 Neptune 資料庫用於社群網路、詐欺警報和客戶 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://langchain-python.dev.org.tw/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 設定為輸入鍵,以符合基礎鏈預期的格式。

from langchain_core.runnables.history import RunnableWithMessageHistory

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

在調用鏈之前,需要為新的 InMemoryChatMessageHistory 將記住的對話產生唯一的 session_id

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)

此頁面是否對您有幫助?