Quantcast
Channel: Web Science and Digital Libraries Research Group
Viewing all articles
Browse latest Browse all 744

2024-02-07: LangChain: A Framework to Build Applications with LLMs

$
0
0

Figure 1: LangChain Components

In the world of artificial intelligence, one game-changing innovation has been grabbing everyone's attention and transforming the way we interact with language – Large Language Models (LLMs). But for developers, harnessing their full potential has often been very challenging. LangChain is a framework for building applications powered by LLMs that makes it easier for developers to use LLMs. For those skilled in Python, JavaScript, or TypeScript programming languages, LangChain provides packages that developers, software engineers, and data scientists can leverage.

Overview of LangChain

LangChain offers an advanced framework for engaging with LLMs, external libraries, prompts, and user interfaces. It is available in Python or JavaScript (TypeScript) packages. LangChain's user-friendly architecture allows users to utilize its various components such as agents and chains, and strategically assemble them to fulfill specific tasks like summarization, Q&A, and more.

Key Components of LangChain

LangChain encompasses six primary modules for constructing LLM applications: Models, Prompts, Chains, Agents, Memory, and Indexes

ModelsModel is one of the fundamental components of LangChain framework. LangChain provides two types of models.
  • llms: llm class provides the interface to call language model APIs and libraries provided by OpenAI, Cohere, Hugging Face, etc. LLMs take a text string as input and return another text string as output. LangChain offers the functionalities to design a customized LLM wrapper.

AI Response: "As of 2021, Nike is the most popular shoe brand in the US. Other popular brands include Adidas, Vans, Converse, and New Balance."

  • chat_models: ChatModel class supports the interaction with different chat models supported by OpenAI. It receives input in the form of a list of chat messages and produces a chat message in return. There are three different types of chat models designed for implementing chatbot applications:
    • HumanMessage: HumanMessages are used for receiving input from the users.
    • AIMessage: AIMessages are response generated by the language model.
    • SystemMessage: The purpose of SystemMessage is to instruct the chat model describing its role.

    AIMessage(content="Black holes are fascinating yet mysterious objects in space. From a visual standpoint, black holes do not emit or reflect any light, making them essentially invisible to the naked eye. This is because their gravitational pull is so strong that nothing, including light, can escape from their vicinity.\n\nHowever, black holes can be indirectly observed through their effects on nearby matter and light. When matter gets too close to a black hole, it spirals around it in a disk-like structure called an accretion disk. Due to the intense gravity, this matter gets heated up and emits high-energy radiation, including X-rays and gamma-rays...")

             Now we can see that this chat model will function as an Astrophysicist for any provided prompt. There's no need to             rewrite the entire prompt if the use case remains the same.

    Prompts: A prompt is a set of instructions provided by users to feed into the model for generating relevant output. LangChain offers predefined templates for inputs designed for an LLM, eliminating the necessity to create the entire prompts from the scratch. The prompts have three types.

    • Prompt Templates: Prompt templates are used to generate templates for string prompts. They are used with the LLM models explained above. 


           From the example above we can see that the prompt will now act as an "Essay generator" on "UFO". We must                   customize the line number and topic name for the specific topics. LangChain also gives the flexibility to design your           personalized prompt template. You can explore the following article to learn more about Prompt Templates.
    • ChatPrompt Templates: ChatPrompt Templates are predefined templates for ChatModels to personalize prompts. As previously mentioned, there are three categories of Chat Models: System, AI, and Human. Each type has a distinct template, as demonstrated in the example below. Similar to Prompt Template, you have the option to create your own templates for ChatPrompts.

    • OutputParsers:  LangChain's prompt libraries support output parsing. Output parsing involves taking the model's output and transforming it into any structured format we want. LangChain has different types of output parsers. To build complex applications using LLMs, we can use any output parser to generate their output in certain formats. The following example involves having an LLM-generated JSON output and utilizing LangChain to parse that output.


    Chains: Chains are used for linking multiple LLMs together or integrating them with other elements, such as third party tools, libraries and even OpenAI functions. Chains are responsible for connecting prompts, LLMs and output parsers. LangChain offers LCEL Chain constructors and Legacy Chains that are readily customizable. The given code demonstrates the integration of multiple LLMs to perform various tasks, including writing a movie review, generating a follow-up comment, summarizing the review, and determining the emotional tone of the review.





    Chains can be simple (i.e. Generic) or specialized (i.e. Utility).

    • Generic: A single LLM is the simplest chain. This simplest chain can be used to build input prompts for text generation. Let's consider an example to demonstrate building a basic chain. In the following example, we've created a prompt template for a movie recommendation system. The input prompt tells the AI to suggest a good movie based on the specified genre. Following this, an LLMChain instance is formed with OpenAI's model to generate the AI prediction for the recommended movie.


    • Utility: Utility are customized chains, consisting of multiple LLMs, designed to address particular tasks. For example, the LLMMathChain is a simple utility chain that gives LLMs the ability to perform mathematical operation. 



    Agents: The Agent class uses LLMs and prompts to determine appropriate course of actions, offering flexibility. In contrast, Chains follow a predetermined sequence of fixed steps. Agents are more versatile and can be integrated with various tools. In case of agents, the LLM decides which tool to use for executing a query based on its nature. For chains, the entire code and sequence of steps are predetermined. Therefore, for a more adaptable and dynamic application, Agents present a superior choice. Below is the code snippet for creating an agent: 


    The code snippet reveals that 
    the agent uses LLM to interact with SERP API to extract information about political events. SERP API, is an API designed for scraping large amount of data from various search engines, allowing the extraction of information for any given search topic.

    Memory: Language Models don't have the ability to track the conversation over time. While engaging with these models, they naturally do not retain information from previous interactions or conversations. This lack of continuity can be challenging for someone trying to build applications such as chatbots, where maintaining a record of past conversations is crucial. In the context of LangChain, conversational memories are constructed based on the ConversationChain. LangChain offers utility classes to help build memory within your application. Following is the implementation of conversational memory that uses LLMChain to interact with both LLM and ChatModel.


    Indexes: Indexing is a key aspect of LangChain that enables llms to retrieve relevant document from a set of documents. It facilitates swift and effective access to both structured and unstructured data. For indexing, LangChain uses a record manage to maintain document writes into the vector store. The following code snippet demonstrates the use of indexing to initialize a vector store and configure the embeddings.

                                                                   Source: LangChain Documentation

    Several alternative frameworks are available to simplify the development of LLM applications. I have also explored LlamaIndex, another open-source framework specifically designed for building LLM-based applications, with a focus on intelligent search, data indexing and retrieval, particularly suitable for those dealing with vector embeddings. While working with LangChain, I found it preferable for developing NLP applications like question-answering systems, chatbots, and summarization tools compared to LlamaIndex. One aspect I appreciate about LangChain is its high level of customization, offering the ability to link multiple LLMs, tools, and components together, enhancing the flexibility and reliability of application development. Integrating OpenAI and Hugging Face LLMs with LangChain was effortless for me, particularly when compared to non-OpenAI LLMs. If you're aiming to build a versatile and extensible general purpose application, and prefer working with OpenAI APIs, I would recommend using LangChain.

    -- Lamia Salsabil (@liya_lamia)


    Viewing all articles
    Browse latest Browse all 744

    Trending Articles