A Beginner’s Guide to Building LLM-Powered Applications with LangChain!
Tags: datascience, dataengineering, llm, database
Published: 2023-08-30 05:11:03

If you're a developer or simply someone passionate about technology, you've likely encountered AI tools such as ChatGPT. These utilities are powered by advanced large language models (LLMs). Interested in taking it up a notch by crafting your own LLM-based applications? If so, LangChain is the platform for you.

Let's keep everything aside and understand about LLMs first. Then, we can go over LangChain with a simple tutorial. Sounds interesting enough? Let's get going.

## What are Large Language Models (LLMs)? 
Large Language Models (LLMs) like GPT-3 and GPT-4 from OpenAI are machine learning algorithms designed to understand and generate human-like text based on the data they've been trained on. These models are built using neural networks with millions or even billions of parameters, making them capable of complex tasks such as translation, summarization, question-answering, and even creative writing. 

Trained on diverse and extensive datasets, often encompassing parts of the internet, books, and other texts, LLMs analyze the patterns and relationships between words and phrases to generate coherent and contextually relevant output. While they can perform a wide range of linguistic tasks, they are not conscious and don't possess understanding or emotions, despite their ability to mimic such qualities in the text they generate.

![how llms work](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6lrpjbdclbghbk74mhdb.png) **Source Credits**: [NVIDIA](https://www.nvidia.com/en-us/glossary/data-science/large-language-models/)

Large language models primarily belong to a category of deep learning structures known as transformer networks. A transformer model is a type of neural network that gains an understanding of context and significance by identifying the connections between elements in a sequence, such as the words in a given sentence.

## What is LangChain?
Developed by [Harrison Chase](https://twitter.com/hwchase17), and debuted in October 2022, [LangChain](https://www.langchain.com/) serves as an open-source platform designed for constructing sturdy applications powered by Large Language Models, such as chatbots like ChatGPT and various tailor-made applications.

Langchain seeks to equip data engineers with an all-encompassing toolkit for utilizing LLMs in diverse use-cases, such as chatbots, automated question-answering, text summarization, and beyond.

#### LangChain is composed of 6 modules explained below:
![Langchain explained](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49k2o2rc2cvtgzwfa187.png) **Image credits: [ByteByteGo](https://blog.bytebytego.com/p/how-to-build-a-smart-chatbot-in-10)**
- **Large Language Models**: 
LangChain serves as a standard interface that allows for interactions with a wide range of Large Language Models (LLMs).

- **Prompt Construction**: 
LangChain offers a variety of classes and functions designed to simplify the process of creating and handling prompts.

- **Conversational Memory**: 
LangChain incorporates memory modules that enable the management and alteration of past chat conversations, a key feature for chatbots that need to recall previous interactions.

- **Intelligent Agents**: 
LangChain equips agents with a comprehensive toolkit. These agents can choose which tools to utilize based on user input.

- **Indexes**: 
Indexes in LangChain are methods for organizing documents in a manner that facilitates effective interaction with LLMs.

- **Chains**: 
While using a single LLM may be sufficient for simpler tasks, LangChain provides a standard interface and some commonly used implementations for chaining LLMs together for more complex applications, either among themselves or with other specialized modules.

## How Does LangChain Work?
![how langchain works](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ei0x0uh7z2kjevp6iz47.png)

LangChain is composed of large amounts of data and it breaks down that data into smaller chunks which can be easily be embedded into vector store. Now, with the help of LLMs, we can retrieve the only information that is needed. 

When a user inserts a prompt, LangChain will query the Vector Store for relevant information. When an exact or almost matching information is found, we feed that information to LLM to complete or generate the answer that user is looking for. 

## Get Started with LangChain
Let's use SingleStore's Notebooks feature (it is FREE to use) as our development environment for this tutorial.

![singlestore sql database queries](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxnfpct01cb9pgit0u9z.png)

The SingleStore Notebook extends the capabilities of Jupyter Notebook to enable data professionals to easily work and play around. 

### What is SingleStore?
[SingleStore](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=langchain) is a distributed, in-memory, SQL database management system designed for high-performance, high-velocity applications. It offers real-time analytics and mixes the capabilities of a traditional operational database with that of an analytical database to allow for transactions and analytics to be performed in a single system.

Signup for [SingleStore](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=langchain) to use the Notebooks.

![SingleStore Notebooks feature](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ug1scnnwrz9ar1op5yji.png)

Once you sign up to SingleStore, you will also receive $600 worth free computing resources. So why not use this opportunity.

Click on 'Notebooks' and start with a blank Notebook.
![singlestore notebooks usage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99l9xf5hltflrjbl8qlx.png)

Name it something like 'LangChain-Tutorial' or as per your wish.
![blank notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk3s4wcsgl5tw3alfxa9.png)

Let's start working with our Notebook that we just created.
Follow this step by step guide and keep adding the code shown in each step in your Notebook and execute it. Let's start!

Now, to use Langchain, let’s first install it with the pip command.

```
!pip install -q langchain
```

To work with LangChain, you need integrations with one or more model providers like OpenAI or Hugging Face. In this example, let's leverage OpenAI’s APIs, so let's install it.

```
!pip install -q openai
```

![adding dependencies](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3of4moqy9ecgf27zqpy.png)

Next, we need to setup the environment variable to playaround. 
Let's do that.

```
import os
os.environ["OPENAI_API_KEY"] = "Your-API-Key"
```
![API key added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2drnsrwl8qe8pgyy3vpq.png)

Hope you know how to get Your-API-Key, if not, go to [this link](https://platform.openai.com/account/api-keys) to get your OpenAI API key. 

[Note: Make sure you still have the quota to use your API Key]

Next, let’s get an LLM like OpenAI and predict with this model.

Let's ask our model the top 5 most populated cities in the world.

```
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.7)
text = "what are the 5 most populated cities in the world?"
print(llm(text))
```

![llm prediction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j17vhzo4atqm1s931j4e.png)

As you can see, our model made a prediction and printed the 5 most populated cities in the world.

### Prompt Templates

Let’s first define the prompt template.

```
from langchain.prompts import PromptTemplate

# Creating a prompt
prompt = PromptTemplate(
    input_variables=["input"],
    template="what are the 5 most {input} cities in the world?",
)
```

![prompt template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35127owu4y3j68jt8pov.png)

We created our prompt. To get a prediction, let’s now call the format method and pass it an input.

![get predictions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mp1zxwsjkiw8r3qsklv2.png)

### Creating Chains
So far, we’ve seen how to initialize a LLM model, and how to get a prediction with this model. Now, let’s take a step forward and chain these steps using the LLMChain class.

```
from langchain.chains import LLMChain
# Instancing a LLM model
llm = OpenAI(temperature=0.7)
# Creating a prompt
prompt = PromptTemplate(
  input_variables=["attribute"],
  template= "What is the largest {attribute} in the world?",
)
```

![creating chains](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fs7tf0as9r98qhtbpq9r.png)

You can see the prediction of the model. 

### Developing an Application Using LangChain LLM
Again use SingleStore's Notebooks as the development environment. 

Let's develop a very simple chat application.

Start with a blank Notebook and name it as per your wish.

- First, install the dependencies.

```
pip install langchain openai
```

![langchain using notebooks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsclp2ccrxs0bhe6ghmn.png)

- Next, import the installed dependencies.

```
from langchain import ConversationChain, OpenAI, PromptTemplate, LLMChain

from langchain.memory import ConversationBufferWindowMemory
```

![Import libraries](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1njxnof769m1xp6s510.png)

- Get your OpenAI API key and save it safely.
![safe openai api key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06ghwqt87jekt2bl7ie3.png)

- Add and customize the LLM template

```
# Customize the LLM template 
template = """Assistant is a large language model trained by OpenAI.

{history}
Human: {human_input}
Assistant:"""

prompt = PromptTemplate(input_variables=["history", "human_input"], template=template)
```
![customize LLM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8s56xn38rjwwfpjqhw2.png)

- Load the ChatGPT chain with your API key you saved safely. Add the human input as 'What is SingleStore?'. You can change your input to whatever you want.

```
chatgpt_chain = LLMChain(

           llm=OpenAI(openai_api_key="YOUR-API-KEY",temperature=0),
           prompt=prompt,
           verbose=True,
           memory=ConversationBufferWindowMemory(k=2),

           )
# Predict a sentence using the chatgpt chain
output = chatgpt_chain.predict(
       human_input="What is SingleStore?"
       )
# Display the model's response
print(output)
```
The script initializes the LLM chain using the OpenAI API key and a preset prompt. It then takes user input and shows the resulting output. 

The expected output is as shown below,
![LLM and chat application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmn5vi630g4j50qojnwn.png)

Play with this by changing the human input text/content. 

The complete execution steps in code format is available on [GitHub](https://github.com/pavanbelagatti/langchain-tutorial/tree/main). 

LangChain emerges as an indispensable framework for data engineers and developers striving to build cutting-edge applications powered by large language models. Unlike traditional tools and platforms, LangChain offers a more robust and versatile framework tailored for complex AI applications. LangChain is not just another tool in a developer's arsenal; it's a transformative framework that redefines what is possible in the realm of AI-powered applications.

In the realm of GenAI applications and LLMs, it is highly recommended to know about vector databases. I recently wrote a complete overview on vector databases, you might like to go through that article.

{% embed https://dev.to/pavanbelagatti/wtf-is-a-vector-database-a-beginners-guide-16p %}

**Note**: There is much more to learning LLMs and LangChain and this is my first attempt in writing something about this framework. This is not a complete guide on LangChains. Please go through more articles and tutorials to understand in-depth about LangChain.

Don't forget to [signup for SingleStore](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=langchain) to use the free Notebooks feature. Play around & have fun learning. 

**Disclaimer**: ChatGPT assisted with only some sections of this article.