チャットボットの応答を生成する

LangChainで設計するエージェント型システム

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

グラフイベントのストリーミング

   

  • イベントをリアルタイム配信

  • 各イベントはワークフローの一歩

  • 応答とツール呼び出しを追跡

  • チャットボットの進行を追跡

User: Where is the Congo?

Agent: 詳細を確認します...
Agent: AIMessage([ツール呼び出し: Google Maps] 「the Congo」を検索中)
Agent: コンゴ民主共和国は 中部アフリカの国です。
LangChainで設計するエージェント型システム

LLM 応答のストリーミング

# ユーザー入力に基づきチャットボットを実行する関数を定義
def stream_graph_updates(user_input: str):


# ユーザー入力でグラフのイベントをストリーム開始 for event in graph.stream({"messages": [("user", user_input)]}):
# チャットボットのノード応答を取得して出力 for value in event.values(): print("Agent:", value["messages"])
# ユーザーの質問を定義して実行 user_query = "Who is Mary Shelley?" stream_graph_updates(user_query)
LangChainで設計するエージェント型システム

LLM 応答のストリーミング

   Agent: [AIMessage(content='Mary Shelley (1797-1851) was an English 
   novelist...

...best known for her groundbreaking work in the Gothic genre, particularly for her novel "Frankenstein; or, The Modern Prometheus," published in 1818. This novel is often considered one of the earliest examples of science fiction and explores themes of creation, responsibility, and the nature of humanity through the story of Victor Frankenstein, a scientist who creates a sentient creature in an unorthodox experiment...',
response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4o-mini-...')]
LangChainで設計するエージェント型システム

LLM とハルシネーション

Agent: [AIMessage(content='Judith 
Love Cohen was an American aerospace 
engineer, and worked on various 
space missions, including the Apollo 
program... Cohen was also the mother 
of actor and writer Adam Cohen, 
who has spoken about her influence on 
his life and career. 
additional_kwargs={}, 
response_metadata={'finish_reason': 
'stop', 'model_name': 
'gpt-4o-mini-...)

   

ハルシネーションの例

 

  • Judith Love Cohen の有名な息子は「Adam Cohen」ではなく Jack Black です。
LangChainで設計するエージェント型システム

LangGraph 図を生成する

# チャットボット図のためのモジュールをインポート
from IPython.display import 
Image, display


# グラフ図の生成と表示を試す try: display(Image(graph.get_graph() .draw_mermaid_png()))
# 必要に応じて例外を返す  
except Exception:
    print("追加の依存関係が必要です。")

基本的なチャットボットのLangGraph図。

LangChainで設計するエージェント型システム

Passons à la pratique !

LangChainで設計するエージェント型システム

Preparing Video For Download...