Conversation with a ReAct agent
1. Conversation with a ReAct agent
Nicely done! So far, we've just been printing2. Conversation
the agent's outputs. It's also useful to know that our agent is responding correctly by printing both the user's query as well as the response. First, we'll define3. Conversation
our tools and query. Then, we'll set up our agent using a pre-loaded model and tools. Next, we'll invoke the agent and store the response before printing both the query, labeled "user_input", and the agent's response as "agent_output".4. Conversation output
When we examine the output, it looks like our agent is working!5. Follow-up questions
To verify that our agent is working, we can ask follow-up questions. While answering, LangChain will update the whole conversation, before printing the answer again as a separate output.6. Follow-up questions
When looking at the output, we'll see a new query for a new rectangle. Then we'll see the full conversation, listing both the old and new queries with their respective answers. The "HumanMessage" refers to our own queries, while the "AIMessage" refers to the agent's answers. If the conversation is up to date, printing the last output will repeat the most recent query and answer, ensuring that the agent works.7. Conversation history
To set up our conversation history, we'll import the HumanMessage and AIMessage modules from LangChain's core messsages module.8. Conversation history
Then, we'll set up a variable called message_history that will store all of our messages.9. Conversation history
Next, we'll define a new query that will ask a new question without providing any additional contextual information. Here, we want to know the area of a new rectangle with different dimensions.10. Conversation history
Next, we'll invoke the app object again, this time passing both the message history and new query to the agent within a dictionary.11. Conversation history
We'll then filter out only the relevant messages from the agent’s response. Here, we'll use a list comprehension to select both HumanMessage and AIMessage instances that contain actual content. When applied to msg.content, the .strip() method removes any trailing whitespaces.12. Conversation history
Finally, we'll format and print the conversation extracted from msg.content, with each message labeled using its proper class name. The "user_input" is our new query, while the "agent_output" will print the full conversation and repeat the agent's most recent output, which is useful for debugging.13. Conversation history output
Let's see if we can produce the elements we need. We have our new query labeled "user_input". Then, we have our agent output listing the full conversation with labeled human and AI messages. When we ask the agent to list the last message, we have our most recent query and answer. Everything is in good shape!14. Let's practice!
Let's head over and practice building conversation!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.