LangChain classic: LCEL apps and chat history
Chains with LCEL and the Runnable interface, serving a chain with LangServe, and a chatbot that remembers each conversation and trims what it sends.
Chains built with LCEL, LangChain's way of joining runnables: what | creates, parallel branches, passthroughs and the settings that reach every step, with a RAG chain written out in full. Then a chain served as a web API with LangServe or plain FastAPI, and a chatbot that remembers each conversation and trims what it sends to the model. Lesson 7.5, A chatbot in one script, puts the module in one file with Gemini. The memory classes here are deprecated in favour of LangGraph's persistence (lesson 11.5), but the ideas carry over unchanged. Run the notebook cells in order, and try each practice task before you open its solution.
Lessons
- 1Chains with LCELBuild a translation chain one runnable at a time, then learn the Runnable interface (invoke, batch, stream, async, schemas, config), the three shapes of composition (sequence, parallel, assign), a RAG chain written in LCEL, and when a chain is not enough.
- 2Serving a chain with LangServeServe the translation chain with FastAPI and LangServe, see the endpoints and schemas it creates, call it with requests and from a Streamlit page, write the same endpoint in plain FastAPI, and learn what a production API around a model needs.
- 3A chatbot with message historyWhy chat models forget, a history passed by hand, RunnableWithMessageHistory with a store of sessions, what the wrapper does on each call, a system prompt with MessagesPlaceholder, extra inputs with input_messages_key, the usual errors, and what replaces these classes today.
- 4Trimming the conversationWays to keep a chat history small (trim, summarise, retrieve, remember facts), trim_messages and its parameters, how tokens are counted, a trimmer inside a chain and inside the history wrapper, why the store keeps everything while the model sees a window, and where to trim.
- 5A chatbot in one scriptA complete chatbot with memory in one script: prompt with a placeholder, trimmer, LCEL chain, history wrapper and streamed answers, how to run it, and which line changes for each alternative.