会話にグラフのメモリを使う
チャットボットにメモリを持たせたので、後続の質問に対する応答をストリームできます。チャットボットはメモリに保存された会話全体にアクセスできるため、後続の質問に追加のコンテキストは不要です。質問はすでに用意されています。
この演習はコースの一部です
LangChainで設計するエージェント型システム
演習の手順
"configurable"を含み、"thread_id"を"single_session_memory"に設定したconfig辞書を作成します。- 各グラフの
eventをループし、graphに対して.stream()メソッドを適用します。引数には、"messages"に"user"ラベル付きのuser_inputを含めた辞書と、config辞書を渡します。 event.values()をvalueでループし、valueに"messages"が存在して空でなければ、"Agent:"に続けて"messages"を出力します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Set up a streaming function for a single user
def stream_memory_responses(user_input: str):
____ = {"____": {"____": "____"}}
# Stream the events in the graph
for ____ in graph.____({"messages": [("user", ____)]}, ____):
# Return the agent's last response
for ____ in event.____():
if "messages" in ____ and value["____"]:
print("Agent:", value["messages"])
stream_memory_responses("Tell me about the Eiffel Tower.")
stream_memory_responses("Who built it?")