Sameep Sawant

Jan 04, 2025 • 4 min read

From setup to action: Building LangChain agents made easy.

From setup to action: Building LangChain agents made easy.

In today’s tech world, everyone is experimenting with ChatGPT, leveraging its power to simplify tasks, automate workflows, and boost productivity. Among its numerous applications, one of the most adopted use cases is Retrieval Augmented Generation (RAG) - a system designed to train on private datasets and provide solid tailored answers.

Developers often rely on agents, a vital part of AI workflows, to create this system. These agents aren’t just tools; they are smart assistants that combine reasoning and action to handle tasks efficiently. With their ability to decide, act, and fetch information, agents have become indispensable for creating powerful, flexible AI solutions.

Setting up these agents often feels like a scary task. Questions like Where do I even start? How do I configure them? Will I need to spend hours debugging? can intimidate even an experienced developer. But don’t worry - I have got your back.

In this blog, I’ll guide you through a few simple steps to set up a basic Langchain agent for yourself and get started with it. By the end of this blog, you’ll be ready to create your agent and start experimenting.

For this guide, we’ll use Langchain as our framework and Python as our programming language. Let’s dive in and make building agents as simple and exciting as possible!

To execute the steps in this guide, you can use any code editor you’re comfortable with or a cloud-based platform like Google Colab. Both options make it simple and accessible to experiment with LangChain. I am using Google Colab for code execution.

First let's create an Open AI API key, required to use any GPT model.

Log in to the OpenAI Platform

  1. Visit the Open AI dashboard and log in to your account.

  2. After logging in, you’ll be redirected to the OpenAI dashboard.

Navigate to the API Key Section

  1. Use the search bar for “API Keys,” or go directly to this Link.

  2. This will take you to the API key management page.

Create a New API Key

  1. Click the Create New Secret Key button.

  2. Enter a name for your key (e.g., “Test Key”) in the input field.

  3. Click Create Secret Key to generate the API key.

Copy and Save Your API Key

  1. Once the key is generated, it will be displayed on the screen.

Important: Copy the key and save it securely. You won’t be able to view it again later.

Let’s start creating an agent -

Step 1: Install dependencies

Run the following command to install LangChain and related dependencies.

pip install langchain langchain-community langchain-core langchain-openai

// If you are using IDE - 
pip install dotenv

Step 2: Set up the API Key

You’ll need an API key for OpenAI’s GPT models. Store it securely using environment variables.

Normal IDE

Create a .env file and add:

OPENAI_API_KEY=your_openai_api_key_here
from dotenv import load_dotenv
import os

load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")

Google Colab

from google.colab import userdata
my_secret_key = userdata.get('OPENAI_API_KEY')

Step 3: Initialize LangChain

Start by importing LangChain components:

from langchain_openai import OpenAI

llm = OpenAI(temperature=0.7, openai_api_key=my_secret_key)

Step 4: Create a Simple Tool

An agent often uses tools to perform actions. Let’s create a basic tool that performs addition.

from langchain.agents import tool

@tool
def add_numbers(inputs: str) -> str:
    """Adds two numbers separated by a comma."""
    num1, num2 = map(int, inputs.split(","))
    return str(num1 + num2)


Step 5: Define the Agent

An agent integrates the LLM and tools to perform tasks dynamically.

from langchain.agents import initialize_agent, AgentType

# Create an agent with tools
agent = initialize_agent(
    tools=[add_numbers],
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

Step 6: Run the Agent

Let’s test the agent with an input query.

response = agent.invoke("What is 3 + 7?")
print(response)

Output -

That’s it—you’ve successfully created your first agent! From here, the possibilities are endless. You can enhance your agent by adding tools, such as searching the web, fetching data, or performing calculations. You can customize the LLM’s behavior by adjusting its parameters or diving into multi-agent systems to handle advanced workflows.

In addition, Autogen and CrewAI are other excellent frameworks to explore while creating agent workflows. Each alternative has unique strengths, so choose the one that best aligns with your project needs and goals.

The journey doesn’t stop here—this is just the beginning of what you can build with LangChain and Python. Experiment, innovate, and have fun creating smarter solutions for your tasks!

Stay tuned for upcoming blogs, where we’ll explore the further possibilities of agents, delve into advanced tools, and uncover how to create multi-agent systems for complex workflows.

Join Sameep on Peerlist!

Join amazing folks like Sameep and thousands of other people in tech.

Create Profile

Join with Sameep’s personal invite link.

2

5

0