問い合わせと応答
エージェントグラフの準備が整い、チャットボットをすぐに実行できます。次は、ChatGPT を使ってクエリに答えられる関数を作成しましょう。この関数はグラフのイベントをリアルタイムでストリーミングし、ユーザーのクエリに対する応答として最後のメッセージを返します。
この演習はコースの一部です
LangChainで設計するエージェント型システム
演習の手順
- チャットボットを実行できるように、文字列パラメータ
user_inputを受け取るstream_graph_updates()関数を定義します。 graphに対して.stream()メソッドを適用し、"messages"内の"user"メッセージとしてuser_inputを渡してイベントをストリーミングします。- 各
event.values()のitemについて、itemの"messages"キーを使って応答を取得し、出力します。 - チャットボットをテストするために、
stream_graph_updates()関数へuser_queryを渡します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define a function to execute the chatbot based on user input
def stream_graph_updates(____: ____):
# Start streaming events from the graph with the user's input
for event in graph.____({"____": [("____", ____)]}):
# Retrieve and print the chatbot node responses
for ____ in event.values():
print("Agent:", ____["____"])
# Define the user query and run the chatbot
____ = "Who is Ada Lovelace?"
stream_graph_updates(____)