[Course Notes] LangChain for LLM Application Development: Part 5

Chanan
3 min readMar 7, 2024

--

LangChain for LLM Application Development: Question and Answer

LangChain for LLM Application Development by DeepLearning.AI

Table of Contents

  1. Introduction
  2. Models, Prompts, and Parsers
  3. Memory
  4. Chains
  5. Question and Answer (This part)
  6. Evaluation
  7. Agents

Question and Answer

Example: Q&A over Documents

Due to the potentially large volume of words in documents, exceeding the capacity of the Large Language Model (LLM), we won’t directly feed all the words into the LLM.

Instead, we will segment the user input query into smaller chunks and then convert each chunk into embedding vectors. Subsequently, all these embedding vectors will be stored in the Vector Database for efficient retrieval and processing.

Upon receiving a user query, our system retrieves the most relevant vector from the Vector Database. This vector is then processed with the Large Language Model (LLM) to generate the desired output or response.

The Stuff method stands out not only for its simplicity but also for its cost-effectiveness compared to other methods like Map Reduce, Refine, and Map Rerank. This affordability, coupled with its effectiveness, renders the Stuff method the most commonly used approach in Large Language Models (LLMs).

The Map Reduce method emerges as the second most prevalent technique. These methods aren’t exclusive to Question Answering tasks; they can also be applied to various other tasks. For instance, the Map Reduce chain is commonly employed in summarization tasks. This involves recursively summarizing information from lengthy documents, showcasing the versatility of these methods beyond Question Answering.

from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import CSVLoader
from langchain.vectorstores import DocArrayInMemorySearch
from IPython.display import display, Markdown
from langchain.llms import OpenAI
from langchain.indexes import VectorstoreIndexCreator
from langchain.embeddings import OpenAIEmbeddings
file = 'OutdoorClothingCatalog_1000.csv'
loader = CSVLoader(file_path=file)
index = VectorstoreIndexCreator(
vectorstore_cls=DocArrayInMemorySearch
).from_loaders([loader])
query ="Please list all your shirts with sun protection \
in a table in markdown and summarize each one."
llm_replacement_model = OpenAI(temperature=0,
model='gpt-3.5-turbo-instruct')
response = index.query(query,
llm = llm_replacement_model)
# Embedding Part
loader = CSVLoader(file_path=file)
docs = loader.load()
embeddings = OpenAIEmbeddings()
embed = embeddings.embed_query("Hi my name is Harrison")
db = DocArrayInMemorySearch.from_documents(
docs,
embeddings
)
# Retrieving Part
query = "Please suggest a shirt with sunblocking"
docs = db.similarity_search(query)
retriever = db.as_retriever()
llm = ChatOpenAI(temperature = 0.0, model=llm_model)
qdocs = "".join([docs[i].page_content for i in range(len(docs))])
response = llm.call_as_llm(f"{qdocs} Question: Please list all your \
shirts with sun protection in a table in markdown and summarize each one.")
qa_stuff = RetrievalQA.from_chain_type( # stuff all the document into a context
llm=llm,
chain_type="stuff",
retriever=retriever,
verbose=True
)
query = "Please list all your shirts with sun protection in a table \
in markdown and summarize each one."
response = qa_stuff.run(query)
display(Markdown(response))
# Generation (LLM) Part
response = index.query(query, llm=llm)
index = VectorstoreIndexCreator(
vectorstore_cls=DocArrayInMemorySearch,
embedding=embeddings,
).from_loaders([loader])

That wraps up our exploration of implementing Question Answering systems using LangChain! In the next part, we’ll delve into Evaluation, where we’ll assess the performance and effectiveness of our Question Answering system built with LangChain.

Stay tuned!

--

--

Chanan

Book and Coffee enthusiast | Data Scientist | Python, NLP, AI