Introduction to Langchain Agents

In this blog, explore the potential of LangChain agents, a framework powered by large language models, to create intelligent applications that excel in natural language processing, text generation, and more.

GraphQL has a role beyond API Query Language- being the backbone of application Integration
background Coditation

Introduction to Langchain Agents

LangChain is an open-source framework designed for developing applications that utilize large language models (LLMs). The framework offers a standardized 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 specialized components within the LangChain framework that interact with the real world. These agents are specifically designed to perform well-defined tasks, such as answering questions, generating text, translating languages, and summarizing text. They serve as tools for automating tasks and engaging with real-world scenarios.

Functioning of LangChain Agents

LangChain agents harness 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, equipping them to excel at various 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 involve 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 include 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 utilized 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 gaining 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, and Arxiv tools to build a simple agent to write an essay. There’s a long list of tools available [here](https://python.langchain.com/docs/integrations/tools/) that an agent can use to interact with the outside world.

1. Installation

For this example we are required to install Langchain and the tools mentioned above, assuming you have a 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 initializes the DuckDuckGo search tool along with Arxiv and the 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, the Arxiv tool will have access to publications in Arxiv.org and the Wikipedia tool will search the Wikipedia articles.

4. Setting up the essay tool


llm = ChatOpenAI(temperature=0, openai_api_key=<openai_api_key>)
prompt_template = "Write an essay for the topic provided by the user with the help of following content: {content}"  
essay = LLMChain(  
    llm=llm,  
    prompt=PromptTemplate.from_template(prompt_template)  
)

This section sets up an essay tool using the ChatOpenAI model from LangChain. We define a prompt template for writing an essay, create a chain using the model and the prompt, and then define the tool.


Tool.from_function(  
	func=essay.run,  
	name="Essay",  
	description="useful when you need to write an essay"  
)

Here, we're creating a new tool using the `Tool.from_function` method.

5. Initializing the agent


tools = [  
    Tool(  
	    name="Search",  
	    func=search.run,  
	    description="useful for when you need to answer questions about current events."  
	),  
	Tool(  
	    name="Arxiv",  
	    func=arxiv.run,  
	    description="useful when you need an answer about encyclopedic general knowledge"  
	),  
	Tool(  
	    name="Wikipedia",  
	    func=wiki.run,  
	    description="useful when you need an answer about encyclopedic general knowledge"  
	),  
	Tool.from_function(  
	    func=essay.run,  
	    name="Essay",  
	    description="useful when you need to write an essay"  
	),
]
agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)

Here, we're initializing an agent with the tools we've defined. This agent will be able to write an essay for the topic provided by the user, the agent would gather the necessary information from the web, Wikipedia, or Arxiv articles.

6. Running the agent


prompt = "Write an essay in 1000 words for the topic {input}, use the tools to retrieve the necessary information"  
input = "Essay on Global Warming – Causes and Solutions"  
  
print(agent.run(prompt.format(input=input)))

Finally, we define a prompt for our agent and run it. The agent will search the web, Arxiv articles, and Wikipedia for information about global warming, fetch the content, and then write an essay on it.

Conclusion

In conclusion, LangChain agents offer several distinct advantages:

  •  User friendly: LangChain agents are intentionally designed to be easily understandable and accessible, even for developers who lack extensive experience with complex language models.
  • Versatility: The adaptability of LangChain agents allows them to find applications across a diverse range of scenarios and requirements.
  • Enhanced capabilities: By leveraging the power of language models, LangChain agents provide features such as natural language comprehension and generation, granting users access to advanced functionalities.

As advancements in language models continue to unfold, the potential for LangChain agents to become more potent and flexible also increases. This progress holds the potential to revolutionize our interactions with computers and reshape content creation processes. Looking ahead, there are several exciting ways in which LangChain agents could be employed:

  1. Realistic Chatbots: LangChain agents could be employed to create chatbots that are not only more realistic but also engaging, leading to more dynamic interactions.
  2. Educational Tools: LangChain agents have the potential to serve as the foundation for educational tools that enhance students' learning experiences by providing personalized and interactive learning content.
  3. Marketing Assistance: Businesses could leverage LangChain agents to develop marketing tools that effectively reach and engage their target audiences, thereby transforming marketing strategies.

The future holds immense promise for the evolution of LangChain agents, marking a pivotal step towards a more interactive and intelligent digital landscape.

Hello, I'm Rekib Ahmed. I am passionate about all things tech, with an interest in cooking, reading, and the always-changing world of innovation.

Want to receive update about our upcoming podcast?

Thanks for joining our newsletter.
Oops! Something went wrong.