始める無料で始める

問い合わせと応答

エージェントグラフの準備が整い、チャットボットをすぐに実行できます。次は、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(____)
コードを編集して実行