このページでは、アプリの使用に関する一般的な手順に加えて、LanggraphAgent
に固有の機能について説明します。
始める前に
このチュートリアルは、次の手順を読んで理解していることを前提としています。
- LangGraph アプリケーションを開発する:
LanggraphAgent
のインスタンスとしてapplication
を開発します。 - ユーザー認証: アプリケーションをクエリするユーザーとして認証します。
サポートされているオペレーション
LanggraphAgent
でサポートされているオペレーションは次のとおりです。
query
: クエリへのレスポンスを同期的に取得します。stream_query
: クエリへのレスポンスをストリーミングします。get_state
: 特定のチェックポイントを取得します。get_state_history
: スレッドのチェックポイントを一覧表示します。update_state
: さまざまなシナリオに対応するブランチを作成します。
クエリへのレスポンスをストリーミングする
LangGraph は複数のストリーミング モードをサポートしています。主なものは次のとおりです。
values
: このモードでは、各ノードが呼び出された後にグラフの完全な状態がストリーミングされます。updates
: このモードでは、各ノードが呼び出された後にグラフの状態の更新がストリーミングされます。
values
(グラフの完全な状態に対応)をストリーミングするには:
for state_values in agent.stream_query(
input=inputs,
stream_mode="values",
config={"configurable": {"thread_id": "streaming-thread-values"}},
):
print(state_values)
updates
(グラフの状態の更新に対応)をストリーミングするには:
for state_updates in agent.stream_query(
input=inputs,
stream_mode="updates",
config={"configurable": {"thread_id": "streaming-thread-updates"}},
):
print(state_updates)
人間参加型
LangGraph では、人間がループに介入する一般的な方法として、ブレークポイントを追加してアプリケーションによる一連のアクションを中断し、後で人間がフローを再開できるようにします。
確認
.query
または .stream_query
を呼び出すときに、interrupt_before=
または interrupt_after=
引数を使用してブレークポイントを設定できます。
response = agent.query(
input=inputs,
interrupt_before=["tools"], # after generating the function call, before invoking the function
interrupt_after=["tools"], # after getting a function response, before moving on
config={"configurable": {"thread_id": "human-in-the-loop-deepdive"}},
)
langchain_load(response['messages'][-1]).pretty_print()
出力は次のようになります。
================================== Ai Message ==================================
Tool Calls:
get_exchange_rate (12610c50-4465-4296-b1f3-d751ec959fd5)
Call ID: 12610c50-4465-4296-b1f3-d751ec959fd5
Args:
currency_from: USD
currency_to: SEK
承認
生成されたツール呼び出しを承認して、残りの実行を再開するには、None
を入力に渡し、config
内にスレッドまたはチェックポイントを指定します。
response = agent.query(
input=None, # Continue with the function call
interrupt_before=["tools"], # after generating the function call, before invoking the function
interrupt_after=["tools"], # after getting a function response, before moving on
config={"configurable": {"thread_id": "human-in-the-loop-deepdive"}},
)
langchain_load(response['messages'][-1]).pretty_print()
出力は次のようになります。
================================= Tool Message =================================
Name: get_exchange_rate
{"amount": 1.0, "base": "USD", "date": "2024-11-14", "rates": {"SEK": 11.0159}}
履歴
特定のスレッドのすべてのチェックポイントを一覧表示するには、.get_state_history
メソッドを使用します。
for state_snapshot in agent.get_state_history(
config={"configurable": {"thread_id": "human-in-the-loop-deepdive"}},
):
if state_snapshot["metadata"]["step"] >= 0:
print(f'step {state_snapshot["metadata"]["step"]}: {state_snapshot["config"]}')
state_snapshot["values"]["messages"][-1].pretty_print()
print("\n")
レスポンスは次の出力シーケンスのようになります。
step 3: {'configurable': {'thread_id': 'human-in-the-loop-deepdive', 'checkpoint_ns': '', 'checkpoint_id': '1efa2e95-ded5-67e0-8003-2d34e04507f5'}}
================================== Ai Message ==================================
The exchange rate from US dollars to Swedish krona is 1 USD to 11.0159 SEK.
step 2: {'configurable': {'thread_id': 'human-in-the-loop-deepdive', 'checkpoint_ns': '', 'checkpoint_id': '1efa2e95-d189-6a77-8002-5dbe79e2ce58'}}
================================= Tool Message =================================
Name: get_exchange_rate
{"amount": 1.0, "base": "USD", "date": "2024-11-14", "rates": {"SEK": 11.0159}}
step 1: {'configurable': {'thread_id': 'human-in-the-loop-deepdive', 'checkpoint_ns': '', 'checkpoint_id': '1efa2e95-cc7f-6d68-8001-1f6b5e57c456'}}
================================== Ai Message ==================================
Tool Calls:
get_exchange_rate (12610c50-4465-4296-b1f3-d751ec959fd5)
Call ID: 12610c50-4465-4296-b1f3-d751ec959fd5
Args:
currency_from: USD
currency_to: SEK
step 0: {'configurable': {'thread_id': 'human-in-the-loop-deepdive', 'checkpoint_ns': '', 'checkpoint_id': '1efa2e95-c2e4-6f3c-8000-477fd654cb53'}}
================================ Human Message =================================
What is the exchange rate from US dollars to Swedish currency?
ステップの構成を取得する
前のチェックポイントを取得するには、checkpoint_id
(と checkpoint_ns
)を指定します。まず、ツール呼び出しが生成されたステップ 1 まで巻き戻します。
snapshot_config = {}
for state_snapshot in agent.get_state_history(
config={"configurable": {"thread_id": "human-in-the-loop-deepdive"}},
):
if state_snapshot["metadata"]["step"] == 1:
snapshot_config = state_snapshot["config"]
break
print(snapshot_config)
出力は次のようになります。
{'configurable': {'thread_id': 'human-in-the-loop-deepdive',
'checkpoint_ns': '',
'checkpoint_id': '1efa2e95-cc7f-6d68-8001-1f6b5e57c456'}}
タイムトラベル
チェックポイントを取得するには、メソッド .get_state
を使用します。
# By default, it gets the latest state [unless (checkpoint_ns, checkpoint_id) is specified]
state = agent.get_state(config={"configurable": {
"thread_id": "human-in-the-loop-deepdive",
}})
print(f'step {state["metadata"]["step"]}: {state["config"]}')
state["values"]["messages"][-1].pretty_print()
デフォルトでは、最新のチェックポイント(タイムスタンプ順)が取得されます。出力は次のようになります。
step 3: {'configurable': {'thread_id': 'human-in-the-loop-deepdive', 'checkpoint_ns': '', 'checkpoint_id': '1efa2e95-ded5-67e0-8003-2d34e04507f5'}}
================================== Ai Message ==================================
The exchange rate from US dollars to Swedish krona is 1 USD to 11.0159 SEK.
構成のチェックポイントを取得する
特定の構成(ステップの構成の snapshot_config
など)に対して、対応するチェックポイントを取得できます。
state = agent.get_state(config=snapshot_config)
print(f'step {state["metadata"]["step"]}: {state["config"]}')
state["values"]["messages"][-1].pretty_print()
出力は次のようになります。
step 1: {'configurable': {'thread_id': 'human-in-the-loop-deepdive', 'checkpoint_ns': '', 'checkpoint_id': '1efa2e95-cc7f-6d68-8001-1f6b5e57c456'}}
================================== Ai Message ==================================
Tool Calls:
get_exchange_rate (12610c50-4465-4296-b1f3-d751ec959fd5)
Call ID: 12610c50-4465-4296-b1f3-d751ec959fd5
Args:
currency_from: USD
currency_to: SEK
リプレイ
特定の状態から再現するには、状態構成(state["config"]
)をアプリケーションに渡します。状態の構成は、次のような辞書です。
{'configurable': {'thread_id': 'human-in-the-loop-deepdive',
'checkpoint_ns': '',
'checkpoint_id': '1efa2e95-cc7f-6d68-8001-1f6b5e57c456'}}
state["config"]
(ツール呼び出しが生成された場所)から再生するには、入力に None
を指定します。
for state_values in agent.stream_query(
input=None, # resume
stream_mode="values",
config=state["config"],
):
langchain_load(state_values["messages"][-1]).pretty_print()
次のような出力結果が得られます。
================================== Ai Message ==================================
Tool Calls:
get_exchange_rate (12610c50-4465-4296-b1f3-d751ec959fd5)
Call ID: 12610c50-4465-4296-b1f3-d751ec959fd5
Args:
currency_from: USD
currency_to: SEK
================================= Tool Message =================================
Name: get_exchange_rate
{"amount": 1.0, "base": "USD", "date": "2024-11-14", "rates": {"SEK": 11.0159}}
================================== Ai Message ==================================
The exchange rate from US dollars to Swedish krona is 1 USD to 11.0159 SEK.
分岐
.update_state
メソッドを使用して、以前のチェックポイントから分岐して、別のシナリオを試すことができます。
branch_config = agent.update_state(
config=state["config"],
values={"messages": [last_message]}, # the update we want to make
)
print(branch_config)
出力は次のようになります。
{'configurable': {'thread_id': 'human-in-the-loop-deepdive',
'checkpoint_ns': '',
'checkpoint_id': '1efa2e96-0560-62ce-8002-d1bb48a337bc'}}
branch_config
を使用してアプリケーションにクエリを実行し、更新された状態のチェックポイントから再開できます。
for state_values in agent.stream_query(
input=None, # resume
stream_mode="values",
config=branch_config,
):
langchain_load(state_values["messages"][-1]).pretty_print()
次のような出力結果が得られます。
================================== Ai Message ==================================
Tool Calls:
get_exchange_rate (12610c50-4465-4296-b1f3-d751ec959fd5)
Call ID: 12610c50-4465-4296-b1f3-d751ec959fd5
Args:
currency_date: 2024-09-01
currency_from: USD
currency_to: SEK
================================= Tool Message =================================
Name: get_exchange_rate
{"amount": 1.0, "base": "USD", "date": "2024-08-30", "rates": {"SEK": 10.2241}}
================================== Ai Message ==================================
The exchange rate from US dollars to Swedish krona on 2024-08-30 was 1 USD to 10.2241 SEK.