Functional API 사용하기
Functional API 사용하기 (Use the functional API)
Functional API는 LangGraph의 핵심 기능들(영속성, 메모리, human-in-the-loop, 스트리밍)을 기존 코드에 최소한의 변경만으로 추가할 수 있게 해줘요.
Functional API의 개념에 대한 자세한 내용은 Functional API를 참고해요.
출처: 문서
본문
간단한 워크플로 만들기 (Creating a simple workflow)
entrypoint를 정의할 때 입력은 함수의 첫 번째 인자로 제한돼요. 여러 입력을 넘기려면 딕셔너리를 사용할 수 있어요.
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
value = inputs["value"]
another_value = inputs["another_value"]
...
my_workflow.invoke({"value": 1, "another_value": 2})
확장 예시: 간단한 워크플로
from langchain_core.utils.uuid import uuid7
from langgraph.func import entrypoint, task
from langgraph.checkpoint.memory import InMemorySaver
# Task that checks if a number is even
@task
def is_even(number: int) -> bool:
return number % 2 == 0
# Task that formats a message
@task
def format_message(is_even: bool) -> str:
return "The number is even." if is_even else "The number is odd."
# Create a checkpointer for persistence
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def workflow(inputs: dict) -> str:
"""Simple workflow to classify a number."""
even = is_even(inputs["number"]).result()
return format_message(even).result()
# Run the workflow with a unique thread ID
config = {"configurable": {"thread_id": str(uuid7())}}
result = workflow.invoke({"number": 7}, config=config)
print(result)
확장 예시: LLM으로 에세이 작성하기
이 예시는 문법적으로 @task와 @entrypoint 데코레이터를 어떻게 쓰는지 보여줘요. checkpointer가 제공되므로 워크플로 결과는 checkpointer에 영속화돼요.
import uuid
from langchain.chat_models import init_chat_model
from langgraph.func import entrypoint, task
from langgraph.checkpoint.memory import InMemorySaver
model = init_chat_model('gpt-3.5-turbo')
# Task: generate essay using an LLM
@task
def compose_essay(topic: str) -> str:
"""Generate an essay about the given topic."""
return model.invoke([
{"role": "system", "content": "You are a helpful assistant that writes essays."},
{"role": "user", "content": f"Write an essay about {topic}."}
]).content
# Create a checkpointer for persistence
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def workflow(topic: str) -> str:
"""Simple workflow that generates an essay with an LLM."""
return compose_essay(topic).result()
# Execute the workflow
config = {"configurable": {"thread_id": str(uuid7())}}
result = workflow.invoke("the history of flight", config=config)
print(result)
병렬 실행 (Parallel execution)
태스크를 동시에 호출하고 결과를 기다리면 병렬로 실행할 수 있어요. IO 바운드 태스크(예: LLM API 호출)에서 성능을 향상시키는 데 유용해요.
@task
def add_one(number: int) -> int:
return number + 1
@entrypoint(checkpointer=checkpointer)
def graph(numbers: list[int]) -> list[str]:
futures = [add_one(i) for i in numbers]
return [f.result() for f in futures]
확장 예시: 병렬 LLM 호출
이 예시는 @task로 여러 LLM 호출을 병렬로 실행하는 방법을 보여줘요. 각 호출이 다른 주제에 대한 문단을 생성하고, 결과를 단일 텍스트 출력으로 결합해요.
import uuid
from langchain.chat_models import init_chat_model
from langgraph.func import entrypoint, task
from langgraph.checkpoint.memory import InMemorySaver
# Initialize the LLM model
model = init_chat_model("gpt-3.5-turbo")
# Task that generates a paragraph about a given topic
@task
def generate_paragraph(topic: str) -> str:
response = model.invoke([
{"role": "system", "content": "You are a helpful assistant that writes educational paragraphs."},
{"role": "user", "content": f"Write a paragraph about {topic}."}
])
return response.content
# Create a checkpointer for persistence
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def workflow(topics: list[str]) -> str:
"""Generates multiple paragraphs in parallel and combines them."""
futures = [generate_paragraph(topic) for topic in topics]
paragraphs = [f.result() for f in futures]
return "\n\n".join(paragraphs)
# Run the workflow
config = {"configurable": {"thread_id": str(uuid7())}}
result = workflow.invoke(["quantum computing", "climate change", "history of aviation"], config=config)
print(result)
이 예시는 LangGraph의 동시성 모델을 사용해 실행 시간을 개선해요. 특히 태스크가 LLM 완성처럼 I/O를 수반할 때요.
그래프 호출하기 (Calling graphs)
Functional API와 Graph API는 같은 기본 런타임을 공유하므로 같은 애플리케이션에서 함께 쓸 수 있어요.
from langgraph.func import entrypoint
from langgraph.graph import StateGraph
builder = StateGraph()
...
some_graph = builder.compile()
@entrypoint()
def some_workflow(some_input: dict) -> int:
# Call a graph defined using the graph API
result_1 = some_graph.invoke(...)
# Call another graph defined using the graph API
result_2 = another_graph.invoke(...)
return {
"result_1": result_1,
"result_2": result_2
}
확장 예시: Functional API에서 간단한 그래프 호출
import uuid
from typing import TypedDict
from langgraph.func import entrypoint
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph
# Define the shared state type
class State(TypedDict):
foo: int
# Define a simple transformation node
def double(state: State) -> State:
return {"foo": state["foo"] * 2}
# Build the graph using the Graph API
builder = StateGraph(State)
builder.add_node("double", double)
builder.set_entry_point("double")
graph = builder.compile()
# Define the functional API workflow
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def workflow(x: int) -> dict:
result = graph.invoke({"foo": x})
return {"bar": result["foo"]}
# Execute the workflow
config = {"configurable": {"thread_id": str(uuid7())}}
print(workflow.invoke(5, config=config)) # Output: {'bar': 10}
다른 entrypoint 호출하기 (Call other entrypoints)
entrypoint나 task 안에서 다른 entrypoint를 호출할 수 있어요.
@entrypoint() # Will automatically use the checkpointer from the parent entrypoint
def some_other_workflow(inputs: dict) -> int:
return inputs["value"]
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
value = some_other_workflow.invoke({"value": 1})
return value
확장 예시: 다른 entrypoint 호출
import uuid
from langgraph.func import entrypoint
from langgraph.checkpoint.memory import InMemorySaver
# Initialize a checkpointer
checkpointer = InMemorySaver()
# A reusable sub-workflow that multiplies a number
@entrypoint()
def multiply(inputs: dict) -> int:
return inputs["a"] * inputs["b"]
# Main workflow that invokes the sub-workflow
@entrypoint(checkpointer=checkpointer)
def main(inputs: dict) -> dict:
result = multiply.invoke({"a": inputs["x"], "b": inputs["y"]})
return {"product": result}
# Execute the main workflow
config = {"configurable": {"thread_id": str(uuid7())}}
print(main.invoke({"x": 6, "y": 7}, config=config)) # Output: {'product': 42}
스트리밍 (Streaming)
Functional API는 Graph API와 같은 스트리밍 메커니즘을 사용해요. 자세한 내용은 streaming 가이드를 읽어보세요.
스트리밍 API로 워크플로 실행에서 값 청크를 스트리밍하는 예시예요.
config = {"configurable": {"thread_id": str(uuid7())}}
stream = main.stream_events({"x": 5}, config=config, version="v3")
for mode, chunk in stream.interleave("values"):
print(f"{mode}: {chunk}")
# values: 10
Example trace 보기: 이 예시의 공개 LangSmith run을 열어보세요.
langgraph.config에서get_stream_writer를 가져와요.- entrypoint 안에서 stream writer 인스턴스를 얻어요.
- 계산이 시작되기 전에 커스텀 데이터를 내보내요.
- 결과를 계산한 뒤 또 다른 커스텀 메시지를 내보내요.
stream_events()로 스트리밍된 출력을 처리해요.interleave("values")의(mode, chunk)쌍을 순회해요.
Python < 3.11에서의 비동기 Python < 3.11을 쓰면서 비동기 코드를 작성한다면
get_stream_writer가 동작하지 않아요. 대신StreamWriter클래스를 직접 사용해요. 자세한 내용은 Python < 3.11에서의 비동기를 참고해요.from langgraph.types import StreamWriter @entrypoint(checkpointer=checkpointer) async def main(inputs: dict, writer: StreamWriter) -> int: # [!code highlight] ...
재시도 정책 (Retry policy)
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.func import entrypoint, task
from langgraph.types import RetryPolicy
# This variable is just used for demonstration purposes to simulate a network failure.
# It's not something you will have in your actual code.
attempts = 0
# Let's configure the RetryPolicy to retry on ValueError.
# The default RetryPolicy is optimized for retrying specific network errors.
retry_policy = RetryPolicy(retry_on=ValueError)
@task(retry_policy=retry_policy)
def get_info():
global attempts
attempts += 1
if attempts < 2:
raise ValueError('Failure')
return "OK"
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def main(inputs, writer):
return get_info().result()
config = {
"configurable": {
"thread_id": "1"
}
}
main.invoke({'any_input': 'foobar'}, config=config)
'OK'
task·entrypoint 타임아웃 설정 (Set task and entrypoint timeouts)
@task 또는 @entrypoint와 함께 timeout 파라미터를 쓰면 단일 async 시도가 얼마나 오래 실행될 수 있는지 제한할 수 있어요. 타임아웃을 초 단위나 datetime.timedelta로 제공해요.
import asyncio
from langgraph.errors import NodeTimeoutError
from langgraph.func import entrypoint, task
from langgraph.types import RetryPolicy
@task(
timeout=1.0,
retry_policy=RetryPolicy(retry_on=NodeTimeoutError),
)
async def call_api(url: str) -> str:
await asyncio.sleep(2)
return f"result from {url}"
@entrypoint(timeout=5.0)
async def workflow(inputs: dict) -> str:
return await call_api(inputs["url"])
try:
await workflow.ainvoke({"url": "https://example.com"})
except NodeTimeoutError:
print("Task timed out")
타임아웃은 async task와 entrypoint에서만 지원돼요. 동기 함수에 timeout을 설정하면 LangGraph는 task나 entrypoint가 선언될 때 오류를 발생시켜요.
task나 entrypoint가 타임아웃을 초과하면 LangGraph는 Python 내장 TimeoutError를 상속하는 NodeTimeoutError를 발생시켜요. 재시도 정책이 TimeoutError나 NodeTimeoutError를 재시도하면, 타임아웃된 시도가 재시도돼요. 타임아웃은 각 시도에 독립적으로 적용되므로, 재시도마다 타이머가 리셋돼요.
태스크 캐싱 (Caching tasks)
import time
from langgraph.cache.memory import InMemoryCache
from langgraph.func import entrypoint, task
from langgraph.types import CachePolicy
@task(cache_policy=CachePolicy(ttl=120)) # [!code highlight]
def slow_add(x: int) -> int:
time.sleep(1)
return x * 2
@entrypoint(cache=InMemoryCache())
def main(inputs: dict) -> dict[str, int]:
result1 = slow_add(inputs["x"]).result()
result2 = slow_add(inputs["x"]).result()
return {"result1": result1, "result2": result2}
stream = main.stream_events({"x": 5}, version="v3")
for snapshot in stream.values:
print(snapshot)
ttl은 초 단위로 지정해요. 이 시간이 지나면 캐시가 무효화돼요.
오류 후 재개하기 (Resuming after an error)
import time
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.func import entrypoint, task
from langgraph.types import StreamWriter
# This variable is just used for demonstration purposes to simulate a network failure.
# It's not something you will have in your actual code.
attempts = 0
@task()
def get_info():
"""
Simulates a task that fails once before succeeding.
Raises an exception on the first attempt, then returns "OK" on subsequent tries.
"""
global attempts
attempts += 1
if attempts < 2:
raise ValueError("Failure") # Simulate a failure on the first attempt
return "OK"
# Initialize an in-memory checkpointer for persistence
checkpointer = InMemorySaver()
@task
def slow_task():
"""
Simulates a slow-running task by introducing a 1-second delay.
"""
time.sleep(1)
return "Ran slow task."
@entrypoint(checkpointer=checkpointer)
def main(inputs, writer: StreamWriter):
"""
Main workflow function that runs the slow_task and get_info tasks sequentially.
Parameters:
- inputs: Dictionary containing workflow input values.
- writer: StreamWriter for streaming custom data.
The workflow first executes `slow_task` and then attempts to execute `get_info`,
which will fail on the first invocation.
"""
slow_task_result = slow_task().result() # Blocking call to slow_task
get_info().result() # Exception will be raised here on the first attempt
return slow_task_result
# Workflow execution configuration with a unique thread identifier
config = {
"configurable": {
"thread_id": "1" # Unique identifier to track workflow execution
}
}
# This invocation will take ~1 second due to the slow_task execution
try:
# First invocation will raise an exception due to the `get_info` task failing
main.invoke({'any_input': 'foobar'}, config=config)
except ValueError:
pass # Handle the failure gracefully
실행을 재개할 때 slow_task의 결과는 이미 체크포인트에 저장돼 있으므로 다시 실행할 필요가 없어요.
main.invoke(None, config=config)
'Ran slow task.'
Human-in-the-loop
Functional API는 interrupt 함수와 Command 프리미티브로 human-in-the-loop 워크플로를 지원해요.
기본 human-in-the-loop 워크플로
세 개의 task를 만들 거예요.
"bar"를 추가해요.- 인간 입력을 위해 일시정지해요. 재개하면 인간 입력을 추가해요.
"qux"를 추가해요.
from langgraph.func import entrypoint, task
from langgraph.types import Command, interrupt
@task
def step_1(input_query):
"""Append bar."""
return f"{input_query} bar"
@task
def human_feedback(input_query):
"""Append user input."""
feedback = interrupt(f"Please provide feedback: {input_query}")
return f"{input_query} {feedback}"
@task
def step_3(input_query):
"""Append qux."""
return f"{input_query} qux"
이제 이 task들을 entrypoint에서 조합할 수 있어요.
from langgraph.checkpoint.memory import InMemorySaver
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def graph(input_query):
result_1 = step_1(input_query).result()
result_2 = human_feedback(result_1).result()
result_3 = step_3(result_2).result()
return result_3
interrupt()가 task 안에서 호출되어, 사람이 이전 task의 출력을 검토하고 편집할 수 있게 돼요. 이전 task들 — 여기서는 step_1 — 의 결과는 영속화되어, interrupt 이후 다시 실행되지 않아요.
쿼리 문자열을 보내 볼게요.
config = {"configurable": {"thread_id": "1"}}
stream = graph.stream_events("foo", config, version="v3")
for message in stream.messages:
for token in message.text:
print(token, end="", flush=True)
step_1 이후 interrupt로 일시정지했어요. 이 인터럽트는 실행을 재개할 방법에 대한 지침을 제공해요. 재개하려면 human_feedback task가 기대하는 데이터를 담은 Command를 발행해요.
# Continue execution
stream = graph.stream_events(Command(resume="baz"), config, version="v3")
for message in stream.messages:
for token in message.text:
print(token, end="", flush=True)
재개 후 실행은 남은 단계를 거쳐 기대대로 종료돼요.
도구 호출 검토 (Review tool calls)
실행 전에 도구 호출을 검토하려면 interrupt를 호출하는 review_tool_call 함수를 추가해요. 이 함수가 호출되면 재개 명령을 발행할 때까지 실행이 일시정지돼요.
도구 호출이 주어지면 우리 함수는 인간 검토를 위해 interrupt해요. 그 시점에 우리는 다음 중 하나를 할 수 있어요.
- 도구 호출을 수락
- 도구 호출을 수정하고 계속
- 커스텀 도구 메시지 생성 (예: 모델에게 도구 호출 재포맷 지시)
from typing import Union
def review_tool_call(tool_call: ToolCall) -> Union[ToolCall, ToolMessage]:
"""Review a tool call, returning a validated version."""
human_review = interrupt(
{
"question": "Is this correct?",
"tool_call": tool_call,
}
)
review_action = human_review["action"]
review_data = human_review.get("data")
if review_action == "continue":
return tool_call
elif review_action == "update":
updated_tool_call = {**tool_call, **{"args": review_data}}
return updated_tool_call
elif review_action == "feedback":
return ToolMessage(
content=review_data, name=tool_call["name"], tool_call_id=tool_call["id"]
)
이제 entrypoint를 갱신해 생성된 도구 호출을 검토하게 해요. 도구 호출이 수락되거나 수정되면 전처럼 실행해요. 그렇지 않으면 인간이 제공한 ToolMessage를 그냥 추가해요. 이전 task들 — 여기서는 초기 모델 호출 — 의 결과는 영속화되어 interrupt 이후 다시 실행되지 않아요.
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph.message import add_messages
from langgraph.types import Command, interrupt
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def agent(messages, previous):
if previous is not None:
messages = add_messages(previous, messages)
model_response = call_model(messages).result()
while True:
if not model_response.tool_calls:
break
# Review tool calls
tool_results = []
tool_calls = []
for i, tool_call in enumerate(model_response.tool_calls):
review = review_tool_call(tool_call)
if isinstance(review, ToolMessage):
tool_results.append(review)
else: # is a validated tool call
tool_calls.append(review)
if review != tool_call:
model_response.tool_calls[i] = review # update message
# Execute remaining tool calls
tool_result_futures = [call_tool(tool_call) for tool_call in tool_calls]
remaining_tool_results = [fut.result() for fut in tool_result_futures]
# Append to message list
messages = add_messages(
messages,
[model_response, *tool_results, *remaining_tool_results],
)
# Call model again
model_response = call_model(messages).result()
# Generate final response
messages = add_messages(messages, model_response)
return entrypoint.final(value=model_response, save=messages)
단기 메모리 (Short-term memory)
단기 메모리는 같은 thread id의 서로 다른 invocation에 걸쳐 정보를 저장할 수 있게 해줘요. 자세한 내용은 단기 메모리를 참고해요.
체크포인트 관리 (Manage checkpoints)
checkpointer가 저장한 정보를 보고 삭제할 수 있어요.
스레드 상태 보기 (View thread state)
config = {
"configurable": {
"thread_id": "1", # [!code highlight]
# optionally provide an ID for a specific checkpoint,
# otherwise the latest checkpoint is shown
# "checkpoint_id": "1f029ca3-1f5b-6704-8004-820c16b69a5a" # [!code highlight]
}
}
graph.get_state(config) # [!code highlight]
StateSnapshot(
values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today?), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]}, next=(),
config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}},
metadata={
'source': 'loop',
'writes': {'call_model': {'messages': AIMessage(content='Your name is Bob.')}},
'step': 4,
'parents': {},
'thread_id': '1'
},
created_at='2025-05-05T16:01:24.680462+00:00',
parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}},
tasks=(),
interrupts=()
)
스레드의 이력 보기 (View the history of the thread)
config = {
"configurable": {
"thread_id": "1" # [!code highlight]
}
}
list(graph.get_state_history(config)) # [!code highlight]
[
StateSnapshot(
values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?"), AIMessage(content='Your name is Bob.')]},
next=(),
config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1f5b-6704-8004-820c16b69a5a'}},
metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Your name is Bob.')}}, 'step': 4, 'parents': {}, 'thread_id': '1'},
created_at='2025-05-05T16:01:24.680462+00:00',
parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}},
tasks=(),
interrupts=()
),
StateSnapshot(
values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?'), HumanMessage(content="what's my name?")]},
next=('call_model',),
config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-1790-6b0a-8003-baf965b6a38f'}},
metadata={'source': 'loop', 'writes': None, 'step': 3, 'parents': {}, 'thread_id': '1'},
created_at='2025-05-05T16:01:23.863421+00:00',
parent_config={...}
tasks=(PregelTask(id='8ab4155e-6b15-b885-9ce5-bed69a2c305c', name='call_model', path=('__pregel_pull', 'call_model'), error=None, interrupts=(), state=None, result={'messages': AIMessage(content='Your name is Bob.')}),),
interrupts=()
),
StateSnapshot(
values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]},
next=('__start__',),
config={...},
metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "what's my name?"}]}}, 'step': 2, 'parents': {}, 'thread_id': '1'},
created_at='2025-05-05T16:01:23.863173+00:00',
parent_config={...}
tasks=(PregelTask(id='24ba39d6-6db1-4c9b-f4c5-682aeaf38dcd', name='__start__', path=('__pregel_pull', '__start__'), error=None, interrupts=(), state=None, result={'messages': [{'role': 'user', 'content': "what's my name?"}]}),),
interrupts=()
),
StateSnapshot(
values={'messages': [HumanMessage(content="hi! I'm bob"), AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')]},
next=(),
config={...},
metadata={'source': 'loop', 'writes': {'call_model': {'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}}, 'step': 1, 'parents': {}, 'thread_id': '1'},
created_at='2025-05-05T16:01:23.862295+00:00',
parent_config={...}
tasks=(),
interrupts=()
),
StateSnapshot(
values={'messages': [HumanMessage(content="hi! I'm bob")]},
next=('call_model',),
config={...},
metadata={'source': 'loop', 'writes': None, 'step': 0, 'parents': {}, 'thread_id': '1'},
created_at='2025-05-05T16:01:22.278960+00:00',
parent_config={...}
tasks=(PregelTask(id='8cbd75e0-3720-b056-04f7-71ac805140a0', name='call_model', path=('__pregel_pull', 'call_model'), error=None, interrupts=(), state=None, result={'messages': AIMessage(content='Hi Bob! How are you doing today? Is there anything I can help you with?')}),),
interrupts=()
),
StateSnapshot(
values={'messages': []},
next=('__start__',),
config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f029ca3-0870-6ce2-bfff-1f3f14c3e565'}},
metadata={'source': 'input', 'writes': {'__start__': {'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}}, 'step': -1, 'parents': {}, 'thread_id': '1'},
created_at='2025-05-05T16:01:22.277497+00:00',
parent_config=None,
tasks=(PregelTask(id='d458367b-8265-812c-18e2-33001d199ce6', name='__start__', path=('__pregel_pull', '__start__'), error=None, interrupts=(), state=None, result={'messages': [{'role': 'user', 'content': "hi! I'm bob"}]}),),
interrupts=()
)
]
반환 값과 저장 값 분리하기 (Decouple return value from saved value)
entrypoint.final을 사용해 호출자에게 반환되는 것과 체크포인트에 영속화되는 것을 분리할 수 있어요. 이런 경우에 유용해요.
- 계산된 결과(예: 요약이나 상태)를 반환하되, 다음 호출에 사용할 다른 내부 값을 저장하고 싶을 때.
- 다음 실행에서
previous파라미터로 넘겨질 값을 제어해야 할 때.
from langgraph.func import entrypoint
from langgraph.checkpoint.memory import InMemorySaver
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def accumulate(n: int, *, previous: int | None) -> entrypoint.final[int, int]:
previous = previous or 0
total = previous + n
# Return the *previous* value to the caller but save the *new* total to the checkpoint.
return entrypoint.final(value=previous, save=total)
config = {"configurable": {"thread_id": "my-thread"}}
print(accumulate.invoke(1, config=config)) # 0
print(accumulate.invoke(2, config=config)) # 1
print(accumulate.invoke(3, config=config)) # 3
챗봇 예시 (Chatbot example)
Functional API와 InMemorySaver checkpointer를 쓰는 간단한 챗봇 예시예요.
이 봇은 이전 대화를 기억하고 멈췄던 곳에서 계속 진행할 수 있어요.
from langchain.messages import BaseMessage
from langgraph.graph import add_messages
from langgraph.func import entrypoint, task
from langgraph.checkpoint.memory import InMemorySaver
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(model="claude-sonnet-4-6")
@task
def call_model(messages: list[BaseMessage]):
response = model.invoke(messages)
return response
checkpointer = InMemorySaver()
@entrypoint(checkpointer=checkpointer)
def workflow(inputs: list[BaseMessage], *, previous: list[BaseMessage]):
if previous:
inputs = add_messages(previous, inputs)
response = call_model(inputs).result()
return entrypoint.final(value=response, save=add_messages(inputs, response))
config = {"configurable": {"thread_id": "1"}}
input_message = {"role": "user", "content": "hi! I'm bob"}
stream = workflow.stream_events([input_message], config, version="v3")
for snapshot in stream.values:
print(snapshot)
input_message = {"role": "user", "content": "what's my name?"}
stream = workflow.stream_events([input_message], config, version="v3")
for snapshot in stream.values:
print(snapshot)
장기 메모리 (Long-term memory)
장기 메모리는 서로 다른 thread id에 걸쳐 정보를 저장할 수 있게 해줘요. 한 대화에서 특정 사용자에 대해 배운 정보를 다른 대화에서 쓸 때 유용해요.
워크플로 (Workflows)
- Workflows and agent 가이드는 Functional API로 워크플로를 만드는 더 많은 예시를 보여줘요.
다른 라이브러리와 통합 (Integrate with other libraries)
- Functional API로 LangGraph 기능을 다른 프레임워크에 추가하기: 영속성, 메모리, 스트리밍 같은 LangGraph 기능을 기본 제공하지 않는 다른 에이전트 프레임워크에 추가해요.
더 알아보기 (Learn more)
- Functional API: functional API의 개념을 다뤄요.
- Streaming 가이드: Graph API와 동일한 스트리밍 메커니즘을 다뤄요.
- 인터럽트: human-in-the-loop와
interrupt()를 다뤄요.