Thêm bộ nhớ và hội thoại

Thiết kế Hệ thống Agentic với LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Kiểm thử việc dùng công cụ

Sơ đồ chatbot đầy đủ với nút công cụ được thêm.

Thiết kế Hệ thống Agentic với LangChain

Kiểm thử việc dùng công cụ

# Produce the chatbot graph
display(Image(app.get_graph().draw_mermaid_png()))


# Define a function to execute the chatbot, streaming each message def stream_tool_responses(user_input: str): for event in graph.stream({"messages": [("user", user_input)]}):
# Return the agent's last response for value in event.values(): print("Agent:", value["messages"])
# Define the query and run the chatbot user_query = "House of Lords" stream_tool_responses(user_query)
Thiết kế Hệ thống Agentic với LangChain

Trực quan hóa sơ đồ

Sơ đồ chatbot LangGraph với nút công cụ

Thiết kế Hệ thống Agentic với LangChain

Streaming đầu ra

Agent: [AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'arguments': 
'{"query":"House of Lords"}', 'name': 'wikipedia'}, 'type': 'function'}]},
response_metadata={'...'}])]

Agent: [ToolMessage(content='Page: House of Lords\nSummary: The House of Lords is the upper house of the Parliament of the United Kingdom. Like the lower house...The House of Lords also has a Church of England role...', name='wikipedia', id='...',')]
Agent: [AIMessage(content='The House of Lords is the upper house of the Parliament of the United Kingdom, located in the Palace of Westminster in London. It is one of the oldest institutions in the world..., additional_kwargs={},
response_metadata= {'finish_reason': 'stop', 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_0ba0d124f1'}, id='run-ae3a0b4f-5b42-4f4e-9409-7c191af0b9c9-0')]
Thiết kế Hệ thống Agentic với LangChain

Thêm bộ nhớ

# Import the modules for saving memory
from langgraph.checkpoint.memory import MemorySaver


# Modify the graph with memory checkpointing memory = MemorySaver()
# Compile the graph passing in memory graph = graph_builder.compile(checkpointer=memory)
Thiết kế Hệ thống Agentic với LangChain

Streaming đầu ra với bộ nhớ

# Set up a streaming function for a single user
def stream_memory_responses(user_input: str):

config = {"configurable": {"thread_id": "single_session_memory"}}
# Stream the events in the graph for event in graph.stream({"messages": [("user", user_input)]}, config):
# Return the agent's last response for value in event.values(): if "messages" in value and value["messages"]: print("Agent:", value["messages"])
stream_memory_responses("What is the Colosseum?") stream_memory_responses("Who built it?")
Thiết kế Hệ thống Agentic với LangChain

Tạo đầu ra với bộ nhớ

stream_memory_responses("What is the Colosseum?")
Agent: [AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 
'id': '...', 'function': {'arguments': '
{"query":"Colosseum"}', 'name': 'wikipedia'}, ...])]

Agent: [ToolMessage(content='Page: Colosseum\nSummary: The Colosseum is an ancient amphitheatre in Rome, Italy. It is the largest standing amphitheatre in the world..)]
Agent: [AIMessage(content='The Colosseum, located in Rome, is the largest ancient amphitheatre still standing. Built under Emperor Vespasian and completed by his son Titus, it hosted gladiatorial games and public events. It is also known as the Flavian Amphitheatre due to its association with the Flavian dynasty.', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4o-mini-...', ...')]
Thiết kế Hệ thống Agentic với LangChain

Tạo đầu ra với bộ nhớ

stream_memory_responses("Who built it?")
Agent: [AIMessage(content='The Colosseum was built by Emperor Vespasian around 
72 AD and completed by his successor, Emperor Titus. Later modifications were 
made by Emperor Domitian...')]
Thiết kế Hệ thống Agentic với LangChain

Cùng luyện tập nào!

Thiết kế Hệ thống Agentic với LangChain

Preparing Video For Download...