그래프에 도구 추가하기
Wikipedia 도구가 준비되었어요. 이제 이 도구를 챗봇의 그래프 워크플로에 통합해 볼까요? 그래프 워크플로의 노드와 엣지를 수정해 도구를 추가 노드로 포함하겠습니다. 사용자 요청에 따라 필요할 때만 챗봇이 도구를 사용하도록 그래프를 설정할 거예요. 도구 노드와 관련 엣지를 관리할 수 있도록 아래 모듈이 이미 임포트되어 있으며, 앞에서 만든 wikipedia_tool도 환경에 준비돼 있어요. 그래프 구성이 완료되면 LangGraph 다이어그램으로 시각화됩니다.
from langgraph.prebuilt import ToolNode, tools_condition
이 연습은 강의의 일부입니다
LangChain으로 에이전트형 시스템 설계하기
연습 안내
.add_node()을 사용해 그래프에"chatbot"노드를 추가하고chatbot함수에 연결하세요.wikipedia_tool을tools로 사용해ToolNode()를 만들고,.add_node()로 그래프에"tools"로 추가하세요.tools_condition에 따라"chatbot"에서 분기하도록.add_conditional_edges()를 적용하세요.- 워크플로를 완성하려면
.add_edge()로"tools"를"chatbot"으로 되돌리고,START에서"chatbot"으로,"chatbot"에서END로 연결하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Add chatbot node to the graph
graph_builder.____("____", ____)
# Create a ToolNode to handle tool calls and add it to the graph
tool_node = ____(tools=[____])
graph_builder.____("tools", ____)
# Set up a condition to direct from chatbot to tool or END node
graph_builder.____("____", ____)
# Connect tools back to chatbot and connect START and END nodes
graph_builder.add_edge("____", "____")
graph_builder.add_edge(____, "chatbot")
graph_builder.add_edge("chatbot", ____)
graph = graph_builder.compile()
try:
display(Image(graph.get_graph().draw_mermaid_png()))
except Exception:
print("Plot generation failed... falling back to cached asset.")
display_fallback()