1. The Modern LangChain Agent Stack
To build agents in modern LangChain, we use LangGraph, which models agents as state graphs where the LLM decides node transitions (e.g. calling tools vs. returning the final response).2. In-Memory Working Agent Project
Let’s build a working agent equipped with a math evaluation tool and a dictionary database look-up tool.2.1 Install Dependencies
Run in your terminal:2.2 Complete Code Implementation
Save and run this script:- The agent notices
"Find siva's email"and runsget_user_email(employee_name="siva"). - The agent notices
"calculate 345 * 12"and runscalculate_math(expression="345 * 12"). - The agent merges both observations and outputs: “Siva’s email is [email protected] and 345 * 12 is 4140.”
3. Practice Exercises
Practice 1: Add a Currency Converter Tool
Extend the working agent by adding a new toolconvert_usd_to_eur(amount: float) -> float that multiplies the USD amount by 0.92. Run the query: "Find siva's email and convert 150 USD to EUR." and print the output.
Instructions:
- Write the
convert_usd_to_eurtool. - Add it to the
toolslist. - Call
create_react_agent(llm, tools). - Invoke the agent and print the final message content.
Solution
Solution