Definirea nodurilor și muchiilor pentru apelarea flexibilă a funcțiilor

Proiectarea sistemelor agentice cu LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Construirea unui flux de lucru cu mai multe instrumente

   

  • Mai multe instrumente disponibile

    • Palindrom
    • Evenimente istorice
    • Wikipedia

Flux complet chatbot

Proiectarea sistemelor agentice cu LangChain

Definiți funcțiile fluxului de lucru

   

  • Creați o funcție de oprire

Nodurile chatbot și instrumente din fluxul de lucru.

Proiectarea sistemelor agentice cu LangChain

Definiți funcțiile fluxului de lucru

   

  • Creați o funcție de oprire

    • Verificați apelurile de instrumente

Regulile funcției de oprire evidențiate.

Proiectarea sistemelor agentice cu LangChain

Definiți funcțiile fluxului de lucru

   

  • Creați o funcție de oprire

    • Verificați apelurile de instrumente
    • Încheiați conversația dacă nu există

Regulile END evidențiate în flux

Proiectarea sistemelor agentice cu LangChain

Definiți funcțiile fluxului de lucru

   

  • Creați o funcție de oprire

    • Verificați apelurile de instrumente
    • Încheiați conversația dacă nu există    
  • Creați un apelant dinamic de instrumente

    • Returnați un răspuns dacă există un apel de instrument

Regulile Call_model evidențiate.

Proiectarea sistemelor agentice cu LangChain

Definiți funcțiile fluxului de lucru

   

  • Creați o funcție de oprire

    • Verificați apelurile de instrumente
    • Încheiați conversația dacă nu există    
  • Creați un apelant dinamic de instrumente

    • Returnați un răspuns dacă există un apel de instrument
    • Invocați LLM-ul doar cu nodul chatbot dacă nu există apeluri

Regulile Call_model evidențiate.

Proiectarea sistemelor agentice cu LangChain

Definiți funcțiile fluxului de lucru

   

  • Creați o funcție de oprire

    • Verificați apelurile de instrumente
    • Încheiați conversația dacă nu există    
  • Creați un apelant dinamic de instrumente

    • Returnați un răspuns dacă există un apel de instrument
    • Invocați LLM-ul doar cu nodul chatbot dacă nu există apeluri
  • Compilați graful complet

Regulile Call_model evidențiate.

Proiectarea sistemelor agentice cu LangChain

Creați o funcție de condiție de oprire

from langgraph.graph import MessagesState, START, END


# Use MessagesState to define the state of the stopping function def should_continue(state: MessagesState):
# Get the last message from the state last_message = state["messages"][-1]
# Check if the last message includes tool calls if last_message.tool_calls: return "tools"
# End the conversation if no tool calls are present return END
Proiectarea sistemelor agentice cu LangChain

Creați un apelant dinamic de instrumente

# Extract the last message from the history
def call_model(state: MessagesState):

last_message = state["messages"][-1]
# If the last message has tool calls, return the tool's response if isinstance(last_message, AIMessage) and last_message.tool_calls:
# Return the messages from the tool call return {"messages": [AIMessage(content=last_message.tool_calls[0]["response"])]}
# Otherwise, proceed with a regular LLM response return {"messages": [model_with_tools.invoke(state["messages"])]}
Proiectarea sistemelor agentice cu LangChain

Creați graful

workflow = StateGraph(MessagesState)












Proiectarea sistemelor agentice cu LangChain

Creați graful

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)

Nodurile chatbot și instrumente.

Proiectarea sistemelor agentice cu LangChain

Creați graful

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")

Adăugarea nodului START la chatbot.

Proiectarea sistemelor agentice cu LangChain

Creați graful

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")
# Define conditions, then loop back to chatbot workflow.add_conditional_edges("chatbot", should_continue, ["tools", END])

Graf cu condiții adăugate.

Proiectarea sistemelor agentice cu LangChain

Creați graful

workflow = StateGraph(MessagesState)


# Add nodes for chatbot and tools workflow.add_node("chatbot", call_model) workflow.add_node("tools", tool_node)
# Connect the START node to the chatbot workflow.add_edge(START, "chatbot")
# Define conditions, then loop back to chatbot workflow.add_conditional_edges("chatbot", should_continue, ["tools", END])
workflow.add_edge("tools", "chatbot")

Flux complet al grafului.

Proiectarea sistemelor agentice cu LangChain

Adăugarea memoriei

# Set up memory and compile the workflow
memory = MemorySaver()

app = workflow.compile( checkpointer=memory)
display(Image(app.get_graph() .draw_mermaid_png()))

Flux complet al grafului.

Proiectarea sistemelor agentice cu LangChain

Să exersăm!

Proiectarea sistemelor agentice cu LangChain

Preparing Video For Download...