Langchain Agents

SaumyaDash
5 min readFeb 8, 2024

--

Introduction

According to papers and research by OpenAI, Google etc. it was demonstrated that utilizing chain of thought/ explaining the thought process out loud to arrive at an answer prevents the model from hallucinations or from generation of incorrect or nonsensical information when compared with outputting the answer immediately.

Langchain agents is one such component which works by breaking a complex task through the creation of multi-step action plan, determining and acting on each step one by one to arrive at the final answer.

It goes through a Thought/Action/Action Input/Observation loop akin to the chain-of-thought process described in the OpenAI and Google papers.

It works by combining:

Chain-of-thought reasoning + Acting

Another important module in LangChain is Tools- they are limited by their training data and lack access to current information or proprietary data — a company’s list of customers, for example. It’s based on the MRKL.

MRKL is an acronym for Modular Reasoning, Knowledge & Language system. It is a modular architecture that combines Large Language Models (LLMs), external knowledge sources & discrete reasoning.

This system consists of an extendable set of modules (called experts) and a router (usually the LLM itself) that routes a natural language (NL) query to the module that can best respond to the query.

Agents and Tools

Agents in LangChain serve as enabling tools for large language models, similar to how humans use calculators for math or perform Google searches for information. These agents empower large language models to perform various tasks, including writing and executing Python code, conducting Google searches, and even executing SQL queries.

The key components of LangChain agents include the agent itself, external tools, and toolkits:

* Agent: The core of the architecture, responsible for processing input, generating action plans, and executing them.

Combining a LLM with set of tools to solve specific problems.

* Tools: These external resources are utilized by the agent to accomplish tasks. They encompass a diverse range, from other LLMs to web APIs.

In langchain connectors to API and computational resources are called tools.

* Toolkits: Toolkits consist of groups of tools purposefully assembled for specific functions. Examples include toolkits for question answering, text generation, and natural language processing.

Combining these elements significantly expands the potential of large language models.

Tools and Toolkits

In LangChain, tools and toolkits provide additional functionality and capabilities to agents.

Tools are individual components that perform specific tasks, such as retrieving information from external sources or processing data.

Conversely, toolkits are collections of tools designed to work together and provide a more comprehensive set of functionalities.

Initialise the agent

There are two ways you can instantiate the agent:

AgentExecutor or initialize_agent.

AgentExecutor

The AgentExecutor class is responsible for executing the agent’s actions and managing the agent’s memory.

It takes an agent, a set of tools, and an optional memory object as input.

The AgentExecutor provides a more flexible and customizable way to run the agent, as you can specify the tools and memory to be used.

When to use AgentExecutor:

When you want more control over executing the agent’s actions and memory management.

When you want to specify the tools and memory to be used by the agent.

Initialize Agent

The initialize agent function is a convenience function provided by LangChain that simplifies creating an agent.

It takes the agent class, the language model, and an optional list of tools as input.

It automatically initializes the agent with the specified language model and tools.

When to use initialize agent:

When you want a simplified way to create an agent without specifying the memory. When you want to create an agent with default settings, quickly.

If you need more customization and control over the agent’s execution, you should use AgentExecutor.

If you prefer a more straightforward and quicker way to create an agent, you can use initialize agent.

The LLM agent is initialized together with the list of tools it should use. The tools can be used by the agent by utilizing the run method for each tool.

DEMO:

We can understand this better by understanding the below HR Chatbot demo which is built using the LangChain agents and tools modules and is powered by the ChatGPT model or gpt-3.5-turbo.

How it works:

Langchain agents here is provided with 3 tools namely:

- Timekeeping Policies

- Employee Data

- Calculator

Timekeeping Policies tool

This tool compares the question (turned into embeddings using OpenAI embedding model) with a vector database of numbers from timekeeping policies. It searches for the most similar vector embeddings to the question’s embeddings using a method called “cosine similarity search.

The tool finds the most similar text values in the database based on the query’s vector embeddings representation, and then it uses this text information to help the LLM answer the user’s question in a conversation. This process is known as in-context learning.

Each embedding represents the location of a text unit (like a word or a paragraph) in the embedding space.

Employee Data tool

Here Python REPL is used where the natural language (NL) query of the user (‘how many sick leaves do I have left’) is converted into Python syntax by the LLM in order for it to extract the information from the data frame needed to answer the query.

Here the ‘user’ is a placeholder that changes depending on who is currently logged in to the chatbot. For example, if the user is ‘Richard Santos’, the LLM will return a different Python operation for the same NL query and also a different output (11 sick leaves)

Here a wrapper provided by LangChain and provides the tool access to the dataframe.

Calculator tool

This is LangChain’s implementation of a math tool using the numexpr package in NumPy.

The LLM is directed to use this tool when presented with math/arithmetic-related tasks instead of going off and doing the computation on its own.

Appendix

Reference Link to Authors HR BOT: Github

--

--