在對話中使用圖形的記憶體
現在你的聊天機器人已能使用記憶體,你可以將它對追問的回應以串流方式輸出。請注意,你的追問不需要額外脈絡,因為機器人可以從記憶體中存放的完整對話取得所需資訊。題目已經幫你設定好問題內容。
本練習屬於課程
Designing Agentic Systems with LangChain
練習說明
- 建立一個
config字典,內含"configurable",並將其中的"thread_id"設為"single_session_memory"。 - 迭代每個圖形
event,對graph呼叫.stream()方法,傳入一個字典:"messages"中放入標記為"user"的user_input,同時傳入config字典。 - 以
value迭代event.values(),若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?")