Externe Tools zu einem Chatbot hinzufügen

Agentische Systeme mit LangChain entwerfen

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Externe Tools mit LangGraph

  Desktop-Monitor mit Mikrochip-Grafik in der Mitte und „API“ in der Mitte.

     

  • API-Tools für Chatbots

     
    • Nachrichten
    • Datenbanken
    • Soziale Medien
    • usw.
Agentische Systeme mit LangChain entwerfen

Ein Wikipedia-Tool hinzufügen

Menschen lesen, sitzen auf großen Büchern.

  Wikipedia-Icon.

Agentische Systeme mit LangChain entwerfen

Ein Wikipedia-Tool hinzufügen

# 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]
Agentische Systeme mit LangChain entwerfen

Ein Wikipedia-Tool hinzufügen

# 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"])]}

   

  • Tools binden

 

  • Chatbot-Knoten aktualisieren

  • Wikipedia aktivieren

  • LLM entscheidet Tool-Aufrufe

Agentische Systeme mit LangChain entwerfen

Weitere API-Tools

  Desktop-Monitor mit Mikrochip-Grafik in der Mitte und „API“ in der Mitte.

   

Agentische Systeme mit LangChain entwerfen

Tool-Knoten hinzufügen

# 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)

Chatbot-Knoten.

Agentische Systeme mit LangChain entwerfen

Tool-Knoten hinzufügen

# 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)

Chatbot- und Tool-Knoten.

Agentische Systeme mit LangChain entwerfen

Tool-Knoten hinzufügen

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



Chatbot mit END- und Tool-Knoten, verbunden durch gestrichelte Linien für bedingte Kanten.

Agentische Systeme mit LangChain entwerfen

Tool-Knoten hinzufügen

# 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)

Vollständiges Chatbot-Diagramm: Chatbot-Knoten mit Tools und END verbunden, oben START-Knoten.

Agentische Systeme mit LangChain entwerfen

Lass uns üben!

Agentische Systeme mit LangChain entwerfen

Preparing Video For Download...