0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lang graph 基礎ガイド

Posted at

② LangGraph 基礎ガイド

―― グラフベース AI ワークフロー構築の第一歩

LangGraph 基礎ガイド

グラフベース AI ワークフロー構築の第一歩
Published 2025-05-07

TL;DR

  • LangGraph は LangChain 上に載る 状態遷移グラフ ライブラリ。分岐・ループ・チェックポイントを“当たり前”にする。:contentReference[oaicite:5]{index=5}
  • ノード=処理、エッジ=条件付き遷移なので、複雑なマルチエージェントアプリでも破綻しにくい。:contentReference[oaicite:6]{index=6}

1. Chain から Graph へ:モデルの違い

Model 特徴 向き不向き
Chain (LangChain) 直列処理・例外時はスコープ外 1質問1回答, 単純 RAG
Graph (LangGraph) DAG / ループ / マージ可 要件ヒアリング → 分析 → 再質問のような対話型業務

2. アーキテクチャ超速レビュー

[Start]─┬─> Node A ─> Node B ─> End
        └─> Node C ---┘
  • ノードRunnable or Python 関数
  • エッジ:True/False/スコア分岐
  • チェックポイント:各ノード後の JSON 状態を自動永続化(S3 / Redis など)([LangChain AI][2])

3. 5 分クイックスタート

# pip install langchain langgraph openai
from langgraph.graph import StateGraph
from langgraph.schema import State

class MyState(State):
    query: str
    answer: str | None = None

def retriever(state: MyState):
    # ベクトル検索など
    return state.model_copy(update={"answer": "Dummy answer"})

def summarizer(state: MyState):
    # LLM で要約
    return state

g = StateGraph(MyState)
g.add_node("retrieve", retriever)
g.add_node("summarize", summarizer)
g.set_entry_point("retrieve").connect("retrieve", "summarize")
app = g.compile()
print(app.invoke(MyState(query="会社のペインは?")))

📝 実務 Tip

  • State を Pydantic v2 で定義すると自動スキーマ生成
  • ループ処理は graph.add_conditional_edge で実装可([Zep - AI Agent Memory][3])

4. Enterprise Best Practice

  1. 小さくモジュール化 : “聞く ➜ まとめる ➜ 提案する” を 3 ノードに
  2. 永続化バックエンド : まずは SQLite → 本番は DynamoDB など
  3. Observability : LangSmith / OpenTelemetry 併用でトレース可視化

5. まとめ

LangGraph を使うと「業務フローを絵に描いて、そのまま動かす」が現実になる。
Semantic Kernel の Planner と組み合わせれば、“LLM が計画 → Graph が実行” の二段ロケットも可能だ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?