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.
Questo esercizio fa parte del corso
Designing Agentic Systems with LangChain
Istruzioni dell'esercizio
- Assign the given natural language question to
new_query. - Invoke the
appobject, passing in all of the messages, including themessage_historyand thenew_query. - Use a list comprehension to extract messages from
response["messages"]labeledHumanMessageorAIMessage. - Pass the new query as input and print the extracted messages by passing the message classes to
"agent_output".
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
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 ____]
})