A gentle introduction to 🦜️🔗Langchain
Tags: python, langchain, llm, chatgpt
Published: 2023-07-26 15:50:15

It's amazing how Langchain became so popular overnight (figuratively). In this post I explore what it does and why it became so hot. 

## Inception

[Langchain](https://github.com/langchain-ai/langchain) was open-sourced by [Harrison Chase](https://www.linkedin.com/in/harrison-chase-961287118/) in October 2022 and in mere span of 9 months, it has gained a mind boggling 56.2K stars on its Github repo. To put the skyrocketing growth into perspective, it took [Keras](https://github.com/keras-team/keras) 101 months (8 years) to gain 58.9k stars. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/59fgksh4c8qr1qe4q3cz.png)

👆 The growth looks ridiculous. 

## 🤔 What exactly is Langchain?

<mark>It is a Python framework designed to simplify the creation of applications using large language models (LLMs). Essentially, it comes with with powerful building blocks and integrations that you can use to build your own LLM application. However, simplify seems to be a understatement here.</mark>


## Getting started

Getting started is easy. Install Langchain with a simple pip install. 

```
pip install langchain
```

But wait, if you are using Python versions< 3.9 you probably will get clang compiler errors as such:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p0me50c5x9hgmhclpps.png)


For me, I like to use [pyenv](https://github.com/pyenv/pyenv) to switch Python versions.

Use Python version 3.9 to avoid the above errors when installing.

```
pyenv install 3.9.16 
pyenv global 3.9.16
pip install langchain
```

## Document loaders 

Document loaders provider an easy way to load data. As of today, there is a total of 115 document loaders. Loading from any supported sources are now reduced to just 2 lines of code: 1) initialize Loader object and 2) calling the `.load()` function.


Even reading a Youtube transcript is simplified down to: 
```python
from langchain.document_loaders import YoutubeLoader
loader = YoutubeLoader.from_youtube_url(
    "https://www.youtube.com/watch?v=QsYGlZkevEg", add_video_info=True
)

loader.load()
```

![](https://media.tenor.com/Yjx_r38x1aYAAAAS/mind-blown-explosion.gif)

Okay, I lied. You need 3 lines of code to load data from the supported sources. Remember to import the loader of your choice from `langchain.document_loaders`.

For the full list of support sources, please see [here](https://python.langchain.com/docs/integrations/document_loaders/).


## Chains and Document transformers

Think of chains as a means to glue together multiple Langchain primitive components together. Of course, there are already a few "pre-glued" chains such as [Summarization](https://python.langchain.com/docs/modules/chains/popular/summarize), [Retrieval QA](https://python.langchain.com/docs/modules/chains/popular/vector_db_qa), [SQL](https://python.langchain.com/docs/modules/chains/popular/sqlite), and etc.

Now, let's see how we can use a Summarization chain to summarize a recent Youtube video of ex-Senior Minister Mr Tharman's full team speeches for his intent to run for President of Singapore.

```python
from langchain import OpenAI, PromptTemplate, LLMChain
from langchain.chains.summarize import load_summarize_chain
from langchain.document_loaders import YoutubeLoader

llm = OpenAI(temperature=0, openai_api_key="<YOUR-OPENAI-API-KEY>")
loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=7cZk5QPbmZ8", add_video_info=True)
docs = loader.load()

chain = load_summarize_chain(llm, chain_type="map_reduce")
chain.run(docs)
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrpsqfig3cp9ci02ff01.png)


Unfortunately, our simple example to summarize the Youtube video didn't work. And the reason, is because gpt3.5 model currently accepts only max 4096 tokens and we have 7491 tokens (7235 in your prompt; 256 for the completion)! 

This is where Langchain's Document Transformers shine. Let's try again and use a `RecursiveCharacterTextSplitter` to split our text.

```python
from langchain import OpenAI, PromptTemplate, LLMChain
from langchain.chains.summarize import load_summarize_chain
from langchain.document_loaders import YoutubeLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter


llm = OpenAI(temperature=0, openai_api_key="<YOUR-OPENAI-API-KEY>")
loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=7cZk5QPbmZ8", add_video_info=True)
docs = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=3000, chunk_overlap=600, length_function=len, add_start_index=True)
texts = text_splitter.create_documents([docs[0].page_content])

chain = load_summarize_chain(llm, chain_type="map_reduce")
chain.run(texts)
```


🥳 And we get: 

_Mr Tarman is a potential candidate for the Singaporean presidency who is known for his independence of thought, sharp intellect, and commitment to social justice. He is seen as a unifying figure who can bring people together and is an advocate for equality of resources in the international community. He has been a mentor to the speaker since 2005 and has helped many people escape poverty and discrimination. He is a respected diplomat and is socially savvy, often invited to events and offering other senior public office holders to grace such causes. He is the ideal candidate for the upcoming presidential election._




## Wrap up

I've barely scratch the surface on what's possible with Langchain but you have seen how with merely 11 lines, we manage to: 

1. Download a Youtube video transcript.
2. Split the transcript into overlapping chunks.
3. For each of the chunk, send a prompt together with the chunk itself to OpenAI API to get a summary.
4. Finally, send a prompt to OpenAI API to get a summary of the summaries in step 3.


Now, I hope you understand why Langchain is so popular now. If not, try implementing the above without Langchain and see how much more time, effort, and hair pulled is required.



## Resources: 
- https://en.wikipedia.org/wiki/LangChain
- https://python.langchain.com/docs/modules/chains/
- https://js.langchain.com/docs/modules/data_connection/document_transformers/
- https://python.langchain.com/docs/modules/chains/popular/summarize