Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

🛠️ LangChain Tools: A Complete Beginner's Guide

A hands-on implementation demonstrating how to create, bind, and execute custom tools using LangChain and OpenAI LLMs.

This repository walks through the complete lifecycle of LangChain tools—from creating your first tool to building an intelligent AI agent capable of automatically selecting and executing multiple tools.


📖 Overview

Large Language Models (LLMs) are excellent at reasoning and generating text, but they cannot perform external operations such as:

  • Mathematical calculations
  • Calling APIs
  • Accessing databases
  • Fetching live information
  • Executing Python functions

LangChain solves this problem using Tools.

A Tool is simply a Python function with metadata that an LLM can understand and invoke whenever required.

This project demonstrates:

  • Creating custom tools
  • Binding tools to an LLM
  • Automatic tool calling
  • Manual tool execution
  • Passing outputs between tools
  • Injecting hidden arguments
  • Building an intelligent ReAct Agent

Project Structure

.
│── tools_in_langchain.ipynb
│── README.md

The notebook contains multiple examples arranged from beginner to advanced concepts.


Features

  • Create custom LangChain tools
  • Tool metadata generation
  • Bind tools with OpenAI models
  • Automatic function calling
  • Manual execution of tool calls
  • Tool chaining
  • Injected tool arguments
  • Currency conversion API integration
  • ReAct Agent implementation
  • Beginner-friendly explanations

Technologies Used

  • Python
  • LangChain
  • LangChain OpenAI
  • OpenAI GPT Models
  • Requests
  • ExchangeRate API

Installation

Clone the repository

git clone https://github.com/Swizknife/tools-in-langchain.git

cd tools-in-langchain

Install dependencies

pip install langchain-openai
pip install langchain-core
pip install langchain
pip install requests

or

pip install -r requirements.txt

Environment Variables

Create an environment variable for your OpenAI API Key.

import os

os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

What You'll Learn

The notebook is divided into several sections.


1. Creating Your First Tool

A simple multiplication function is converted into a LangChain Tool using the @tool decorator.

@tool
def multiply(a: int, b: int):
    return a * b

LangChain automatically generates:

  • Tool name
  • Description
  • Input schema
  • Validation

You also learn how to inspect

multiply.name
multiply.description
multiply.args

2. Binding Tools to an LLM

Instead of only chatting,

llm.invoke(...)

the tool is attached to the model

llm.bind_tools([multiply])

Now the LLM can decide whether a tool is needed to answer a user's question.


3. Automatic Tool Calling

Example:

Multiply 3 by 1000

Instead of generating the answer directly,

the model returns a Tool Call.

AIMessage(
    tool_calls=[
        {
            "name":"multiply",
            "args":{"a":3,"b":1000}
        }
    ]
)

This is one of LangChain's core concepts.

The LLM does not execute the function.

It only decides which tool should be executed.


4. Executing Tool Calls

The notebook demonstrates how to manually execute the returned tool call.

tool_result = multiply.invoke(...)

The result is then appended back into the conversation.

Human Message

↓

AI Tool Call

↓

Python Function

↓

Tool Result

↓

Final AI Response

This illustrates the complete tool execution loop.


5. Building API Tools

The notebook introduces a more realistic use case.

A custom tool fetches live currency exchange rates from an external API.

get_conversion_factor()

The tool makes an HTTP request and returns

  • Base Currency
  • Target Currency
  • Conversion Rate

This demonstrates how LangChain tools can integrate with external services.


6. Injected Tool Arguments

One of the most interesting concepts demonstrated is

InjectedToolArg

The conversion tool requires a conversion rate.

However,

the user should never provide this value.

Instead,

the value is automatically injected from another tool.

convert(
    base_currency_value,
    conversion_rate
)

The conversion rate is hidden from the LLM and supplied programmatically.

This is useful for

  • API keys
  • Database connections
  • Authentication tokens
  • Hidden intermediate values
  • Internal application state

7. Tool Chaining

The notebook manually chains two tools together.

Workflow:

User Question

↓

Get Exchange Rate

↓

Extract Conversion Rate

↓

Inject Rate

↓

Convert Currency

↓

Final Answer

This demonstrates how multiple tools can work together to solve a complex task.


8. Creating an AI Agent

Finally, the notebook creates a LangChain Agent.

initialize_agent(...)

using

STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION

Unlike manual execution,

the Agent automatically

  • decides which tools to use
  • determines their order
  • executes them
  • returns the final response

without requiring manual orchestration.


Workflow

            User Query
                 │
                 ▼
          OpenAI Language Model
                 │
     ┌───────────┴────────────┐
     │                        │
Needs Tool?               Normal Response
     │
     ▼
 Select Appropriate Tool
     │
     ▼
 Execute Python Function
     │
     ▼
 Tool Output
     │
     ▼
 Final AI Response

Concepts Covered

  • LangChain Tools
  • Tool Decorators
  • Tool Metadata
  • Tool Binding
  • Function Calling
  • AIMessage
  • HumanMessage
  • ToolMessage
  • Tool Execution
  • InjectedToolArg
  • Tool Chaining
  • External APIs
  • ReAct Agent
  • Structured Chat Agent

Example Queries

Multiply 5 and 20

Multiply 100 and 400

Convert 50 USD to INR

What is the conversion factor between INR and USD?

Convert 1000 INR to USD

Learning Outcomes

After completing this notebook, you'll understand how to:

  • Build custom LangChain tools
  • Bind tools to LLMs
  • Inspect tool metadata
  • Handle tool calls manually
  • Execute tools programmatically
  • Pass outputs between tools
  • Hide internal arguments using InjectedToolArg
  • Integrate external REST APIs
  • Create autonomous LangChain Agents
  • Build AI applications capable of interacting with real-world systems

Future Improvements

Some possible extensions include:

  • Weather Tool
  • Calculator Tool
  • Wikipedia Search Tool
  • SQL Database Tool
  • Gmail Tool
  • Google Calendar Tool
  • File Reader Tool
  • PDF Question Answering Tool
  • Multi-tool AI Assistant
  • LangGraph Agent implementation

References

  • LangChain Documentation
  • OpenAI API
  • ExchangeRate API

Author

Soumya Sharma

Electronics & Communication Engineering
Birla Institute of Technology, Mesra

Interested in:

  • Generative AI
  • Large Language Models
  • AI Agents
  • LangChain
  • Machine Learning
  • Deep Learning

⭐ If you found this project helpful, consider giving it a star!

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages