Ask questions about conversation history
With a basic ReAct agent in LangChain, you can ask follow-up questions by keeping the agent's conversation history. Since the LLM has access to all previous messages, you can now ask new questions, and the agent can use the full message context to respond.
You'll now ask a follow-up question about the sides of a different triangle.
To be able to use the HumanMessage and AIMessage capabilities, the following modules have already been imported for you: HumanMessage
, AIMessage
.
This exercise is part of the course
Designing Agentic Systems with LangChain
Exercise instructions
- Assign the given natural language question to
new_query
. - Invoke the
app
object, passing in all of the messages, including themessage_history
and thenew_query
. - Use a list comprehension to extract messages from
response["messages"]
labeledHumanMessage
orAIMessage
. - Pass the new query as input and print the extracted messages by passing the message classes to
"agent_output"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
message_history = response["messages"]
____ = "What about one with sides 12 and 14?"
# Invoke the app with the full message history
response = app.____({"messages": ____ + [("human", ____)]})
# Extract the human and AI messages from the result
filtered_messages = [msg for msg in ____["____"] if isinstance(msg, (____, ____)) and msg.content.strip()]
# Pass the new query as input and print the final outputs
print({
"user_input": ____,
"agent_output": [f"{msg.____.____}: {msg.content}" for msg in ____]
})