LangChain 101: A quickstart guide to LangChain concepts & its implementation

Abdurrahman
6 min readAug 21, 2023

--

Let’s break the ice on LangChain and understand what it brings to the world of generative AI. It is an NLP framework built by Harrison Chase which is useful in creating applications based on language models. There are tons of use cases, some of them are chatbots, generative question & answering, summarization etc. It provides a way to chain multiple components together to create a robust application. LangChain has many components like LLMs, Prompt Templates, Chains, Agents, and Memory. Let’s look at each one of them to understand how these components work.

LLMs: It is a wrapper around a large language model that helps in utilizing the functionality of the chosen large model like GPT 3.5, BLOOM etc. There are other LLM integrations such as Hugging Face, Cohere, OpenAI, etc. Other integrations can be found here. These integrations work with their respective APIs.

Prompt Templates: These are simply just templates for different kinds of prompts when using the LLM application. We generally take input from the user, create a prompt and then send it to the LLM. Example of prompt template -

template = """ 
Given the information {input} about a topic, I want you to create -
1. a short summary of the topic
2. do something...
3. do something...
"""

Chains: It allows to combine multiple components together to create a single, coherent application. An example of this could be, getting the user input, formatting it with a Prompt Template, and then passing it to an LLM. More complex chains can be built by combining multiple chains together, or by combining chains with other components. For example, get the data from a specific URL, summarize the returned data, and answer questions using the generated summary from the data.

Agents: Think of an agent as a BOT at your disposal to complete the task you assign to it, but with a brain of its own. BOT uses LLMs to think of what actions to perform to complete the task, perform the actions, seek an observation and repeat until done. This is part of a framework called Reasoning And Acting (ReAct) which is nothing but chain of thoughts + actions. If you want to know more about agents read this. And if you want to know about different agent types you can find them here.

Memory: It is exactly as the word suggests, remembering the information from earlier in the conversation. LangChain provides utilities to add memory to the application. There are different types of memory and details for each with an example can be found here.

Now let’s create a simple LangChain application to summarize the input text from the user using an LLM. Go ahead and install the required libraries.

pip install langchain
pip install openai

For demo purposes, I will be using OpenAIs model gpt-3.5-turbo, feel free to use any other. If you are following this article and want to use the same model, I suggest you head over to OpenAI’s website here and create a free account. Go to your profile and create an API key which will be used later. You can refer to the screenshot below to find the APIs section and how to create the API key.

All set, let’s start with the development of the application.

Import all the necessary libraries, we will use PromptTemplate to run the same prompt with different user input, ChatOpenAI is just a wrapper to use LLM and LLMChain to combine components together in our case generating a prompt and making an API call to LLM to get the summary.

from langchain import PromptTemplate 
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain

Now paste the API key that you created earlier in the following way.

open_AI_API_Key = 'PASTE YOUR API KEY HERE'

Let’s create a prompt template which takes in the input text and adds it to a prompt that we want to ask an LLM. The prompt template will be as follows

summary_template = """
Given the text {input_text} about a person, I want you to create -
1. a short summary
2. one positive and negative aspect of the person
"""
summary_promt_template = PromptTemplate(
input_variables=["input_text"],
template=summary_template)

Going forward let’s create a connection to the LLM model using the wrapper and the API key. There is a parameter called temperature which is nothing but the randomness/creativity of the output given by the LLM, a higher temperature results in more diverse and creative output, while a lower temperature makes the output more focused.

llm = ChatOpenAI(
temperature=0,
model="gpt-3.5-turbo",
openai_api_key=open_AI_API_Key)

Next, let’s create a chain in order to combine the components i.e. prompt template and LLM together.

chain = LLMChain(
llm=llm,
prompt=summary_promt_template)

And we are set, all we need to do now is just run the chain and we will get the summary. Before that don’t forget to add some input text which will be passed to the prepare the prompt. Create a variable called input_text and provide some information, you can do a simple Google search and copy and paste the information from Wikipedia or from anywhere you want. For example, search for Mark Zuckerberg and from his wikipedia page copy and paste some text. But keep in mind there is a token limit for each LLM, in our case gpt 3.5 has a token limit of 4096, so make sure you copy only the text below this number. The token limit is inclusive of prompt + response.

input_text = """
Mark Elliot Zuckerberg (/ˈzʌkərbɜːrɡ/; born May 14, 1984) is an American billionaire business magnate, computer programmer, internet entrepreneur, and philanthropist. He co-founded the social media website Facebook and its parent company Meta Platforms (formerly Facebook, Inc.), of which he is executive chairman, chief executive officer, and controlling shareholder.[1][2]

Zuckerberg attended Harvard University, where he launched Facebook in February 2004 with his roommates Eduardo Saverin, Andrew McCollum, Dustin Moskovitz, and Chris Hughes. Originally launched in only select college campuses, the site expanded rapidly and eventually beyond colleges, reaching one billion users in 2012. Zuckerberg took the company public in May 2012 with majority shares. In 2007, at age 23, he became the world's youngest self-made billionaire. He has since used his funds to organize multiple philanthropic endeavors, including the establishment of the Chan Zuckerberg Initiative.

Zuckerberg has been listed as one of the most influential people in the world on four occasions in 2008, 2011, 2016 and 2019 respectively and nominated as a finalist in 2009, 2012, 2014, 2015, 2017, 2018, 2020, 2021 and 2022. He was named Person of the Year by Time magazine in 2010, the same year when Facebook eclipsed more than half a billion users.[3][4][5] In December 2016, Zuckerberg was ranked tenth on the Forbes list of The World's Most Powerful People.[6] In the Forbes 400 list of wealthiest Americans in 2022, he was ranked 11th with a wealth of $57.7 billion, down from his status as the third-richest American in 2021 with a net worth of $134.5 billion. As of July 2023, Zuckerberg's net worth was estimated at $115.0 billion by Forbes, making him the 7th richest person in the world.[7] A film depicting Zuckerberg's early career, legal troubles and initial success with the site, The Social Network, was released in 2010 and won multiple Academy Awards.

Zuckerberg's prominence and fast rise in the technology industry has prompted political and legal attention. The founding of Facebook involved Zuckerberg in multiple lawsuits regarding the creation and ownership of the website as well as issues of user privacy. In 2013, he co-founded the pro-immigration lobbying group FWD.us. On April 10 and 11, 2018, Zuckerberg testified before the United States Senate Committee on Commerce, Science, and Transportation regarding the usage of personal data by Facebook in relation to the Facebook–Cambridge Analytica data breach.[8]
"""


print(chain.run(input_text=input_text))

Congratulations!! you have built your first LangChain application using a large language model. There is a lot that can be done with LangChain, this is just an introductory article to get you started with LangChain, you can explore more and create awesome applications using LangChain.

--

--