Day 2 - Module 5: Exercises
These exercises are based on the concepts and code presented in Module 5: Implementing Single Agents (ReAct Pattern) (src/05-single-agent/). Choose one of the framework examples (react-agent-lc.py, react-agent-li.py, or reasoning-agent-sk.py) to work with for exercises 5.1 and 5.2.
Exercise 5.1: Triggering Multiple Tools
Goal: Modify the input query to require the agent to use a sequence of tools to find the answer.
- Choose one of the agent scripts (
react-agent-lc.py,react-agent-li.py, orreasoning-agent-sk.py). - Modify the initial user input/query. Instead of asking for the time, ask a question that requires chaining information from multiple tools. For example:
- "What is the weather like for Dennis right now?" (Requires
get_current_username->get_current_location_of_user->get_weather) - "What time is it where Dennis is located?" (Requires
get_current_username->get_current_location_of_user->get_current_time)
- "What is the weather like for Dennis right now?" (Requires
- Run the chosen script.
- Observe the verbose output (enable
verbose=Trueif needed in LangChain/LlamaIndex, or observe the sequence of calls in Semantic Kernel). Does the agent correctly identify the need for multiple tools? Does it call them in a logical order (e.g., get username first, then location, then weather/time)? Does it arrive at the correct final answer?
Exercise 5.2: Adding a New Tool
Goal: Practice extending the agent's capabilities by adding a new tool.
- Choose one of the agent scripts.
- Define a new tool function: Add a simple Python function, for example, a basic calculator.
- Integrate the tool:
- LangChain (
react-agent-lc.py): Add the@tooldecorator above youradd_numbersfunction and include the function object in thetoolslist passed tocreate_react_agent. - LlamaIndex (
react-agent-li.py): Create aFunctionToolfrom your function (add_tool = FunctionTool.from_defaults(fn=add_numbers)) and addadd_toolto thetoolslist passed toReActAgent.from_tools. - Semantic Kernel (
reasoning-agent-sk.py): Add theadd_numbersfunction to theChefPluginclass inplugins.pyand decorate it with@kernel_function.
- LangChain (
- Modify the input query: Change the user input to ask a question that requires the new tool, e.g., "What is 25 plus 17?".
- Run the chosen script.
- Observe the output. Does the agent recognize the need for the
add_numberstool? Does it call the tool with the correct arguments (25 and 17)? Does it provide the correct sum (42) in its final answer?
Exercise 5.3 (Conceptual): Handling Tool Errors in ReAct
Goal: Think about how an agent reacts when a tool fails.
- Consider the ReAct loop: Thought -> Action -> Action Input -> Observation -> Thought...
- Imagine the
get_weathertool fails (e.g., the API is down, invalid location provided) and returns an error message or raises an exception. - What should the agent do in its next "Thought" step after receiving this error "Observation"?
- Should it try the same tool again?
- Should it try a different tool?
- Should it inform the user it cannot get the weather?
- Should it try to guess the weather?
- How does the specific prompt given to the ReAct agent (like the ones in
react-agent-lc.pyorreact-agent-li.py) influence how it handles errors? Do the prompts explicitly mention error handling? - How does the
handle_parsing_errors=Trueoption in LangChain'sAgentExecutorrelate to this? Does it handle tool execution errors or just errors in parsing the LLM's output?