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?

LangGraphの使い方(超基本編)

Posted at

LangGraphとは

エージェントワークフローを作成するために使用されるLLMアプリ構築するためのライブラリです。

AIエージェントが今後の流行になるとのことでしたので、LangGraphの基本的な使用方法を試してみました。

インストール

pip install -U langgraph

LangGraphの概念

Graphは基本的に「Node」と「Edge」という要素で構成されている。

  • Node
    • 処理部分
  • Edge
    • Node同士の接続部分

Stateというアプリのスナップショットを表す共有データ構造もある。

NodeとEdgeを組み合わせることで、時間経過と共にStateが変化するワークフローを構成することができる。

シンプルなグラフ

Stateの定義

TypedDictStateクラスを定義する。

from typing_extensions import TypedDict

class State(TypedDict):
    value: str

Graphの作成

StateGraphに定義したStateクラスを引数にインスタンスを作成する。

from langgraph.graph import StateGraph

graph = StateGraph(State)

Nodeの作成

nodeStateを受け取って、更新したい情報を返す関数として定義する。
今回はNodeを2つ作成し、受け取った文字列に_node1_node2を付け加える処理にしている。

def node1(state: State):
    value = state["value"] + "_node1"
    return {"value": value}

def node2(state: State):
    value = state["value"] + "_node2"
    return {"value": value}

GraphにNodeの追加

graphadd_nodeメソッドを使用して、NodeをGraphに追加する。

graph.add_node("node1", node1)
graph.add_node("node2", node2)

Edgeの追加

node1node2add_edgeでEdgeとして接続する。

graph.add_edge("node1", "node2")

始点と終点を定義

set_entry_pointで始点を、set_finish_pointで終点を定義する。

graph.set_entry_point("node")

graph.set_finish_point("node2")

Graphのコンパイル

NodeとEdgeで構成したGraphをコンパイルする。

graph = graph.compile()

コンパイルしたGraphからmermaidで可視化したグラフを出力することができる。

from IPython.display import Image, display

display(Image(graph.get_graph().draw_mermaid_png()))

image.png

mermaid出力方法
print(graph.get_graph().draw_mermaid())

実行

invokeStateの初期値を与える。

graph.invoke({"value": "hoge"})

出力

{'value': 'hoge_node1_node2'}

初期値のhogeの入力に対して、node1で実行されてhoge_node1になり、node2で実行されてhoge_node1_node2になっていて、期待した動作になっている。

デバッグ

invokeの引数で、debugTrueにすることで実行時の情報を可視化できる。

graph.invoke({"value": "hoge"}, debug=True)

出力

[-1:checkpoint] State at the end of step -1:
{}
[0:tasks] Starting 1 task for step 0:
- __start__ -> {'value': 'hoge'}
[0:writes] Finished step 0 with writes to 1 channel:
- value -> 'hoge'
[0:checkpoint] State at the end of step 0:
{'value': 'hoge'}
[1:tasks] Starting 1 task for step 1:
- node1 -> {'value': 'hoge'}
[1:writes] Finished step 1 with writes to 1 channel:
- value -> 'hoge_node1'
[1:checkpoint] State at the end of step 1:
{'value': 'hoge_node1'}
[2:tasks] Starting 1 task for step 2:
- node2 -> {'value': 'hoge_node1'}
[2:writes] Finished step 2 with writes to 1 channel:
- value -> 'hoge_node1_node2'
[2:checkpoint] State at the end of step 2:
{'value': 'hoge_node1_node2'}

Graphの流れを追うことができる。

まとめ

LangGraphの超基本的な使用方法について試してみました。
AIエージェントは、さらに複雑なGraph構造になると思われるので、とりあえず基本的な使用方法を知れてよかったと思います。
今後は条件分岐やLLMを使用した本格的なAIエージェントの作成を試してみたいと思います。

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?