The Talent500 Blog
LangChain

Getting started with LangChainJS

Since ChatGPT came into the picture, large language models (LLMs) have become the talk of the town. These are powerful tools that can do some amazing things. And you don’t need a supercomputer in your basement to use them.

Currently, the tech industry is all about building applications on top of large language models (LLMs). Think chatbots with PDF integration, image editing apps, and so much more. But what if you want to create your own app using these powerful models?

In this in-depth blog, we’ll explore a well known framework, LangChain, which enables you to work with these large language models and build apps on top of it.

So lets get started!

What are Large Language Models (LLMs)?

LLMs refers to models with an exceptionally large number of parameters. Large Language Models are used in natural language processing and understanding tasks, and their performance improves with an increase in the number of parameters.

Getting started with LangChainJS 1     

    Large Language Model vs Language model

To consider a language model to be large language model, there are two main aspects:

Quantitative traits

The quantitative distinction of an LLM primarily lies in the number of parameters it possesses. These models are characterized by having an extremely high number of parameters, typically on the order of 10 to 100 billion. 

The parameters in these models are learned from vast amounts of text data during training, which allows them to generate text, answer questions, and perform various language-related tasks with impressive accuracy and fluency.

 As LLMs increase in size, they exhibit certain qualitative improvements in their capabilities.  Larger models tend to have a deeper understanding of the nuances in human language. They can answer questions more accurately, summarize text more effectively, and generate human-like text.

Some LLMs extend their capabilities to work with other modalities, such as images, by incorporating multimodal learning, which enables them to generate text descriptions for images, translate between languages, and more.

What is LangChain

LangChain is an open source framework that simplifies the creation of applications powered by large language models (LLMs). LLMs are advanced AI algorithms that use deep learning and extensive datasets to understand, summarize, generate, and predict content.

Getting started with LangChainJS 2

LangChain star history

LangChain allows you to build applications that are:

Context-Aware: LangChain makes it easy to build applications that are capable of remembering the context. It does so by establishing connections between the language model and various sources of context, such as prompt instructions, few-shot examples, or external content. This integration ensures that the language model is not isolated but is informed by the broader context in which it operates. As a result, the application becomes highly responsive to the specific task or query at hand, delivering more relevant and accurate responses.

Reasoning Capabilities: LangChain enables applications to leverage the language model’s formidable reasoning capabilities. These capabilities include the model’s capacity to deduce logical conclusions and make informed decisions based on the provided context. 

How Does LangChain Work?

To work with LangChain, you need to choose an appropriate model for your application like the GPT-3 or you can also use your custom models.

Once you’ve got your language model ready, you’re all set to begin crafting applications with LangChain. 

LangChain comes equipped with a range of user-friendly tools and APIs, which streamline the process of linking your language model to external data sources, enabling interactions with the surrounding environment, and simplifying the development process of applications.

Getting started with LangChainJS 3

Basic Working of LangChain

In simpler terms, LangChain takes a massive amount of data and divides it into smaller pieces that can be stored in a vector database, similar to PineCone. When an end user provides a prompt, we can retrieve the relevant information from the vector database and then feed that information to a large language model to generate the answer.

Installing LangChain 

To start using LangChain, you need to install it. You can do this using package managers like npm, Yarn, or pnpm. Here’s how you can install LangChain using npm:

npm install -S langchain

If you are a python developer, to install LangChain run:

pip install langchain

In this blog, we will try to stick with JavaScript but ofcourse the implementation remains the same.

Modules in LangChain

LangChain comprises six essential modules, each serving a unique role:

  • Large Language Models: LangChain connects with different language models, giving it the ability to understand and generate text.
  • Prompt templates: It is like having a template where we add the users prompt and the prompt will be with in a boundary. It is for formatting the user input in a way that it makes sense
  • Conversational History: LangChain can remember and use past conversations, which is super helpful for applications like chatbots. LangChain offers a wide array of tools  for adding memory to a system. You can utilize these tools independently or seamlessly integrate them into a sequence of actions.
  • Indexing: LangChain organizes documents in a way that makes it easy for the models to find the right information.
  • Chaining Models: You can link multiple models together in LangChain for more complex tasks, like having them work together to do something smart.
  • Agents: Agents use a language model to decide what actions to take and in what order, whereas in chains, actions are just programmed into the code.

Advantages of using LangChain

User-Friendly: LangChain offers an intuitive high-level API, simplifying the process of linking language models with different data sources and constructing intricate applications.

Adaptability: This framework is versatile, enabling the creation of a broad spectrum of applications, ranging from chatbots to question-answering systems.

Scalability: LangChain empowers the development of applications capable of efficiently handling substantial volumes of data.

Open-Source: LangChain is an open-source framework, ensuring it is freely available for use and modification.

Supportive Community: A robust and engaged community of LangChain users and developers stands ready to provide assistance and support.

Comprehensive Documentation: LangChain’s documentation is both extensive and user-friendly, facilitating ease of use.

Seamless Integration: LangChain seamlessly integrates with other popular frameworks and libraries like Flask and TensorFlow.

Expandability: Developers can enhance the framework by incorporating their unique features and functionalities, making it highly adaptable to specific requirements.

How to start using LangChain

Once you have installed LangChain using JavaScript or Python, now  you start working on the environment.

In most scenarios, you need some kind of model, for example any OpenAI models and the API keys and now you can start using the model.

  import { OpenAI } from “langchain/llms/openai”;

const llm = new OpenAI({

  openAIApiKey: “YOUR_KEY_HERE”,

});

Now you can start using it.

   //Import the OpenAPI Large Language Model (you can import other models here eg. Cohere)

    import { OpenAI } from “langchain/llms”;

    //Load environment variables (populate process.env from .env file)

    import * as dotenv from “dotenv”;

    dotenv.config();

    export const run = async () => {

      //Instantiate the OpenAI model

      // Pass the ‘temperature’ parameter, which determines the degree of randomness in the model’s output. Lower values yield more predictable output, while higher values generate more random results. The temperature parameter can be set within the range of 0 to 1, with 0 indicating min predictability and 1 indicating max randomness.

      const model = new OpenAI({ temperature: 0.9 });

      //Calls out to the model’s (OpenAI’s) endpoint passing the prompt. This call returns a string

      const res = await model.call(

        “What country has the most beautiful beaches?”

      );

      console.log({ res });

    };

    run();

What can you build with LangChain

LangChain enables the creation of many useful LLM-powered applications that can benefit millions or even billions of people. With LangChain, we can build apps that reduce daily tasks and allow people to focus on creativity.

Some of the use cases of LangChain are:

  1. Question-Answering Systems: LangChain can be used to develop systems that can understand and respond to user questions with relevant information.
  2. Text Generation: You can build applications for text generation, including generating creative content, automatic report writing, or content for websites.
  3. Language Learning Apps: LangChain can be used in applications that assist users in learning new languages, improving vocabulary, and practicing language skills.
  4. Chat with documents app: LangChain can be used to build on top of a dataset like a report or pdf and users can interact with the document.
  5. Coding Assistant: LangChain can power an AI code assistant, providing context-aware code suggestions, error detection, and documentation retrieval, enhancing developer productivity and reducing coding errors in real-time.
  6. Legal Document Analysis: It can assist law firms in automatically summarizing legal documents, extracting key information, and performing legal research, thus streamlining legal processes.
  7.  Personal Assistants: LangChain’s flexibility allows developers to integrate and expand the capabilities of virtual personal assistants, making them more versatile and adaptable to individual user needs.

There are many more app ideas, and I’ll leave those to your creativity.

Wrapping it Up

Thank you for visiting this blog. In this detailed post, we discussed large language models, how they work, and more. We also explored LangChain, an open-source framework for building applications on top of large language models. 

We covered how to get started with LangChain, its applications, and use cases. If you’re interested in building applications using LangChain, this blog provides a good starting point.

Keep Learning!

1+
Adarsh M

Adarsh M

A soon-to-be engineer and software developer. Curious about advanced tools and Web technologies. Loves to be a part of fast moving technical world.

Add comment