0
0
LangChainframework~30 mins

Creating tools for agents in LangChain - Try It Yourself

Choose your learning style9 modes available
Creating Tools for Agents with LangChain
📖 Scenario: You are building a simple assistant that can perform specific tasks using tools. These tools help the assistant answer questions or perform actions by connecting to external functions.
🎯 Goal: Create a LangChain agent that uses a custom tool to answer questions about the current date and time.
📋 What You'll Learn
Create a tool function that returns the current date and time as a string.
Create a LangChain Tool object using the tool function.
Create an agent that uses the tool to answer questions.
Run the agent with a sample question about the current date and time.
💡 Why This Matters
🌍 Real World
Agents with tools help automate tasks by connecting language models to real functions, making assistants smarter and more useful.
💼 Career
Understanding how to create and use tools in LangChain is valuable for building AI assistants, chatbots, and automation systems in software development roles.
Progress0 / 4 steps
1
Create a tool function to get current date and time
Write a Python function called get_current_datetime that returns the current date and time as a string using datetime.now() and strftime with format "%Y-%m-%d %H:%M:%S".
LangChain
Need a hint?

Use from datetime import datetime and return datetime.now().strftime("%Y-%m-%d %H:%M:%S").

2
Create a LangChain Tool using the function
Import Tool from langchain.agents and create a variable called datetime_tool that is a Tool object. Set its name to "CurrentDateTime", func to get_current_datetime, and description to "Returns the current date and time as a string.".
LangChain
Need a hint?

Use Tool(name=..., func=..., description=...) to create the tool.

3
Create an agent using the tool
Import initialize_agent and AgentType from langchain.agents. Create a variable called agent by calling initialize_agent with a list containing datetime_tool, an llm set to None (placeholder), and agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION. This sets up the agent to use the tool.
LangChain
Need a hint?

Use initialize_agent(tools=[datetime_tool], llm=None, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION).

4
Run the agent with a sample question
Call agent.run with the string "What is the current date and time?" to get the answer using the tool.
LangChain
Need a hint?

Use agent.run("What is the current date and time?") to ask the agent.