Добавление внешних инструментов в чат-бот

Проектирование агентных систем с LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Внешние инструменты с LangGraph

  Монитор с изображением микрочипа и надписью «API» в центре.

     

  • API-инструменты для чат-ботов

     
    • Новости
    • Базы данных
    • Социальные сети
    • И другие
Проектирование агентных систем с LangChain

Добавление инструмента Wikipedia

Люди читают книги, сидя на больших томах.

  Иконка 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

Давайте потренируемся!

Проектирование агентных систем с LangChain

Preparing Video For Download...