LangChain Introduction & Components PDF
Document Details
Uploaded by Deleted User
Presidency University
Tags
Summary
This presentation introduces LangChain, an open-source framework for building applications leveraging large language models (LLMs). It provides tools and abstractions to chain together components and create complex workflows.
Full Transcript
MODULE #3 : Introduction to LangChain Module #3 - LangChain 1 L16. Introduction Langchain: Introduction, L1 Co3 T1:CH:2, to Langchain Components Pg.no.50-57 L01: Explain the key...
MODULE #3 : Introduction to LangChain Module #3 - LangChain 1 L16. Introduction Langchain: Introduction, L1 Co3 T1:CH:2, to Langchain Components Pg.no.50-57 L01: Explain the key L2 components of Langchain. L02:Classify the variant components of Langchain. Module #3 - LangChain 2 The LangChain… The Era…. Large Language Models (LLMs) entered the world stage with the release of OpenAI’s GPT-3 in 2020. The Google’s “sentient” LaMDA chatbot, The first high-performance and open-source LLM called BLOOM was released. OpenAI released their next-generation text embedding model and the next generation of “GPT-3.5” models in 2022. Coinciding with the momentous launch of OpenAI’s ChatGPT the following month, LangChain is an opensource project by Harrison Chase in October 2022, while working at machine learning startup Robust Intelligence and it enjoyed a meteoric rise to prominence: as of June 2023. Module #3 - LangChain 3 What is LangChain? A Simplified Definition.. LangChain is an open-source framework designed to simplify the process of building applications that leverage the power of large language models. It provides a set of tools and abstractions that make it easier to chain together different components and create complex workflows. LangChain itself is not an LLM It is a powerful framework designed to simplify and enhance the development of applications that interact with any LLMs. Its primary goal is to enable developers to build robust, flexible, and scalable applications by abstracting away the complexities of working with LLMs. Module #3 - LangChain 4 5 Module #3 - LangChain Get Started: https://www.pinecone.io/learn/series/langchain/langchain-intro/ https://www.pinecone.io/learn/langchain/ This framework comes with a package for both Python and JavaScript. LangChain can facilitate most use cases for LLMs and natural language processing (NLP), like chatbots, intelligent search, question-answering, summarization services or even virtual agents capable of robotic process automation. The purpose of LangChain is to establish a connection between these LLMs and external data sources, like personal documents or the internet, allowing developers to access and utilize relevant information. Module #3 - LangChain 6 How LangChain Works? Step1 : A user asks a question to the language model. Step 2: The vector representation of the question is used to do a similarity search in the vector database and the relevant information is fetched from the vector database. Step 3: The response is later fed to the language model. Step 4: The language model generates an answer or takes an action. Module #3 - LangChain 7 LangChain - Pipeline Module #3 - LangChain 8 Initially, the system starts with a large document containing a vast array of data. This document is then broken down into smaller, more manageable chunks. These chunks are subsequently embedded into vectors — a process that transforms the data into a format that can be quickly and efficiently retrieved by the system. These vectors are stored in a vector store, essentially a database optimized for handling vectorized data. When a user inputs a prompt into the system, LangChain queries this vector store to find information that closely matches or is relevant to the user's request. The system employs large LLMs to understand the context and intent of the user's prompt, which guides the retrieval of pertinent information from the vector store. Once the relevant information is identified, the LLM uses it to generate or complete an answer that accurately addresses the query. Module #3 - LangChain 9 Why LangChain? Simplify Development seamlessly to build application without much change in code. Multiple Tools Integrations Cloud Storage LangChain integrates with Amazon, Google, and Microsoft Azure cloud storage systems, allowing users to interact with files and data stored in these platforms. Few-Shot Learning Allowing the development of models that can learn from limited examples. Module #3 - LangChain 10 Language Models Incorporates language models from OpenAI, Anthropic, and Hugging Face, providing powerful NLP capabilities. Versatility for Multiple Use cases Possibilities for utilizing LLMs in various domains, including customer service, knowledge management, and more. Module #3 - LangChain 11 The LangChain framework is designed for developing applications powered by language models, enabling features like context awareness and reasoning. It comprises LangChain Libraries for integrating components into chains and agents, LangChain Templates for deployable architectures, LangServe for turning chains into APIs, and LangSmith for debugging and evaluating chains. Image Credit: LangChain Architecture (Github) Module #3 - LangChain 12 Get started: https://python.langchain.com/docs/concepts/#langchain-expression-language-lcel Development: Build your applications using LangChain's open-source building blocks, components, and third-party integrations Use LangGraph to build stateful agents with first-class streaming and human-in-the-loop support. Productionization: Use LangSmith to inspect, monitor and evaluate chains, optimize and deploy. Deployment: Turn your LangGraph applications into production-ready APIs and Assistants with LangGraph Cloud. 13 Concretely, the framework consists of the following open-source libraries: langchain-core: Base abstractions and LangChain Expression Language. langchain-community: Third party integrations. Partner packages e.g. langchain-openai, langchain-anthropic, etc… langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangGraph: Build robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph. Integrates smoothly with LangChain, but can be used without it. LangServe: Deploy LangChain chains as REST APIs. LangSmith: A developer platform that lets you debug, test, evaluate, and monitor LLM applications. 14 A simplified LangChain The core is that we can “chain” together different components to create more advanced use cases around LLMs. Prompt templates: Prompt templates are templates for different types of prompts. Like “chatbot” style templates, ELI5 question-answering, etc LLMs : Large language models like GPT-3, BLOOM, etc Tools: Agents use LLMs to decide what actions should be taken. Tools like web search or calculators are used Memory: Short-term memory, long-term memory. Module #3 - LangChain 15 LangChain – Core Key components Model Prompt Template Output Parser Chain Agent Retrieval Memory Module #3 - LangChain 16 17 Key Components – The Model The Model component abstracts and provides the LLM model. It provides the function of returning a text answer to the input prompt. Langchain supports various LLM https://python.langchain.com/docs/integrations/llms/ LangChain relies heavily on models, with two main types: ChatModels: Tailored for conversation, these are designed for interactive tasks where maintaining context across exchanges is key. LLMs: Accept string inputs and generate string outputs. These are the fundamental models used for tasks like NLP and SQL generation. 18 The model supports not only LLM but also LLM-based ChatModel. LLM is a model that reads one string and returns one string. ChatModel is a model optimized for chatting that receives multiple conversation sentences as input and responds with the next conversation sentence. ChatModel also has models provided by various vendors and open source models, https://python.langchain.com/docs/integrations/chat/ For general text processing, the LLM model is used, and for chatbots, LLM applications are developed using ChatModel. 19 Get started: https://python.langchain.com/docs/integrations/chat/openai/ Chat models use a sequence of messages as inputs and return messages as outputs. Chat Models are LLMs-based models that are capable of generating human-like text. Chat models are generally newer models… All chatmodels. Generally, we provide prompts to the ChatModel and the Model generates responses as chat messages. LangChain does not host any Chat Models, rather we rely on third party integrations. There are a lot of providers who provide Chat Models that can be integrated through LangChain. Some of the providers are Anthropic, OpenAI, MistralAI, GoogleGenerative AI, HuggingFace, etc.. Basic building blocks of an AI application using LangChain Module #3 - LangChain 20 Define the Model and invoke a Prompt to the Model Generate a response from the Gemini Chat models #load the API key from the environment file. # Then define the chat model with the name, API_key, and temperature parameters # Then invoke a prompt to the model and generate a response. 21 Parameters when constructing ChatModels model: the name of the model temperature: the sampling temperature timeout: request timeout max_tokens: max tokens to generate stop: default stop sequences max_retries: max number of times to retry requests api_key: API key for the model provider base_url: endpoint to send requests to 22 # Invoke the model with the message 23 Define the Model and invoke a Prompt to the Model Generate a response from Huggingface Chat models # Load with Huggingface Hub. # It will call you the token, that is already taken from Huggingface. # Put the token & successfully log in. 24 Define the Model and invoke a Prompt to the Model Generate a response from Huggingface Chat models # from LangChain, use HuggingfaceEndpoint & ChatHuggingFace To use the huggingface models. # HuggingfaceEndPoint helps to connect the LLM models that are hosted on Hugging Face’s API. # ChatHuggingface function takes the model and makes it useful for conversational tasks. 25 LLMs model https://python.langchain.com/v0.1/docs/integrations/llms/ Language models that takes a string as input and returns a string. These are traditionally older models (newer models generally are Chat Models,). Although the underlying models are string in, string out, the LangChain wrappers also allow these models to take messages as input. When messages are passed in as input, they will be formatted into a string under the hood before being passed to the underlying model. LangChain does not host any LLMs, rather we rely on third party integrations. 26 Message Generally, LLM models take a list of messages and generate messages. All messages have a role, content & response_metadata property. content describes the output string. response_metadata provides some metadata such as a number of tokens, ethics, and others. The role tells us that “WHO” is saying the message. The standard roles are user, assistant, system & tool. Including roles, “user” is HumanMessage is the question of the user “assistant” is AIMessage(Output of the message), “system” is SystemMessage that tells the model how to behave to generate output “tool” is ToolMessage that contains the output to call a tool. 27 #create a message with different roles # invoke the message to the model. # with the message, we can provide the response structure to the model. 28 Prompt Template Prompt templates help to translate user input and parameters into instructions for a language model. This can be used to guide a model's response, helping it understand the context and generate relevant and coherent language-based output. Prompt Templates take as input a dictionary, where each key represents a variable in the prompt template to fill in. Prompt Templates output a PromptValue. 29 Prompt Template This PromptValue can be passed to an LLM or a ChatModel and can also be cast to a string or a list of messages. The reason this PromptValue exists is to make it easy to switch between strings and messages. The following are few types of Prompt Template: String PromptTemplates These prompt templates are used to format a single string (simpler inputs) For example, a common way to construct and use a PromptTemplate is as follows: 30 #String PromptTemplates ChatPromptTemplates These prompt templates are used to format a list of messages. These "templates" consist of a list of templates themselves. 31 In the above example, this ChatPromptTemplate will construct two messages when called. The first is a system message, that has no variables to format. The second is a HumanMessage, and will be formatted by the topic variable the user passes in. 32 MessagesPlaceholder This prompt template is responsible for adding a list of messages in a particular place/spot. In the ChatPromptTemplate, we saw how we could format two messages, each one a string. But what if we wanted the user to pass in a list of messages that we would slot into a particular spot? This is how you use MessagesPlaceholder. 33 As in the above code, a list of two messages is produced, the first one being a system message, and the second one being the HumanMessage we passed in. If we had passed in 5 messages, then it would have produced 6 messages in total (the system message plus the 5 passed in). This is useful for letting a list of messages be slotted into a particular spot. An alternative way to accomplish the same thing without using the MessagesPlaceholder class explicitly is: 34 Few-shot prompting One common prompting technique for achieving better performance is to include examples as part of the prompt. This is known as few-shot prompting. This gives the language model concrete examples of how it should behave. Sometimes these examples are hardcoded into the prompt, it may be nice to dynamically select them. Example: Selectors are classes responsible for selecting and then formatting examples into prompts. Module #3 - LangChain 35 Chains – are Building Blocks Chains in LangChain are sequences of steps where each step uses the output from the previous one. They help in creating more complex workflows by combining simpler components. A chain is a series of automated actions from the user's query to the model's output. For example, developers can use a chain for: Analogy: Chains are like making a delicious Connecting to different data sources. masala dosa. You have to follow steps: making Generating unique content. the batter, preparing the filling, cooking the Translating multiple languages. dosa, and finally assembling it all together.** Answering user queries. 36 # an example of chain 37 Chains are sequences of calls to components (such as LLMs, prompts, or other chains) that can be executed together. They allow you to combine multiple steps into a single, reusable pipeline. Types of Chains: SimpleSequentialChain: Executes a series of steps in order, passing the output of one step as input to the next. SequentialChain: Similar to SimpleSequentialChain, but allows for multiple inputs and outputs. RouterChain: Dynamically selects which chain to use based on the input. 38 #Chain types 39 High-level Structure of LangChain Module #3 - LangChain 40 Output Parser Get started: https://python.langchain.com/docs/concepts/#output-parsers Output parser is responsible for taking the output of a model and transforming it to a more suitable format for downstream tasks. Generate structured data, or to normalize output from chat models and LLMs. LangChain has lots of different types of output parsers. This is a list of output parsers LangChain supports are: JSON, XML, CSV,OutputFixing,RetryWithError, Pydantic, YAML, PandasDataFrame, Enum, Datetime, Structured. 41 Output Parser The output results from the LLM model is formatted in formats such as JSON, XML, or Pandas DataFrame depending on the requirements. The output parser parses the resulting values from the LLM model according to the format and returns them as text. 42 Agents - are The Decision makers** Agents use AI models to decide what action to take. They need an executor, much like a cricket captain needs a team to implement the strategy. The agent will choose a tool to use, get the results, and then decide the next step. Analogy: Agents are like a cricket captain who decides which player should bowl next based on the game situation.** 43 Agents Language models can't take actions - they just output text. A big use case for LangChain is creating agents. Agents are systems that use an LLM as a reasoning engine to determine which actions to take and what the inputs to those actions should be. The results of those actions can then be fed back into the agent and it determine whether more actions are needed, LangGraph is an extension of LangChain specifically aimed at creating highly controllable and customizable agents. Please check out that documentation for a more in depth overview of agent concepts. 44 Agents in LangChain are autonomous entities that can use tools and make decisions to complete tasks. They combine the power of LLMs with the ability to interact with external tools and APIs. Components of an Agent: Agent: The decision-making entity. Tools: Functions or APIs that the agent can use. Toolkits: Pre-configured sets of tools for specific tasks. AgentExecutor: Manages the execution of the agent and its interactions with tools. 45 #Agents 46 The LLM model can use multiple tools. The agent decides which tool to be used. The Agent uses various methods to find the appropriate tool for the question. In general, the Agent uses the description of the tool included when registering the tool to help LLM determine which tool is appropriate for the problem. 47 ReAct agents One popular architecture for building agents is ReAct. ReAct combines reasoning and acting in an iterative process - in fact the name "ReAct" stands for "Reason" and "Act". # an example of Agent 48 Memory Large Language Models (LLMs) process information based on the current input they receive. They lack inherent memory and don’t retain information from previous interactions. This is why each query or prompt is treated as independent, and this characteristic is often referred to as statelessness. Conversational language model applications refine their responses with information recalled from past interactions. Module #3 - LangChain 49 Memory LangChain allows developers to include memory capabilities in their systems. It supports: Simple memory systems that recall the most recent conversations. Complex memory structures that analyse historical messages to return the most relevant results. Module #3 - LangChain 50 51 Types of memory ConversationBufferMemory allows for storing of messages and then extracts the messages in a variable. ConversationBufferWindowMemory maintains a list of conversational exchanges over a period, utilizing only the most recent K interactions. Entity can extract information on entities in a conversation. ConversationKGMemory utilizes a knowledge graph to recreate memory. 52 ConversationSummaryBufferMemory merges two concepts by retaining a buffer of recent exchanges while also summarizing older interactions instead of discarding them entirely. ConversationTokenBufferMemory keeps a buffer of recent interactions in memory and uses token length instead of the number of interactions. VectorStoreRetrieverMemory stores memories in a vector store and queries the top-K most “salient” docs every time it is called. ConversationSummaryMemory captures the main points and key takeaways from a conversation. 53 #ConversationalBufferMemory 54 Retrieval Retrieval is a library used in the RAG (Retrieval Argument Generation) architecture. As mentioned earlier, LLM can only provide answers based on learned knowledge. When a company has a document or a document such as a manual for a specific service, this document is saved in the Document Database If there is information needed to answer a question, the document database is searched, a sentence (or paragraph) related to the question. Finally, the LLM answers based on the question. 55 To achieve retrieval process, various Retrievers in LangChain are components that additional functions are required such as: fetch relevant information from external sources. Vector stores are specialized databases that store function to save a document and retrieve vector embeddings of text, enabling create a search index, semantic search capabilities. function to create an index by dividing a Types of Retrievers: large document into several pieces. VectorStoreRetriever: Retrieves similar documents The Retrieval component provides these based on vector similarity. functions. SVMRetriever: Uses Support Vector Machines for retrieval. TFIDFRetriever: Uses TF-IDF for document similarity. 56 Applications of LangChain Module #3 - LangChain 57 Module #3 - LangChain 58