Key Concepts Of Langchain
LangChain contains common abstractions that are useful to build language applications. This post introduces some of those concepts.
1. Language Models
- Language models (LLM) are the core of LangChain applications.
- Their main function is text generation: given a prompt, generate a text that follows the prompt.
- At the core of a language model is a neural network that takes a sequence of tokens as input and outputs a probability distribution over the next token. The probability distribution is learned from a large corpus of text.
2. Prompts
- Prompts are inputs passed to models to get desired outputs.
ChatPromptTemplate
enable create template so that prompts can be generated automatically and reused repeatedly.
3. Parser
- Parsers extract structured information from model outputs -> enable chaining of models.
ResponseSchema
andStructuredOutputParser
works together to create format instruction and parse model outputs.
4. Memory
- Memory store and manage the context and history of the ongoing conversation, and allow the LLM to refer back to earlier parts of the conversation to generate relevant and context-aware responses.
ConversationBufferMemory
stores the entire conversation history in a buffer, can become long and costly - O(n^2) for n turns.ConversationBufferWindowMemory
andConversationTokenBufferMemory
limit the size of the buffer to the last k turns or tokens. - O(n) for n turns.ConversationSummaryBufferMemory
uses an LLM to generate a summary of the conversation history - O(n) for n turns.
5. Chain
Chain is a fundamental concept in LangChain, it allows you to:
- Combine an LLM with a prompt to create a functional unit that processes input.
- Chain multiple subchains together to perform a sequence of operations on the input.
- Route input data to different subchains based on conditions or requirements.
- Easily switch between different types of chains to achieve various tasks.
Types of Chains:
LLMChain
= LLM + a single prompt, the most basic type of chain.SimpleSequentialChain
andSequentialChain
= a sequence of subchains.LLMRouterChain
used to route input data to different subchains based on specific conditions.
6. Embedding & VectorStore
Embedding
capture the semantic meaning of the text and allow comparisons in a vector spaceVectorStoreIndexCreator
create a vector store index. It helps create a vector database that stores vector representations generated by embeddings.- The two combined to enable information retrieval and answer questions from external knowledge bases.