Customize Large Language Models using LangChain β€” Part-1

In this article series we will see how we can customise and develop applications using LangChain by taking advantage of LLM in its background. We will also see how we can build the custom Q&A systems from custom dataset from scratch and with low code, no code strategy. so, stay tuned to this space on this exiting article series.

credits: Intenet

Introduction:

LangChain is a software development framework designed to simplify the creation of applications using large language models (LLMs). It provides a standard interface for interacting with LLMs, as well as a variety of tools and libraries that can be used to build applications. LangChain can be used to build a wide variety of applications, including:

  • Chatbots
  • Question-answering systems
  • Text summarization systems
  • Natural language generation systems

LangChain has below mentioned modules

different modules available in LangChain, credits: author

The explanation of the six main concepts in LangChain:

  • Models: Models are the core of LangChain. They are large language models (LLMs) that have been trained on massive datasets of text and code. Models can be used to perform a wide variety of tasks, such as generating text, translating languages, and answering questions.
  • Prompts: Prompts are used to guide models to generate specific outputs. Prompts can be simple or complex, and they can be used to generate a wide variety of creative text formats, like poems, code, scripts, musical pieces, email, letters, etc.
  • Memory: Memory is used to store data that can be accessed by models and agents. Memory can be used to store things like the results of previous computations, the state of the world, or the user’s preferences.
  • Indexes: Indexes are used to speed up the access of data. Indexes can be created for any type of data, including text, code, and images.
  • Chains: Chains are sequences of steps that are used to perform a task. Chains can be used to generate text, translate languages, answer questions, and perform other tasks.
  • Agents: Agents are used to control the execution of chains. Agents can be used to make decisions, track progress, and handle errors.

With this basic understanding and with no further delay let us head towards coding our own question answer system on our custom dataset.

Installation:

pip install langchain openai

#if langchain & openai are already installed

pip install --upgrade langchain openai

Required Libraries:

from langchain.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI

from dotenv import load_dotenv, find_dotenv
import openai
import os

use your open_ai key for fetching the document embeddings

_ = load_dotenv(find_dotenv()) # read the local .env file
openai.api_key = os.getenv('OPENAI_API_KEY')

load all the documents from the local directory as per the path given

def load_document(path):
dir_loader = DirectoryLoader(path=path)
docs = dir_loader.load()
return docs

docs = load_document('<YOUR_DOCS_PATH>')
print(len(docs))

split the text in the documents to create the vector embeddings out of it.

def split_text(docs, chunk_size=500, chunk_overlap=10):
splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
documents = splitter.split_documents(documents=docs)
return documents

documents = split_text(docs)
len(documents)

use OpenAIEmbeddings to get the embeddings and create our own embedding on top of that and store it in a vectorstore.

embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')
db = Chroma.from_documents(docs, embeddings)

query = "<YOUR_QUERY>"
docs = db.similarity_search(query)

we can now find the similarity with score and without score of the documents with the query that we passed. this methodology can be used for semantic document search.

def find_similiar_docs(query, k=3, score=True):
if score:
similar_docs = db.similarity_search_with_score(query, k=k)
else:
similar_docs = db.similarity_search(query, k=k)
return similar_docs

finally we can use the OpenAI LLM to build a Q&A system as below

# model = "text-davinci-003"
model = "gpt-3.5-turbo"
# model = "gpt-4"
llm = OpenAI(model_name=model)

chain = load_qa_chain(llm, chain_type="stuff")

def get_answer(query):
similar_docs = find_similiar_docs(query, k=1, score=False)
answer = chain.run(input_documents=similar_docs, question=query)
return answer

query = "What is the method discussed for predicting power consumption ?"
answer = get_answer(query)
answer

Now the system is fully trained on the documents you provided and will be able to answer the question that are passed to it. we can use more concepts like ConversationBufferMemory & ConversationChain to make our own conversational bots which will remember the historical conversation context and refine the answers based on that.

Conclusion:

In conclusion, LangChain is a powerful framework that can be used to build a wide variety of applications using large language models (LLMs). It is easy to use, flexible, and scalable, making it a great choice for developers of all levels of experience. If you are looking to build an application that can take advantage of the latest advances in natural language processing, then LangChain is a great option to consider. Please stay tuned for my next article where we see the similar architecture of training a chatbot to answer on your own documents but with β€œno code”.

please clap and follow if you like the content of the articles. Happy learning.

--

--