Esnek fonksiyon çağrımı için düğüm ve kenarları tanımlama

LangChain ile Aracı Sistemler Tasarlama

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Çok araçlı bir iş akışı oluşturma

   

  • Birden çok kullanılabilir araç

    • Palindrom
    • Tarihî olaylar
    • Wikipedia

Tam sohbet botu iş akışı

LangChain ile Aracı Sistemler Tasarlama

İş akışı fonksiyonlarını tanımlayın

   

  • Durdurma fonksiyonu oluşturun

Sohbet botu iş akışının sohbet ve araç düğümleri.

LangChain ile Aracı Sistemler Tasarlama

İş akışı fonksiyonlarını tanımlayın

   

  • Durdurma fonksiyonu oluşturun

    • Araç çağrılarını kontrol edin

Durdurma fonksiyonu kuralları vurgulanmış.

LangChain ile Aracı Sistemler Tasarlama

İş akışı fonksiyonlarını tanımlayın

   

  • Durdurma fonksiyonu oluşturun

    • Araç çağrılarını kontrol edin
    • Yoksa sohbeti bitirin

İş akışında vurgulanan END kuralları

LangChain ile Aracı Sistemler Tasarlama

İş akışı fonksiyonlarını tanımlayın

   

  • Durdurma fonksiyonu oluşturun

    • Araç çağrılarını kontrol edin
    • Yoksa sohbeti bitirin    
  • Dinamik bir araç çağırıcı oluşturun

    • Araç çağrısı varsa araç yanıtını döndürün

Call_model kuralları vurgulanmış.

LangChain ile Aracı Sistemler Tasarlama

İş akışı fonksiyonlarını tanımlayın

   

  • Durdurma fonksiyonu oluşturun

    • Araç çağrılarını kontrol edin
    • Yoksa sohbeti bitirin    
  • Dinamik bir araç çağırıcı oluşturun

    • Araç çağrısı varsa araç yanıtını döndürün
    • Araç çağrısı yoksa LLM’i yalnızca sohbet düğümüyle çağırın

Call_model kuralları vurgulanmış.

LangChain ile Aracı Sistemler Tasarlama

İş akışı fonksiyonlarını tanımlayın

   

  • Durdurma fonksiyonu oluşturun

    • Araç çağrılarını kontrol edin
    • Yoksa sohbeti bitirin    
  • Dinamik bir araç çağırıcı oluşturun

    • Araç çağrısı varsa araç yanıtını döndürün
    • Araç çağrısı yoksa LLM’i yalnızca sohbet düğümüyle çağırın
  • Tam grafiği derleyin

Call_model kuralları vurgulanmış.

LangChain ile Aracı Sistemler Tasarlama

Durdurma koşulu fonksiyonu oluşturma

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
LangChain ile Aracı Sistemler Tasarlama

Dinamik bir araç çağırıcı oluşturma

# 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"])]}
LangChain ile Aracı Sistemler Tasarlama

Grafiği oluşturma

workflow = StateGraph(MessagesState)












LangChain ile Aracı Sistemler Tasarlama

Grafiği oluşturma

workflow = StateGraph(MessagesState)


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

Sohbet botu ve araç düğümleri.

LangChain ile Aracı Sistemler Tasarlama

Grafiği oluşturma

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

START düğümünü sohbet botuna ekleyin.

LangChain ile Aracı Sistemler Tasarlama

Grafiği oluşturma

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

Koşullar eklenmiş grafik.

LangChain ile Aracı Sistemler Tasarlama

Grafiği oluşturma

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

Tam grafik iş akışı.

LangChain ile Aracı Sistemler Tasarlama

Bellek ekleme

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

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

Tam grafik iş akışı.

LangChain ile Aracı Sistemler Tasarlama

Ayo berlatih!

LangChain ile Aracı Sistemler Tasarlama

Preparing Video For Download...