DEV Community

technonotes-hacker
technonotes-hacker

Posted on

Promotion - Framework - LangChain

Intro

  • Open-Source Framework
  • Its used for building the applications powered by LLM.
  • In simple words --> Langchain helps to connect an LLM with prompts, data , Tools , memory , APIs and other components to build useful AI Application.
  • LLM can answer your question but you can't build a APLLICATION.

Without LangChain

User --> Prompt --> LLM --> Answer

With Langchain

Why we need Langchain ?

  • It helps to build real world applications around LLMs by connecting with Prompts, documents , database , API's , Tools , Memory and Other Components.
  • See LLM is also powerful, but its mainly a TEXT GENERATION Engine.
  • But for PRODUCTION AI APPLICATION you need entire workflow around it.
  • Example:

Langchain Architecture

  • Its a framework and supports many things.
  • Its like a SET OF COMPONENTS that sits between user/Application & LLM , connecting the model to data, tools and workflow.

Langchain Components

  • It provides different building blocks with that you can combine to create LLM applications like:
  1. Chatbots
  2. RAG Systems
  3. AI Agents
  4. AI Data Analysts
  • 6 Areas that Langchain is designed to HELP.

Model I/O

  • Main area of Langchain.
  • It handles the interaction between the application and the LLM or chat model.
  • Model I/O = Input --> Model --> Output

  • Main parts of Model I/O:
1. Prompt Template
2. Models
3. Output Parser
Enter fullscreen mode Exit fullscreen mode
  • Prompt --> Instruction to the LLM model so that it can perform the task for us.
  • Instead of repeating the prompt ( hardcoding prompts ) , Langchain allows us to create a reusable templates.
  • If you want to ask 100 different topics like:
1. Explain Python
2. Explain SQL
3. Explain Deep Learning
........................
........................
Enter fullscreen mode Exit fullscreen mode
  • You can create a reusable temple which will allow to explain in simple terms.
Explain { Topic } in simple terms. 
Enter fullscreen mode Exit fullscreen mode

Models

  • Model is responsible for generating the response.
  • Langchain will work with different types of models.
  • Its supports Multi Model Systems.

  • StrOutputParser --> It will help to put in Plain TEXT.

  • Temperature --> Output generation depends on this parameter. In other words , how creative you are going to get the response.

Data Connections

  • Connecting the LLM Application to the external or private data source so the LLM can retrieve and use information that is not available in its training DATA.
  • Like Confidential Data , Policy , etc
  • Eg., A company has 500 PDF documents and we want to build a chatbot that answers questions from the PDFs.

  • Document Loader is supported by LangChain.
  • Eg., PDF , CSV , File , Txt , Word Docs , Website , Database , Notion , Google Drive , APIs.
  • Directly அந்த documentல இருந்து எதுமே எடுக்க முடியாது, அதனால its divided into Text Splitters.
  • Text Splitters --> Chunks or Tokens , Large documents are divided into smaller chunks.

  • Now the models can't understand the text data , so EMBEDDINGs coming into picture.
  • Embeddings --> Converting text into numerical vectors using Semantic or Similarities.
  • Once the numerical Vectors are created then it needs to be stored.
  • Vector Store or Vectore Database --> The embeddings are stored in a vector Store or Vectore Database. ( Eg., FAISS , Chroma , Pinecone , Weaviate , Milvus , Qdrant )
  • Retreiver --> Searches the vector store for information relevant to the user's questions.
  • RAG --> All these components together are commonly called as RAG - Retrieval Augmented Generation.

Chains in LangChain

  • Sequence of steps , where the output of 1 step becomes the input of next step.
  • Chain = Connecting Multiple Operations together to complete a TASK.

Why use CHAINS ?

  1. Reuse workflows
  2. Modularize AI applications
  3. Connect Multiple components Together
  4. Build complex applications like Chat-bots, RAG systems and Agents.

Basic LLM Chain

  • Takes the user input , puts that into a prompt , sends the prompt to LLM such as Gemini and returns the response.

  • There are 3 components:
  1. Prompt
  2. LLM
  3. Output Parser
  • Who is responsible to combine all 3 ? Chain.

  • Temperature --> Controls the randomness or creativity of the LLM output.

Sequential Chain in LangChain

  • Executing multiple steps one after the another.
  • Where the output of one step is passed to the next step.

  • Connect both the chain , which is called Runnable Pass through in LangChain.
  • LCEL --> LangChain Expression Language. ( Its a syntax and framework in LangChain used to combine different components into chains using " | ". )

Parallel Chain

  • Runs multiple independent chains at the same time using the same input.

  • All 3 chains can work independently.
  • Example: If we type --> Machine Learning , we want Gemini to generate , Definition , Advantages & Applications.

LangChain Memory

  • Allows application to remember information from previous interactions and use that information in future conversations.
  • Without Memory:
User : My name is Leo
AI : Nice to meet you Leo

User : What is my name ?
AI : I don't know
Enter fullscreen mode Exit fullscreen mode
  • With Memory:
User : My name is Leo
AI : Nice to meet you Leo

User : What is my name ?
AI : Your name is Leo.
Enter fullscreen mode Exit fullscreen mode

Why do we need Memory ?

  • LLMs are generally stateless between individual calls.
  • stateless --> Meaning --> They won't remember any past conversation.
  • Memory solves this by maintaining relevant conversation information and providing it to the model with next request.

Stateless Vs State-full LLM

  • Stateless means the LLM doesn't remember previous request automatically. Each request treated independently.
  • State-full means the application maintains information about previous interactions and provide relevant state to the LLM.

Key in Memory

  • Stateless to Statefull is the key idea behind LangChain Memory.

Session ID

  • Statefull applications usually need to identify whose conversation is being maintained.

Overall

  • LLM usually Stateless.
  • Application + Memory/State --> Statefull Experience
  • A Stateless LLM treats each API request independently and doesn't automatically retain previous conversation context.
  • A Statefull LLM application maintains state such as conversation history and provides that state with subsequent requests , allowing the system to maintain context across interactoins.

Conversation Buffer Memory in LangChain

  • Simplest type of memory.
  • It stores complete conversation history and sends that history along with every new question to the LLM.

Without Memory:

  • Because the second request doesn't automatically contain the first conversation.

With Conversation Buffer Memory:

  • Memory stores the previous messages.

Main Disadvantage :

  • Suppose the conversation becomes very long.
  • Every new request may require a large amount of history to be sent to the model.
  • This will lead to :
  1. Higher Token Usage
  2. Higher Cost
  3. Slower Response
  4. Context Window Limitations

Conversation Buffer Window in LangChain

  • Keeps only the most recent part of the conversation instead of the complete conversation.
  • The word WINDOW means we maintain a moving window of recent messages.

  • So:
1. Buffer Memory --> Remembers everything 
2. Buffer Window Memory --> Remembers only the recent messages
Enter fullscreen mode Exit fullscreen mode
  • Why we need window memory ?

Conversation Token Buffer Memory in LangChain

  • Its a token-based memory approach.
  • Instead of saying: " Keep the last 4 messages "
  • It says : Keep the conversation history only up to a certain number of tokens.
  • This is useful because LLMs process tokens, not messages.

Conversation Summary Memory in LangChain

  • It keeps a summary of the previous conversation instead of sending the entire conversation to the LLM every time.
  • Its is very useful when the conversation becomes long.
  • Conversation:

  • Summary:

LangChain Agent

  • Agent places important role when it comes to Generative AI.
  • Its an application where an LLM can decide what action to take, use available tools, observe the result and continue until it can answer the user's request.
  • Agent can decide which tool needs to be used.
  • Example:

  • Easy to REMEMBER:
  1. Chain --> You can decide the steps
  2. Agent --> LLM decides the steps.

Agent Architecture

The Four Main Components

LangChain Callbacks

  • Callbacks are the mechanisms that allow you to monitor and respond to events that happen during a LangChain execution.
  • Callbacks let us observe what is happening inside an LLM application.

why we need Callbacks ?

  • Image you Agents is taking 10 seconds to respond.

  • To track all these steps we need Callback.

Notes

  • Temperature

  • Agent --> Its responsible for deciding which tool I should use ? what should I do ? what should I generate ? All these are decided by the agent.

Top comments (0)