LangChain — GenAI enabler across digital spectrum

Rudra Amidhepuram
DataDreamers
Published in
8 min readJul 19, 2023

Hi folks, Have you ever wondered that how these ChatGPT and Google Bard and other AI chat bots are working? Well, they were built on top of Large Language Models (LLM). We can also create and cutomize our own AI applications using LLMs and LangChain. In this Blog I’m going to explain about Langchain.

What is Langchain is all about?

LangChain is a software development framework designed to simplify the creation of applications using large language models (LLMs). It was created by Harrison Chase in October 2022 and is open source software released under the MIT License.

LangChain provides a standard interface for agents, memory, and chains. Agents involve an LLM making decisions about which Actions to take, taking that Action, seeing an Observation, and repeating that until done. Memory refers to persisting state between calls of a chain/agent. Chains go beyond a single LLM call and involve sequences of calls (whether to an LLM or a different utility). Data Augmented Generation involves specific types of chains that first interact with an external data source to fetch data for use in the generation step.

Some of the features of LangChain include:

  • Agents: Agents are a way to make decisions using an LLM. They can be used to create chatbots, games, and other applications.
  • Memory: Memory is a way to store state between calls to an LLM. This can be used to keep track of the conversation in a chatbot or to store the results of previous queries.
  • Chains: Chains are a way to combine multiple LLM calls into a single application. This can be used to create summarization tools, question answering systems, and other applications.
  • Data Augmented Generation: Data Augmented Generation is a way to use an LLM to generate text that is based on external data. This can be used to create news articles, product descriptions, and other types of content.

LangChain is a powerful tool that can be used to create a wide variety of applications using LLMs. It is still under development, but it has already been used to create a number of impressive applications.

Here are some examples of applications that have been created using LangChain:

  • Chatbots: LangChain can be used to create chatbots that can hold conversations with humans. These chatbots can be used for customer service, education, or entertainment.
  • Games: LangChain can be used to create games that use LLMs to generate text, dialogue, and other content. These games can be used for education, entertainment, or research.
  • Summarization tools: LangChain can be used to create summarization tools that can automatically summarize text documents. These tools can be used for research, education, or business.
  • Question answering systems: LangChain can be used to create question answering systems that can answer questions about text documents. These systems can be used for research, education, or business.
  • Other applications: LangChain can be used to create a wide variety of other applications that use LLMs. Some examples include:
  • Content generation
  • Translation
  • Code generation
  • Data analysis
  • Medical diagnosis

Let’s come to the main components of Langchain.

PromptTempalets:

A prompt template in LangChain is a class that can be used to generate prompts for LLMs. A prompt is a piece of text that is used to guide the LLM’s response. In a High level Prompt templates will give an option to set the context of a LLM response. Prompt templates can be used to generate prompts for a variety of tasks, such as question answering, summarization, and creative writing.

for a better understanding, let’s look at few examples.

In the above code snippet, there no prompts were used. so the answer for the question “why did the duck cross the road? ” is so generic. Now let’s look at example of using a prompt.

code snippet for using PromtTemplate Img2

Let’s look at the above code snippet, there a key word “restaurant_template” is set to a string which sets the context for a LLM response. in the case above example. if you aks a question like “what are some good names for a restuarant that is a cafe that has live hard rock music and memorabilia”? then the context for LLM response should be “the model should act like a naming consultant for new restaurants and it should return a list of restaurant name, each should be short,catchy and easy to remember and aslo it should relate to the type of restaurant we are meaning”.

Img3 Output

The above image is the output of the Img 2., to understand this better in the img2 if you change the restaurant_description with other variable let’s say “description_02" then we will get different output, because the context will be set to “description_02” which mean model should suggest name for “a burger place that is themed with baseball memorabilia ”

Now let’s look at the other important component of the LangChain

Tools and Chains

Tools in LangChain are reusable modules that can be used to perform a variety of tasks, such as accessing data, generating text, and translating languages. Tools are implemented as Python classes and can be easily integrated into LangChain applications.

Chains in LangChain are sequences of tools that are executed in order to perform a specific task. Chains can be used to create complex applications that can perform a variety of tasks, such as question answering, summarization, and creative writing.

Here are some examples of tools and chains in LangChain:

  • The GoogleSearch tool can be used to search the web for information.
  • The TextGeneration tool can be used to generate text, such as poems, code, scripts, musical pieces, email, letters, etc.
  • The Translate tool can be used to translate text from one language to another.
  • The QuestionAnswering chain can be used to answer questions about a given topic.
  • The Summarization chain can be used to summarize a text document.

Tools and chains in LangChain provide a powerful way to create applications that use LLMs. They are easy to use and can be easily integrated into existing applications.

Here are some of the benefits of using tools and chains in LangChain:

  • Reusability: Tools and chains are reusable modules that can be used in multiple applications. This can save time and effort when developing new applications.
  • Flexibility: Tools and chains can be easily customized to meet the specific needs of an application. This allows developers to create applications that are tailored to their specific requirements.
  • Scalability: Tools and chains can be easily scaled to handle large amounts of data or complex tasks. This makes them a good choice for developing applications that need to handle large amounts of data or complex tasks.

If you are interested in creating applications that use LLMs, I recommend that you learn about tools and chains in LangChain. They are a powerful tool that can help you to create applications that are reusable, flexible, and scalable.

let’s at some coding examples of chains

Img4

In the above code snippet I used LLMChain and taken OpenAI LLM, after that I have taken an article which was of length 3533. if you look at the PromptTemplate, the context of the response was set to “Extract the key facts out the article and Don’t include opinions. Give each fact a number and keep them short sentence”. let’s say this as Task 1.

That mean from you are saying to the promptTemplate that, from the above article you only need “facts” but not opinions and that facts should be short in sentence and you need it number wise. Now I belive that you are clear about what a PromptTemplate is.

Img5

Once you execute the LLM chain the above was the output and the output was in the same format as we mentioned in the PromptTemplate. To understand more about chain let’s have one more example.

Img6

Here in the img6, I was using the same LLM and changed my PromptTemplate, saying “use the facts from the Task1 as input and write a short paragraph for investors using those facts.” Let’s say this as Task 2

as you can observe in the Task 1 from the given article we picked facts and in the Task 2 we used those facts and wrote some summary. here these tasks were performed seperately.

Here comes the intersting part

Img7

In the above code snippet I have used “Simple Sequential Chain”. Using this I have linked Task1 and Task2, by doing this I need not to run two task seperately and these thing will happen sequentially as per sequence defined. By this I hope you understand why and how chain are used in LangChain. However this is to give you a basic understand of chains and there are different type of chains in the Langchain for more chain please refer LangChain documentation.

I don’t want to make this a lengthy article, so I will make part 2 of it where I’ll be explaining remaining components of Langchain.

Before ending this article let me show you, How to make custom Chatbot for PDF using Langchain and ChatGPT.

Img8

you need to install and import all the libraries as show in above img, then you need generate openAI API key and you need provide that API key in order to exchange data between chatGPT and Langchain.

img9

After that, read your PDF file and make that into chucks such that each chunk_size is 1000.

Img 10

Now you need to import OpenAI Embeddings and convert your pdf text into embeddings. If you don’t know about embeddings, ignore for now, I’ll explain in the next article. But let me tell you in High level, Embeddings are vectors which are obtained by converting text data into numerical data.

Img 10

Now import Question answering chain from Langchain and start asking questions about your pdf document, In my case I have given a pdf about Transformer. So for my question it was able to answer it. One thing to note here is that the model is limited to PDF uploaded.

Thanks for reading..!!

Happy learning

Sources:

https://python.langchain.com/en/latest/index.html

https://www.youtube.com/watch?v=phHqvLHCwH4&list=PL8motc6AQftk1Bs42EW45kwYbyJ4jOdiZ&index=3

--

--