Why Use LCEL?
LangChain Expression Language (LCEL) is a declarative way to compose chains. It offers several benefits out of the box:- First-class Streaming Support: When you build your chains with LCEL, you get the best possible time-to-first-token (time elapsed until the first chunk of output comes out).
- Async Support: Any chain built with LCEL can be called both with the synchronous API (e.g.,
.invoke()) and with the asynchronous API (e.g.,.ainvoke()). - Optimized Parallel Execution: Whenever your LCEL chains have steps that can be executed in parallel, LangChain executes them automatically.
Structure of a Basic Chain
At its core, a chain consists of:- Input: The starting dictionary or string (e.g.,
{"topic": "science"}). - Prompt Template: Takes raw input and formats it into prompt messages.
- Model: Takes prompt messages and returns an AI message response.
- Parser: Takes the AI message and extracts raw text or structured data.
|):