Introduction to Langchain Agents 🦜️

Introduction

LangChain is an open-source framework designed for developing applications utilising large language models (LLMs). The framework offers a standardised interface for constructing chains, a multitude of integrations with various tools, and pre-built end-to-end chains tailored for common applications.

LangChain agents are specialised components within the LangChain framework that interacts with the real world. These agents are specifically designed to perform well-defined tasks, such as answering questions, generating text, translating languages, and summarising text. They serve as tools for automating tasks and engaging with real-world scenarios.

Functioning of LangChain Agents

LangChain agents leverage the capabilities of large language models (LLMs) to process natural language input and generate corresponding output. These LLMs have undergone extensive training on vast datasets comprising text and code, enabling them to excel at a wide array of tasks, including comprehending queries, text generation, and language translation.

Architecture

The fundamental architecture of a LangChain agent is structured as follows:

  1. Input Reception: The agent receives natural language input from the user.
  2. Processing with LLM: The agent employs the LLM to process the input and formulate an action plan.
  3. Plan Execution: The agent executes the devised action plan, which might entail interacting with other tools or services.
  4. Output Delivery: Subsequently, the agent delivers the output of the executed plan back to the user.

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

  • Agent: The core of the architecture, responsible for processing input, generating action plans, and executing them.
  • Tools: These external resources are harnessed by the agent to accomplish tasks. They encompass a diverse range, from other LLMs to web APIs.
  • Toolkits: Toolkits consist of groups of tools purposefully assembled for specific functions. Examples include toolkits for question answering, text generation, and natural language processing.

By fostering an understanding of LangChain and its agent-based architecture, users can harness its potential for creating intelligent applications that proficiently process and generate natural language text.

Getting started

Agents use a combination of an LLM (or an LLM Chain) as well as a Toolkit in order to perform a predefined series of steps to accomplish a goal. For this example we will use Wikipedia, DuckDuckGo, Arxiv tool to build a simple agent to write essay. There’s a long list of tools available here that an agent can use to interact with the outside world.

1. Installation

For this example we are require to install Langchain and the tools mentioned above, assuming you have Python3.8 environment on your machine.

pip install arxiv wikipedia duckduckgo-search langchain openai

2. Importing Necessary libraries

from langchain.tools import Tool, DuckDuckGoSearchRun, ArxivQueryRun, WikipediaQueryRun  
from langchain.utilities import WikipediaAPIWrapper  

from langchain.agents import initialize_agent  
from langchain.agents import AgentType  
from langchain.chat_models import ChatOpenAI  
from langchain.chains import LLMChain  
from langchain.prompts import PromptTemplate

3. Setting up the tools

search = DuckDuckGoSearchRun()  
arxiv = ArxivQueryRun()  
wiki = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())

This initialises the DuckDuckGo search tool along with Arxiv and Wikipedia tool provided by LangChain. These tools will help us to build our essay agent. The DuckDuckGo tool allows you to search the web using DuckDuckGo and retrieve the results, similarly Arxiv tool will have access to publications in Arxiv.org and Wikipedia tool will search the Wikipedia articles.

read more