AI Agents
Tool use, ReAct loop — an agent that searches, calculates, and makes decisions autonomously.
GitHub Copilot doesn't just autocomplete — it can read your entire repo, run tests, write a fix, and open a PR. That's an AI agent.
Agents are LLMs that can USE TOOLS: search the web, run code, read files, call APIs, query databases. They decide which tool to call, call it, observe the result, and repeat until the task is done. ReAct = Reason + Act.
📈 The ReAct Loop (5–25 min)
Thought — LLM reasons about what to do next.
Action — LLM calls a tool (calculator, search, Python REPL).
Observation — tool returns a result.
Final Answer — LLM synthesises all observations into a response.
💻 Live Demo — Research Agent (25–45 min)
>from langchain_groq import ChatGroq from langchain.agents import create_react_agent, AgentExecutor from langchain_core.tools import tool from langchain import hub import os, math llm = ChatGroq(model="llama3-8b-8192", api_key=os.getenv("GROQ_API_KEY"), temperature=0) # Define tools the agent can use @tool def calculator(expression: str) -> str: "Evaluate a mathematical expression. Input: a Python math expression string." try: result = eval(expression, {"__builtins__": {}}, vars(math)) return f"Result: {result}" except Exception as e: return f"Error: {e}" @tool def unit_converter(value_and_units: str) -> str: "Convert units. Input format: "100 km to miles" or "5 kg to pounds"." parts = value_and_units.split() if len(parts) < 4: return "Invalid format. Use: "100 km to miles"" val, from_u, _, to_u = parts[0], parts[1].lower(), parts[2], parts[3].lower() conversions = {("km","miles"): 0.621371, ("kg","pounds"): 2.20462, ("celsius","fahrenheit"): None} if (from_u, to_u) in conversions: factor = conversions[(from_u, to_u)] if factor: return f"{float(val) * factor:.4f} {to_u}" else: return f"{float(val) * 9/5 + 32:.1f} fahrenheit" return "Conversion not supported" tools = [calculator, unit_converter] # Load standard ReAct prompt from LangChain hub react_prompt = hub.pull("hwchase17/react") agent = create_react_agent(llm, tools, react_prompt) executor = AgentExecutor(agent=agent, tools=tools, verbose=True, max_iterations=5) result = executor.invoke({ "input": "If I run 42.2 km (a marathon) and burn 2800 calories, how many calories per mile is that?" }) print(result["output"])
📋 You Code It (45–55 min)
Add a word_counter tool that counts words in a string. Ask the agent: "Count the words in this sentence and tell me the square root of that number: The quick brown fox jumps over the lazy dog."
⚡ Day 12 Cheat Sheet
@toolcreate_react_agent(llm, tools, prompt)AgentExecutor(agent, tools)executor.invoke({"input": q})verbose=Truemax_iterations=5hub.pull("hwchase17/react")tool docstring