VTeam AI

Beginner Tutorials

Beginners guide for LangChain

LangChain, developed by Harrison Chase, is an open-source framework designed to simplify application development using Large Language Models (LLMs). It introduces 'Chains', sequences of modular units, that transform and interpret user input for LLMs. Compatible with major LLM providers, LangChain paves the way for versatile applications, from chatbots to translations.

LLMs have taken the world by storm. The impact is huge and given the zero-shot capabilities i.e. it can perform any task without prior training, they are capable of replacing almost anything, affecting the IT industry with immediate effect. 

But when we say LLMs can perform any task, things aren’t that straightforward some months back. Writing long, structured prompts, and fine-tuning for a specific task is not everyone’s cup of tea and non-AI guys want to have a more straightforward solution.

Divider

LangChain enters the room…

So what is this LangChain that is the talk of the town for some time now?

LangChain is a framework designed to simplify the creation of applications using large language models (LLMs). It provides developers with tools, components, and interfaces that streamline the process of building end-to-end applications powered by LLMs and chat models. LangChain was created by Harrison Chase and is an open-source project available on GitHub.

The core concept of LangChain revolves around the idea of creating Chains, which are logical links between one or more LLMs. These Chains can range from simple to advanced, allowing developers to combine different components and models to accomplish specific tasks. The framework offers several functionalities to aid in this process:

  1. Components and Chains: LangChain employs modular building blocks known as components that can be combined to create powerful applications. Chains are sequences of these components (or other chains) working together to perform a specific task. For example, a chain could involve a prompt template, a language model, and an output parser collaborating to process user input and generate responses.
  2. Prompt Templates and Values: Prompt templates are used to create prompt values, which are input data for the language model. These templates help convert user input and other dynamic information into a format suitable for the language model. PromptValues are classes with methods that can be converted into the input types expected by different LLMs.
  3. Example Selectors: Example Selectors are utilized to dynamically include examples in prompts. They take user input and return a list of examples to incorporate into prompts, making them contextually relevant and powerful.
  4. Output Parsers: Output Parsers structure language model responses into more usable formats. They help transform raw outputs into organized and meaningful results that can be presented to users or processed further.

LangChain's capabilities are not limited to a single model or application type. It provides integrations with various LLM providers like OpenAI, Hugging Face, Cohere, and more. The framework's versatility allows developers to create applications such as chatbots, generative question-answering, summarization, translation, and other tasks leveraging the strengths of different LLMs and components.

Divider

So why should I care about LangChain, it looks to be some other Python package. No?

Trust us, you should. LangChain makes your life damn easy if it involves interacting with LLms for anything or building anything related to ML or AI. The significance of LangChain lies in its role as a powerful and innovative framework that simplifies the development of applications utilizing large language models LLMs and natural language processing NLP technology.

  1. Simplifying LLM-Powered Applications: LangChain provides developers with a streamlined way to create applications powered by language models. It offers modular abstractions and implementations for various components needed to work with language models, including models, prompts, memory, indexes, chains, and agents. This simplification accelerates the development process and reduces the complexity associated with building applications that leverage LLMs.
  2. Integration of LLMs with Data Sources: LangChain allows developers to connect language models with diverse data sources. It enables applications to interact with their environment, making them more data-aware and agentic. This integration opens up possibilities for creating dynamic and responsive applications that can leverage data from various sources for improved performance and user experiences.
  3. Enhancing Smart Contracts: LangChain introduces a novel dimension to smart contracts by incorporating NLP capabilities. Smart contracts, which are self-executing agreements with predefined rules, gain the ability to understand and process human language. This integration makes smart contracts more versatile and user-friendly, enabling them to execute actions based on language-based conditions, such as sentiment analysis of news articles.
  4. Decentralized and Private Language Data Handling: Traditional language processing often involves sharing sensitive textual data with centralized platforms, raising concerns about privacy and security. LangChain employs blockchain's decentralized architecture to enhance the security and privacy of language data. Data can be stored, processed, and utilized within a secure and decentralized environment, minimizing the risks associated with data breaches and unauthorized access.
  5. Multi-Model Language Learning Solutions: LangChain's Chains are logical connections between multiple LLMs, offering the potential to combine the strengths of different models. This facilitates the execution of more complex and sophisticated tasks, allowing developers to create innovative solutions to intricate problems. LangChain Chains empower users to link various LLMs, enhancing the capabilities of traditional single-model arrangements.
  6. Impact in Various Industries: LangChain's capabilities extend beyond the realm of language processing. It has implications in fields such as blockchain, finance, and beyond. Its potential to handle data securely, enhance smart contracts, and create dynamic applications has the potential to reshape how various industries operate and interact with language data.

Now, as you know what are LangChains and their significance, it's time to get your hands dirty and build our 1st LangChain app.

Divider

Setting Up LangChain

Note: You need an OpenAI API key to get started 

Assuming you have Python present in your system already, we will start with

Pic1

So, what sort of app should we create? 

APP 1: GRAMMAR CHECKER 

Let’s create an app that intakes an English sentence, checks the grammar, and outputs ‘Correct’ when the input is grammatically right else rewrite the sentence. Ok?

  1. Import the required libraries and functions

Pic2

      2. Create a baseline prompt template for your app

  Pic3333

The explanation is easy:

  • We have created a system template which tells the LLM what it would be doing in this task
  • The human template is nothing but the texts we would be providing for grammar correction which we would pass as a parameter while running the LangChain object.
  • ChatPrompt combines both the System message and the Human message to form the final prompt

      3. Creating LangChain object using ChatPrompt finalized

Pic5

       4. Time to run the chain object

Pic6

Pic7.1

One thing you have noticed, we need to tell the LangChain object about the task just once. Later on, we just passed the text to be checked as a prompt. 

Let’s complicate a few things in the next short tutorial. Instead of just the input text, we will pass a few parameters as well to make the app more customizable.

APP 2: TONE CHANGER

This app will intake two parameters, input the sentence and tone of the text, and rewrite the sentence according to the tone passed.

Pic8

Pic9

Do notice how easily the parameters can be passed making it a dynamic prompt.

APP 3: Language Translation

In this app, we will translate input sentences into the desired language. It is very similar to what we did in the above step but with a twist where we would be passing a list of languages to translate a sentence into

Pic10

Pic11

As you can see, how the multiple outputs are presented as list. This is because of the use of a OutputParser which we have added in this example.

With this, we will be wrapping this beginner’s tutorial for LangChain to build apps using LLMs very easily. Do remember that this is just the beginning and LangChain has a lot to offer. We will be back soon with more complicated tutorials on LangChain.

Disclaimer: The views and opinions expressed in this blog post are solely those of the authors and do not reflect the official policy or position of any of the mentioned tools. This blog post is not a form of advertising and no remuneration was received for the creation and publication of this post. The intention is to share our findings and experiences using these tools and is intended purely for informational purposes.