Search icon CANCEL
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Learning Hub
Free Learning
Arrow right icon
LangChain in your Pocket
LangChain in your Pocket

LangChain in your Pocket: LangChain Essentials: From Basic Concepts to Advanced Applications

By Mehul Gupta
€19.99 €13.98
Book May 2024 152 pages 1st Edition
€19.99 €13.98
€14.99 Monthly
€19.99 €13.98
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now
Table of content icon View table of contents Preview book icon Preview Book

LangChain in your Pocket

Chapter 1: Introduction

2023 has been the year of GenAI. Since the inception of ChatGPT in late 2022, there has been no turning back. One after the other new things are coming up: different LLMs like GPT, LLaMA, PaLM, etc; frameworks like LangChain and Llama-Index; AI agents like AutoGPT, BabyAGI, etc. This book is a deep dive into the most famous and useful framework around LLMs, i.e., LangChain, an open-source project that debuted in October 2022, pioneered by Harrison Chase and initially developed while Chase was at Robust Intelligence, an MLOps company.


But before we jump onto LangChain, we must understand LLMs.

1.1 What are LLMs?

Large Language Models (LLMs) are nothing but Machine Learning models that have a human-like understanding of language. Because of this ability, any random task, be it Summary generation, Language Translation, Classification, etc. can be done using a single model. These advanced algorithms utilise deep learning techniques and vast datasets to comprehend and generate human-like language. The term "large" emphasizes the extensive training these models undergo on massive datasets, enabling them to capture intricate patterns and entity relationships within the text and their usual humongous size. Let’s very quickly understand the salient features of a general LLM:

  1. An LLM, based on Neural Networks, predicts subsequent tokens from a given sequence of text and already predicted tokens. There is no black box!
  2. To understand the context, LLMs utilise multi-head attention (discussed in the paper ‘Attention is all you need’) or its variants, focussing on vital words within the input sequence. It would require another book to discuss the concept of Attention so skipping it for now.
  3. As an LLM predicts tokens sequentially, it never knows the final output at any given moment unless the final output is completely predicted.
  4. Zero-shot abilities allow LLMs to tackle diverse problems, not confined to specific tasks like classification or regression.


What is Zero-shot learning?

Zero-shot learning refers to a capability within ML models like LLMs to perform tasks or generate outputs for which they haven't been explicitly trained. In essence, these models can generalize their knowledge to tackle unseen or new tasks without specific examples or data during their training phase. This ability comes from the model's extensive training on huge corpora where these models can learn varied linguistic patterns and contexts, enabling it to infer and generalize information to new scenarios.


  1. Typically massive in size and trained on vast datasets, LLMs adhere to the Transformers architecture or a subset of it like GPT (decoder part of a Transformer).
  2. Pre-trained LLMs, akin to 'Generalists,' are well-suited for various tasks but not exceptional in any specific area. So if you wish to have a 99% accuracy on a classification task using a pre-trained LLM, this is a little difficult.
  3. For specialized tasks, 'Fine-Tuning' involves training a pre-trained LLM with specific data.
  4. LLMs vary in size denoted by terms like small, large, or XXL, influenced by parameters and architectural modifications. The size usually denotes the number of weight parameters being used.
  5. The numeric identifier in some LLM names, such as LLaMA-70B, signifies the number of parameters. Such LLM families don’t use the small, large, XL nomenclature.
  6. Details regarding training data or architecture for LLMs are often not fully disclosed by their respective owner companies, creating gaps in understanding certain LLM families.
  7. Due to their massive size, loading LLMs into memory can be a challenging task. This becomes a bottleneck when you don’t have huge resources. This is where APIs come into the picture where you can access models as big as hundreds of GBs with a url without loading it in your memory. We will be using OpenAI API for most of the tutorials in this book.
  8. LLMs, being generative models, introduce an element of randomness, resulting in varying outputs for the same input. This is something you all must have observed while using ChatGPT.
  9. To perform any task, we need to provide the LLM a prompt i.e. a natural language sentence specifying the task's details.
  10. Every LLM has a token restriction (word limit) for input and output.


As of now (2023 end), many organizations have developed a variety of LLMs, each one of them bringing something new to the table. Let’s have a brief about different types of LLM families.

1.2 Different LLM families

The GPTs

This is where the GenAI era kickstarted. GPT stands for Generative Pre-trained Transformers. ChatGPT stems from this family of models and one of its versions, GPT3.5, serves as its foundation.

  1. GPTs, including GPT3.5, are built upon the decoder component of a Transformer architecture. This design gives them the ability to generate content on their own.
  2. When comparing models, their parameter counts play a significant role. For instance, GPT3.5 boasts 175 billion parameters, while its successor, GPT4, amasses a whopping 1,760 billion parameters.
  3. GPT3.5 Turbo represents an upgraded and more finely-tuned iteration of GPT3.5. With around 20 billion parameters tailored specifically for natural language processing tasks, it enhances performance and optimization.
  4. Among these models, only GPT4 possesses the multi-modal feature, allowing it to process and understand various types of inputs, such as images, audio and more.


LLaMA by Meta

LLaMA, standing for Large Language Model Meta AI, is a family of LLMs developed and released by Meta AI, the parent company of Facebook following the Transformer architecture. Introduced in February 2023, LLaMA is designed to be an open-source and efficient foundation for language models, ranging from 7 billion to 65 billion parameters. The first version of LLaMA included multiple model sizes and Meta AI has continued to evolve this family of models. A special effort has been put into making it more safe and secure to use.

FLAN models

FLAN-T5 and FLAN-Alpaca are LLMs introduced as instruction-tuned language models. These models leverage synthetic instruction tuning, a method that fine-tunes a base language model with instructions generated by humans and machines.

What is Instruction-based Fine-Tuning?

During instruction fine-tuning, you're required to include specific guidelines, termed 'instructions,' along with the input and output data while training the model. Here's an example illustrating the process of instruction fine-tuning for an LLM: General fine-tuning sample:

Input: 'Compose a narrative involving the theme of nature'

Output: '........'


Input: 'Identify elements in this passage: the house is blue'

Output: '......'


Instruction fine-tuning adaptation:

Instruct: 'Generate a story based on the provided subject'

Input: 'Nature and wildlife'

Output: '........'


Instruct: 'Recognize details within the given text'

Input: 'The house is painted blue'

Output: '.......'


Below is a quick brief on two major FLAN models:

  1. FLAN-T5: FLAN-T5 is an enhanced version of T5 (Text-to-Text Transfer Transformer) that has been fine-tuned using a mixture of tasks, as detailed in the paper "Scaling Instruction-Finetuned Language Models”.
  2. FLAN-Alpaca: FLAN-Alpaca extends the concept of instruction tuning and is a fine-tuned version of LLaMA.


Mistral AI emerged as a noteworthy startup, gaining attention with their inaugural model, Mistral-7B, which outperformed established competitors despite having just 7 billion parameters.

  1. Architecture-wise, Mistral models mirror GPT, relying on the Transformer's Decoder component.
  2. Parameters vary across Mistral and Mixtral, with Mistral having 7B parameters and Mixtral featuring 84B. Mixtral is a Mixture of Expert (MoE) model, which involves a combination of multiple specialized networks for doing specific sub-tasks in a given problem.

Phi by Microsoft

Phi, developed by Microsoft, prioritizes delivering strong performance while maintaining a notably smaller model size, which makes it more suitable for practical use in production environments.

  1. In terms of structure, Phi models also adopt the Transformer model as their foundation.
  2. When considering parameters, Phi models vary in size: Phi-1 and Phi-1.5 hover around 1.3 billion parameters, while Phi-2 boasts 2.7 billion.
  3. Interestingly, despite its smaller size, Phi-2 has outperformed models nearly ten times larger, such as LLaMA-13B and has even surpassed ChatGPT in certain tasks.

This list of language models extends far and wide. While I couldn't cover all, notable models besides those mentioned include Claude (with approximately 130 billion parameters, yielding similar results to GPT3.5), Cohere, PaLM, T5 (developed by Google), Falcon and more. Exploring these models can offer further insights into their capabilities.


Now that we have known enough about LLMs, time to go back to LangChain.

1.3 What is LangChain used for?

A simple answer: Simplifying any task you wish to accomplish using LLMs.


LangChain is designed to simplify the creation of applications using LLMs. Be it a grammar correction app or a complex NER extraction, LangChain has got you covered.

  1. Advanced Chat Applications: LangChain enables the development of chat applications capable of handling complex questions and transactions from users.
  2. Composable Tools and Integrations: LangChain offers composable 3rd party tools and integrations like Google Search, ArXiv, Bing Search, Wikipedia, etc. making it easy for developers to work with language models.
  3. Short-Term and Long-Term Memory Support: LangChain supports short-term and long-term memory, allowing LLMs to retain information across interactions, which is particularly useful in chatbot scenarios.
  4. Support to most LLMs: LangChain provides support to most of the LLMs using API or even local LLMs that can be loaded using Hugging Face or fine-tuned by you.
  5. Evaluation: Not just building apps, it does provide functionality to evaluate the results of your LLMs by using either pre-defined metrics or custom metrics. This is crucial because evaluating a generative model, be it GANs or LLMs, is always a challenge.
  6. Complex applications: LangChain can be used for building solutions for real complex problems like recommendation systems, AI agents like AutoGPT, etc.

1.4 Why LangChain?

To answer this, we will take a step back and understand what contributes to the success of a task when dealing with GenAI and LLMs:


  1. The LLM you’re using.
  2. The prompt you feed.
  3. The integration of external tools/wrappers you wish to use (say Google search).

LangChain, in the hood, takes care of all three aspects together, especially the prompt and coding part. So assume you wish to create an Automatic SQL generator using LLMs with the following features:

  1. The app should connect and traverse through tables and DBs.
  2. Generate queries given a prompt.

Now, to do this manually, you need a lot of effort from writing codes to connect with databases, fetching data, getting an apt prompt and whatnot. Now using LangChain, this is a couple of lines of code as everything gets handled in the hood by LangChain.

It is possible to create any app without LangChain also, but LangChain simplifies it to a great extent and hence way better than manual prompting.

I have even used other frameworks available open-source like Llama-Index or Promptify but the range that LangChain has got is just amazing. I can’t think of an app I wish to create which I can’t do using LangChain. Others do have great utilities but are very specific like Llama-Index is a specialist with the RAG framework (discussed in upcoming chapters) but doesn’t have a range as big as LangChain. Hence, LangChain should be your go-to framework any day.

1.5 Book Overview

In this book, we will be going through a lot of concepts starting with:


Hello World

This chapter deals with setting up LangChain in your system and building a series of beginner-friendly LangChain apps using the OpenAI API key or local LLMs.


Different LangChain Modules

Once done with the basic setup in the above chapter, this chapter will introduce different components of the LangChain Framework like Models, PromptTemplate, Chains, Agents, Callbacks, Retrieval and Memory and what role these modules play in creating an application.


Model and Prompts

This chapter explains the Model and Prompts module from LangChain that enables different models and provides numerous templates for building any LangChain app.



The heart of LangChain, chains can be considered as short programs that are written around LLMs to provide a one-line solution for common problems like Summarization, NER, etc.



More flexible and versatile compared to chains, agents help you build complex solutions and enable access to 3rd party tools like Google search, file writing, etc. with ease.


OutputParsers and Memory

The OutputParsers enable formatting the output from LLMs making them more useful for production-grade applications and avoiding ambiguous formats. The memory module enables memory for LangChain apps, hence remembering past conversations in the current session similar to ChatGPT.



Similar to Tensorflow callbacks, it enables a deep dive into the internal workings of LangChain apps making logging and debugging easy.


RAG Framework and Vector Databases

One of the key features of LangChain is its support for RAG i.e. providing external context to LLMs as text files, PDFs, etc. Check out how to implement RAG using Vector Databases and LangChain in this chapter.


LangChain for NLP

Most important of all, this chapter deals with how LLMs and LangChain can be used for solving various NLP problems, be it NER, Text tagging, Sentiment analysis, Few-shot classification, etc.


Handling LLM Hallucinations

Since the inception of LLMs, hallucinations i.e. LLMs giving factually wrong answers have been a problem. LangChain provides a solution to that too which you can check out in this chapter.


Evaluating LLMs

Unless you can measure metrics around your ML problem, you can’t evaluate how good is the solution. Measuring metrics for LLMs is a tough task but LangChain provides an answer to this as well. Check out this section on how to calculate different metrics for LLMs.


Advanced Prompt Engineering

LangChain supports Prompt Engineering Frameworks like ReAct, Trees of Thoughts, etc. that can be explored in this chapter alongside examples.


Autonomous AI agents

After LLMs, the next big thing coming up is AI agents like AutoGPT, BabyAGI, etc. Check out how LangChain enables these AI agents in this book.


LangSmith and LangServe

Before ending, this chapter introduces two major extensions to LangChain that are LangSmith which enables detailed debugging, logging and visualization while LangServe supports deploying a LangChain app.


Additional Features

This chapter covers some other functionalities that LangChain provides like Fallbacks, Safety and Security chains, etc.

So, let’s embark on this exciting journey. I hope you enjoy this book. All the codes used in this book can be found here :

Left arrow icon Right arrow icon

Key benefits

  • Step-by-step code explanations with expected outputs for each solution
  • Practical examples and hands-on tutorials for real-world application
  • Detailed discussions on managing and evaluating large language models


"LangChain in your Pocket" offers a detailed exploration into the LangChain framework, designed to enhance your skills in developing sophisticated language understanding models and applications. This book begins with the basics, introducing you to the fundamental concepts of LangChain through a simple "Hello World" example. As you progress, you'll delve into various LangChain modules, learning how to create agents, manage memory, and utilize output parsers effectively. The journey continues as you explore the RAG Framework, vector databases, and their applications in natural language processing, providing you with the tools to tackle common NLP problems efficiently. The book also addresses critical aspects of working with large language models (LLMs), such as prompt engineering, handling hallucinations, and evaluating model outputs. Advanced topics like autonomous AI agents and the integration of LangSmith and LangServe are covered, giving you a holistic view of what you can achieve with LangChain. By the end of this book, you will not only understand the technical aspects of LangChain but also how to apply these principles in real-world scenarios, making it an essential resource for anyone looking to advance their capabilities in AI and language processing.

What you will learn

Navigate the basic to advanced features of LangChain Build and manage language understanding models and applications Employ advanced prompt engineering techniques Implement and evaluate large language models effectively Develop autonomous AI agents with LangChain Integrate LangSmith and LangServe for enhanced functionality

Product Details

Country selected

Publication date : May 15, 2024
Length 152 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781836201250
Category :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details

Publication date : May 15, 2024
Length 152 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781836201250
Category :
Tools :

Table of Contents

19 Chapters
Preface Chevron down icon Chevron up icon
1. Chapter 1: Introduction Chevron down icon Chevron up icon
2. Chapter 2: Hello World Chevron down icon Chevron up icon
3. Chapter 3: Different LangChain Modules Chevron down icon Chevron up icon
4. Chapter 4: Models and Prompts Chevron down icon Chevron up icon
5. Chapter 5: Chains Chevron down icon Chevron up icon
6. Chapter 6: Agents Chevron down icon Chevron up icon
7. Chapter 7: OutputParsers and Memory Chevron down icon Chevron up icon
8. Chapter 8: Callbacks Chevron down icon Chevron up icon
9. Chapter 9: RAG Framework and Vector Databases Chevron down icon Chevron up icon
10. Chapter 10: LangChain for NLP problems Chevron down icon Chevron up icon
11. Chapter 11: Handling LLM Hallucinations Chevron down icon Chevron up icon
12. Chapter 12: Evaluating LLMs Chevron down icon Chevron up icon
13. Chapter 13: Advanced Prompt Engineering Chevron down icon Chevron up icon
14. Chapter 14: Autonomous AI agents Chevron down icon Chevron up icon
15. Chapter 15: LangSmith and LangServe Chevron down icon Chevron up icon
16. Chapter 16: Additional Features Chevron down icon Chevron up icon
17. Endnotes Chevron down icon Chevron up icon
18. About the Author Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Top Reviews
No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial


How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to
  • To contact us directly if a problem is not resolved, use
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.