챗봇에 외부 도구 추가

LangChain으로 에이전트형 시스템 설계하기

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

LangGraph로 외부 도구 연결

  가운데에 마이크로칩 그래픽과 "API"가 있는 데스크톱 모니터.

     

  • 챗봇용 API 도구

     
    • 뉴스
    • 데이터베이스
    • 소셜 미디어
    • 기타
LangChain으로 에이전트형 시스템 설계하기

Wikipedia 도구 추가

큰 책 위에 앉아 독서하는 사람들.

  위키백과 아이콘.

LangChain으로 에이전트형 시스템 설계하기

Wikipedia 도구 추가

# Modules for building a Wikipedia tool
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_community.tools import WikipediaQueryRun


# Initialize Wikipedia API wrapper to fetch top 1 result api_wrapper = WikipediaAPIWrapper(top_k_results=1)
# Create a Wikipedia query tool using the API wrapper wikipedia_tool = WikipediaQueryRun(api_wrapper=api_wrapper)
tools = [wikipedia_tool]
LangChain으로 에이전트형 시스템 설계하기

Wikipedia 도구 추가

# Bind the Wikipedia tool to 
# the language model
llm_with_tools = llm.bind_tools(tools)


# Modify chatbot function to # respond with Wikipedia def chatbot(state: State): return {"messages": [llm_with_tools.invoke( state["messages"])]}

   

  • 도구 바인딩

 

  • 챗봇 노드 업데이트

  • Wikipedia 사용

  • LLM이 도구 호출 결정

LangChain으로 에이전트형 시스템 설계하기

기타 API 도구

  가운데에 마이크로칩 그래픽과 "API"가 있는 데스크톱 모니터.

   

LangChain으로 에이전트형 시스템 설계하기

도구 노드 추가

# Modules for adding tool conditions 
# and nodes
from langgraph.prebuilt import 
ToolNode, tools_condition


# Add chatbot node to the graph graph_builder.add_node("chatbot", chatbot)

챗봇 노드.

LangChain으로 에이전트형 시스템 설계하기

도구 노드 추가

# Modules for adding tool conditions 
# and nodes
from langgraph.prebuilt import 
ToolNode, tools_condition

# Add chatbot node to the graph
graph_builder.add_node("chatbot",
                       chatbot)


# Create a ToolNode to handle tool calls # and add it to the graph tool_node = ToolNode(tools=[wikipedia_tool]) graph_builder.add_node("tools", tool_node)

챗봇과 도구 노드.

LangChain으로 에이전트형 시스템 설계하기

도구 노드 추가

# Set up a condition to direct from chatbot 
# to tool or end node
graph_builder.add_conditional_edges(
             "chatbot", tools_condition)



점선으로 표시된 조건부 간선으로 END 및 도구 노드에 연결된 챗봇.

LangChain으로 에이전트형 시스템 설계하기

도구 노드 추가

# Set up a condition to direct from chatbot 
# to tool or end node
graph_builder.add_conditional_edges(
             "chatbot", tools_condition)



# Connect tools back to chatbot and # add START and END nodes graph_builder.add_edge("tools", "chatbot") graph_builder.add_edge(START, "chatbot") graph_builder.add_edge("chatbot", END)

도구와 END 노드에 연결되고 위에 START 노드가 있는 전체 챗봇 다이어그램.

LangChain으로 에이전트형 시스템 설계하기

Ayo berlatih!

LangChain으로 에이전트형 시스템 설계하기

Preparing Video For Download...