LangChain is a framework that ‘chains’ different components to create complex and flexible solutions with multiple LLMs. This is useful when the task is too complex for a specific LLM alone.
LangChain abstracts common AI programming concepts into unified interfaces and provides a robust implementation. You can easily swap out your AI model with a different one without changing your applications.
Chains are implementations of strategies used to interact with indexes.
They make interactions between LLMs and private data easier, especially when working with many documents.
Four Types of Chains
Stuffing
Stuffing feeds all the relevant data to the LLMs in a single prompt. This strategy works well with small pieces of data. However, this approach will not work when the documents exceed the prompt context length limit.
Map Reduce
This method runs each chunk of data through LLMs for summarisation or initial processing. It then combines the results into a single prompt to obtain the final result. Although it may lose some information during the process, it scales well for large amounts of data.
Refine
Similar to Map Reduce, this strategy takes a stepped approach. The difference is that it only runs the initial processing for the first chuck of data. The output is then passed on alongside the next document to be refined. This method can produce high-quality output compared to the parallel process of Map Reduce. The downside is that it does introduce some dependencies, and the execution will be slower than the parallel processing.
Map-Rerank
This method runs initial processing on each chunk of data and obtains a response and a confidence score. It then ranks the confidence score and returns the response with the highest confidence score. This strategy is only valid when the expected response is contained in a single document.
Components of LangChain
The four main components in LangChain are Agents, LLMs, Prompt Templates and Memory.
Prompt Templates
In LangChain, PromptTemplate
is responsible for constructing the input to an AI model, and the constructed result is represented by the PromptValue
class (returned by the formatPromptValue
of a PromptTemplate
), which can be converted to a string or list of ChatMessage
objects.
Abstracting the prompt enables the same prompt to be used in different model types by converting it into the exact input type each model expects. A PromptValue
is often dynamically constructed by various inputs to the PromptTemplate
.
References
- Radovanovic, M.B., Rajko (2023). Emerging Architectures for LLM Applications. [online] Andreessen Horowitz. Available at: https://a16z.com/emerging-architectures-for-llm-applications/.