Using LangChain to build LLM-powered applications

Avikumar Talaviya
AI Science
Published in
7 min readAug 10, 2023

--

Learn everything you should know about LangChain for large language model applications

source: medium

Introduction

Large language models have been the buzzword in recent times. Startups like OpenAI, Cohere, Anthropic, etc have released many LLMs during the past couple of years. Developers increasingly leverage LLMs to build AI applications for various use cases as these language models become more powerful. building LLM-based applications is complex because we have custom datasets, data storage and a series of tasks to perform before the expected result can be archived. To tackle this problem programmer Harrison Chase released a Python framework in October 2022 which allows to work with LLMs to develop applications. It is essentially a set of abstractions and components which integrates with various LLMs to develop AI applications. In this blog, we will briefly look into various functions and workings of the LangChain framework. so let’s get started!

(Note: This article was published on the Machine Hack blog platform — Click Here)

Table of contents:

  1. What is LangChain and what are its functions?
  2. Model I/O and Data connections
  3. Chains in LangChain
  4. Agents in LangChain
  5. Industry use-cases
  6. Conclusion

What is LangChain and what are its functions?

LangChain is a framework for AI developers to build LLM-powered applications with the support of a large number of model providers under its umbrella. It enables applications that are:

  • Data-aware: allowing integration with a wide range of external data sources
  • Agentic: Allowing language model to interact with its environment

The major value proposition of LangChain are:

  1. Components: A set of abstractions of language models, data sources, data transformers, vector stores, etc. components are modular and easy to use for developing applications.
  2. Off-the-shelf chains: A sequence of LLM calls and components assembly to accomplish high-level tasks.

Since the release of the LangChain in October 2022. It has become highly popular in the developer community, generating 54k+ GitHub stars. (GitHub repository: link)

The purpose of LangChain is very clear. as mentioned on their official GitHub page:

Large language models (LLMs) are emerging as a transformative technology, enabling developers to build applications that they previously could not. However, using these LLMs in isolation is often insufficient for creating a truly powerful app — the real power comes when you can combine them with other sources of computation or knowledge.

LangChain aims to bridge the gap of easy-to-use developer frameworks to build LLM-powered applications.

Functions supported by LangChain

LangChain has a set of modules that allow building the entire LLM application pipeline with numerous integrations with various data loaders, vector stores, LLM providers, etc. Let’s look at each module with its core function.

LangChain core modules (source: data science dojo)
  1. Model I/O: Interface with language models
  2. Data connection: Interface with application-specific data sources
  3. Chains: Construct a sequence of calls
  4. Agents: Flexible calls to chains to archive high-level task
  5. Memory: Persist application state between runs of a chain
  6. Callbacks: Log and stream steps of sequential chains

We will look at each module and its functions with code snippets in order to get a better idea of LangChain in the next sections.

Model I/O and Data connections

Model I/O

The model I/O module of LangChain allows us to interact with language models in the application environment. It is made of three functions, namely:

  1. Prompts: templates to manage input to the model
  2. Language models: an interface to call LLM for input and output
  3. Output parsers: information extractors from model
Models I/O workflow (source: official docs)

Let’s learn more about them in more detail:

  1. Prompts: Prompts have become a key point of conversion when it comes to LLMs. Prompts are the input to the LLM in a certain manner to get the desired output. Prompts are made of several components depending upon use cases. some of the prompt templates are as follows:
  • Few shot prompt templates
  • Composition
  • Partial prompt templates
  • Serialization

2. Language models: Models integration and interfaces that are supported by LangChain are mainly from two types:

  • LLMs: Models that take input prompts in order to return an output. for example, ‘ada’, ‘text-davinci’, etc
  • Chat Models: Models that take a list of chat messages as input to return an output message.

3. Output parsers: Output parsers structure the output of the model in a certain format rather than plain text output.

Data connections

LLMs are trained on huge amounts of public data, it does not cater to users’ private data. To get the best out of these language models, we need to use custom data sources to find answers based on them. LangChain gives you the building blocks to load, transform, store, and query your data for any use case. these blocks are as follows:

Data connection retriever workflow
  1. Document loaders: Set of classes to load data from external sources
  2. Document transformers: Split the documents in chunks to prepare for embedding models
  3. Text embedding models: Transforms unstructured text into vector embeddings for any LLM application
  4. Vector stores: Store and search over vector embeddings
  5. Retrievers: Retrieval functions to execute queries

Now, we will learn about Chaining in LangChain to streamline LLM workflow to accomplish certain tasks.

Chains in LangChain

Chains in LangChain allow calling LLMs in a sequential manner along with other tasks like retrieving data from sources and data transformations. LangChain provides a chain interface for such chained applications.

The concept of chaining is simple but powerful which simplifies complex LLM workflows in modular format and allows developers to be more productive. we will take one example of chaining multiple tasks to build an LLM workflow.

In our example, we will use an LLMChain block to prompt the input and get output from LLM.

# import necessary components from langchain 
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)

# create chain using LLMChain
chain = LLMChain(llm=llm, prompt=prompt)

# Run the chain only specifying the input variable.
print(chain.run("colorful socks"))

Output>>> Colorful Toes Co.

LangChain supports different types of chains to perform specific tasks and help build different types of applications. let’s look at the different types of chains supported in LangChain

  1. Sequential chain: Calling two or more chains for different tasks
  2. Load summarization chain: For summarizing documents
  3. Retrieval Q&A chain: for developing questions and answering applications on your custom data

To learn more about chains, I highly recommend reading their official documentation (Link: here)

Agents in LangChain

LangChain provides Agents which allow applications to utilize a dynamic chain of calls to various tools, including LLMs, based on user input. Agents have the capability to choose and employ multiple tools, using the output from one tool as input for the next tool in the chain. This flexibility enables the development of applications that can adapt and respond to different user requirements effectively.

There are two types of agents:

  1. Action agents: Decides next steps of action, after completion of each action stage.
  2. Plan and execute agents: This type of agent planes everything at once and executes each action subsequently.

Action agents are more appropriate for handling smaller tasks, whereas plan-and-execute agents excel in managing complex or lengthy tasks that involve the maintenance of long-term objectives and concentration.

Let’s look at the one code example:

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)

tools = load_tools(["serpapi", "llm-math"], llm=llm)

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")


----------------------------------[Result]------------------------------------

> Entering new AgentExecutor chain...
I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.
Action: Search
Action Input: "Leo DiCaprio girlfriend"
Observation: Camila Morrone
Thought: I need to find out Camila Morrone's age
Action: Search
Action Input: "Camila Morrone age"
Observation: 25 years
Thought: I need to calculate 25 raised to the 0.43 power
Action: Calculator
Action Input: 25^0.43
Observation: Answer: 3.991298452658078

Thought: I now know the final answer
Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078.

> Finished chain.





"Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078."

Industry use-cases

Now, we will learn about some of the use cases LangChain to build LLM-powered applications.

  1. Chatbots: Conversational assistants
  2. Question-answering over data: Build custom QA bots over your data
  3. Text Summarizer: summarize pdf, web document data
  4. Agents: build AI agents to accomplish multiple tasks with a single input
  5. Code understanding bots: Interprete code and build code assistants

Conclusion

In conclusion, LangChain emerges as a powerful and versatile framework that empowers AI developers to create cutting-edge LLM-powered applications with ease. By leveraging a vast number of models, LangChain offers various functions and modules to build powerful AI applications.

--

--