IniziaInizia gratis

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

Visualizza il corso

Istruzioni dell'esercizio

  • Assign the given natural language question to new_query.
  • Invoke the app object, passing in all of the messages, including the message_history and the new_query.
  • Use a list comprehension to extract messages from response["messages"] labeled HumanMessage or AIMessage.
  • 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 ____]
})
Modifica ed esegui il codice