Build a Simple AI Application with Large Language Model (LLM) using LangChain

Jericho Siahaya
9 min readJun 10, 2023
LangChain

Over the past few months, the AI world has been captivated by the incredible rise of Large Language Models (LLMs). With the explosive growth from the companies such as OpenAI (ChatGPT), Microsoft (Bing Chat), and Google (Bard). There is no doubt this powerful AI model becoming so popular and has opened up new possibilities for natural language processing applications, enabling developers to create more sophisticated, human-like interactions in chatbots, question-answering systems, summarization tools, and beyond.

Before we go deep into building the LLM-based AI application, we need to understand about LLM first.

What is Large Language Model?

Large Language Model (LLM) refer to an advanced artificial intelligence model that is trained on vast amounts of text data to understand and generate human-like language. This model, such as OpenAI’s GPT-4 (Generative Pre-trained Transformer 4), is built using deep learning techniques and are capable of performing a wide range of natural language processing tasks.

LLM is trained on diverse text sources like books, articles, websites, and more, allowing them to acquire a broad knowledge base and linguistic patterns. By leveraging this vast knowledge, LLM can generate coherent and contextually relevant text, answer questions, translate languages, perform sentiment analysis, and even engage in conversations.

So, in a simple ELI5 way, you can imagine you have a super-smart friend who knows everything about words and language. They’ve read tons of books and articles and can understand and talk like a human. That’s basically what a Large Language Model is — a super-smart AI that has been trained on a massive amount of text data.

Photo by Glen Noble on Unsplash

It can understand what you say or write and respond in a way that sounds just like a real person. It’s like having a language expert at your fingertips, ready to help you with all sorts of language-related tasks, from answering questions to writing stories. LLMs are changing the way we interact with computers and making our conversations with AI feel more natural and human-like.

LangChain as LLM framework

LangChain official website

LangChain is a framework for developing applications powered by language models. The LangChain framework is designed around these principles.

  1. Data-aware: connect a language model to other sources of data.
  2. Agentic: allow a language model to interact with its environment.

Inside the framework, it has some modules. Modules are the core abstractions which we view as the building blocks of any LLM-powered application. For each module LangChain provides standard, extendable interfaces. LangChain also provides external integrations and even end-to-end implementations for off-the-shelf use. The modules are:

  1. Models: LangChain offers support for various model types and model integrations. It enables you to easily integrate and work with different language models, enhancing your applications’ capabilities.
  2. Prompts: LangChain allows you to manage, optimize, and serialize prompts efficiently. This helps in generating more accurate and contextually relevant responses from the language models.
  3. Memory: LangChain provides a standard interface for memory and a collection of memory implementations. It facilitates the persistence of state between calls in a chain or agent, enhancing the model’s knowledge and recall abilities.
  4. Indexes: To boost the power of language models, LangChain helps you effectively combine them with your own text data. It provides best practices for indexing and searching through your data sources.
  5. Chains: Chains are sequences of calls, either to language models or other utilities. LangChain offers a standard interface for chains, along with numerous integrations and end-to-end chains for common applications.
  6. Agents: Agents enable language models to make decisions, take actions, observe outcomes, and repeat the process until the objective is met. LangChain provides a standard interface for agents, a selection of agents to choose from, and examples of end-to-end agents.
  7. Callbacks: Callbacks let you log and stream the intermediate steps of any chain, making it easy to observe, debug, and evaluate the internals of an application.

Best practices and built-in implementations for common LangChain use cases:

  1. Autonomous Agents: LangChain supports the development of autonomous agents like AutoGPT and BabyAGI, which are long-running agents that perform multiple steps to achieve an objective.
  2. Agent Simulations: LangChain facilitates the creation of sandbox environments where agents can interact with each other or react to events, offering insights into their long-term memory abilities.
  3. Personal Assistants: LangChain is ideal for building personal assistants that can take actions, remember interactions, and have access to your data, providing personalized assistance.
  4. Question Answering: LangChain excels in answering questions over specific documents, utilizing the information within those documents to construct accurate and relevant answers.
  5. Chatbots: Leveraging the text generation capabilities of language models, LangChain empowers the creation of engaging chatbots.
  6. Querying Tabular Data: LangChain provides guidance on using language models to query data stored in tabular formats, such as CSV files, SQL databases, or dataframes.
  7. Code Understanding: LangChain assists in using language models to query and comprehend source code from platforms like GitHub.
  8. Interacting with APIs: LangChain enables language models to interact with APIs, providing them with up-to-date information and the ability to take actions based on real-time data.
  9. Extraction: LangChain helps extract structured information from unstructured text, streamlining data analysis and interpretation.
  10. Summarization: LangChain supports the summarization of longer documents into concise, easily digestible chunks of information, making it a powerful tool for data augmentation.
  11. Evaluation: As generative models are challenging to evaluate using traditional metrics, LangChain offers prompts and chains to aid in the evaluation process using language models themselves.

For more detailed explanation, you can take a look on the project’s documentation here: https://python.langchain.com/en/latest/.

Let’s cook some code!

Since, LangChain is framework so it’s not really difficult to get started with the code. We are going to cook the code using Python language which is the most popular programming language for both machine learning and deep learning development.

To get started, install LangChain with the following command:

pip install langchain
pip install openai

Yes, we will use OpenAI’s API so we need to install their SDK too. Then, like everybody does, we need to setup the OpenAI key, so you need to gather one from here: https://platform.openai.com/account/api-keys.

import os
os.environ['OPENAI_API_KEY'] = 'your key'

This step is quite important since most of LangChain modules, will need OpenAI API key to be setup in your environment, so you don’t need to pass the API key anymore when you want to use one of the modules. After that, we will import the LLMs, because we want more randomness, so we set the temperature to 0.9.

from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9)

I’m just going to give a simple input to the model:

print(llm("give me a dad joke"))

Result:

Q: What did the fish say when it swam into a wall?
A: Dam!

Pretty simple, and funny, right? 😅 (pardon my humor)

Now that you have a solid knowledge about the basic of this framework, then we will go deeper to explore another use case of this framework.

Chains

As the name of the framework include this word. Chains are a series of reusable components that can be linked together to perform a specific task. LangChain provides a standard interface for Chains, as well as some common implementations of chains for ease of use.

There are few types of chains in LangChain, such as LLM Chain, Router Chains, Sequential Chains, Transformation Chain, Analyze Document Chain, and more. In this article, we will try to use Q&A Chain to build a simple prompt that takes text-based data (article/documentation) and make a Q&A application.

Import all the necessary packages:

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.docstore.document import Document
from langchain.prompts import PromptTemplate
from langchain.indexes.vectorstore import VectorstoreIndexCreator
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI

Because we want to make Q&A application based on text-based data, then I will use this article: https://www.kompas.id/baca/english/2023/06/08/en-prediksi-kiamat-baru-semua-benda-di-alam-semesta-berakhir-dengan-menguap, from Harian Kompas as my dataset.

Kompas.id

We need to extract the whole article in a single txt file, and load into out Python program:

with open("/content/article.txt") as f:
aticle_kompas = f.read()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_text(article_kompas)

embeddings = OpenAIEmbeddings()

Using Chroma, we will vectorize the dataset.

Chroma is a vector database for building AI applications with embeddings. There exists a wrapper around Chroma vector databases, allowing you to use it as a vectorstore, whether for semantic search or example selection.

docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()

Now that we prepared the data, then we can query into our article. Let’s try using simple query, since the article talk about black holes and space-related object, my query would be like this:

query = "What is Space Time?"

Query the data using the QA Chain:

docs = docsearch.get_relevant_documents(query)
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
chain({"input_documents": docs, "question": query}, return_only_outputs=True)

Result:

{'output_text': ' Space-time is a concept in physics that combines the three dimensions of space and the one dimension of time into a single four-dimensional continuum. It is used to describe the behavior of matter and energy in the universe.'}

Well answered, right? How about we modify the prompt and turn the answer into Indonesian.

prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.

{context}

Question: {question}
Answer in Indonesian:"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff", prompt=PROMPT)
chain({"input_documents": docs, "question": query}, return_only_outputs=True)

Result:

{'output_text': ' Ruang-waktu adalah konsep yang menggabungkan ruang dan waktu menjadi satu konsep yang terintegrasi. Menurut teori relativitas umum Einstein, ruang-waktu akan dipengaruhi oleh gaya gravitasi, sehingga menyebabkan ruang-waktu menjadi melengkung.'}

Using the same query, we got the result in both English and Indonesian.

For the last cook, I want to try the summarization model in this framework. We will use AnalyzeDocumentChain for this one. The AnalyzeDocumentChain is more of an end to chain. This chain takes in a single document, splits it up, and then runs it through a CombineDocumentsChain. This can be used as more of an end-to-end chain.

from langchain import OpenAI
from langchain.chains.summarize import load_summarize_chain

prompt_template = """Write a concise summary of the following:


{text}


CONCISE SUMMARY IN INDONESIAN:"""
PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])

llm = OpenAI(temperature=0)
summary_chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=PROMPT, combine_prompt=PROMPT)

Using the same article, we prepared the model and prompt. In this case, I will try to summarize the article using Indonesian language. Then run the model with dataset:

from langchain.chains import AnalyzeDocumentChain
summarize_document_chain = AnalyzeDocumentChain(combine_docs_chain=summary_chain)
summarize_document_chain.run(article_kompas)

Result:


Stephen Hawking menyatakan pada tahun 1974 bahwa lubang hitam akan menghilang melalui proses radiasi Hawking. Namun, sebuah studi baru menunjukkan bahwa semua objek besar di alam semesta juga akan menguap melalui proses yang sama. Ini akan mengubah pandangan kita tentang alam semesta dan masa depannya. Fisikawan masih perlu menemukan bukti radiasi Hawking yang diproduksi oleh objek-objek bermasa besar untuk memverifikasi prediksi tentang akhir bintang-bintang besar di alam semesta.

The full code of this project is available here: machine-learning/LangChain — Kompas Article.ipynb at main · jerichosiahaya/machine-learning · GitHub

With these advanced techniques and handle the prompts smartly, LangChain can do amazing things in our projects. It can help answer questions by looking up information in databases and even come up with creative solutions to problems. The possibilities with LangChain are endless and can bring a whole new level of capability to our work.

For a comprehensive guide on LangChain, please refer to its official documentation, which provides valuable information on installing, setting up your environment, and working with this useful Python framework. The documentation covers various components, like different supported models for machine learning, memory management, and more.

--

--