Following my highly esteemed Squad colleague's article, Tristan, along with his constant sharing of discoveries about the framework at Reboot Conseil since he first took an interest in it (and I sincerely thank him for that), I decided to finally roll up my sleeves and explore the components of this incredible tool.
Tristan had written in his previous article:
However, despite its remarkable potential, LangChain's complexity can pose challenges, particularly for those less versed in advanced programming.
Tristan, highly esteemed AI squad member
I can confirm: it's quite confusing when you first dive in, but as with many tools and concepts in our profession as developers, it eventually "clicks" at some point.
Definition of "click": a minimal but sufficient understanding of a critically important concept, which triggers a sudden wave of dopamine in the brain that's been trying to understand said concept.
That moment arrived today for me, so I'm taking advantage of my current excitement to summarize LangChain's potential from my perspective as a developer, which I glimpsed during my exploratory journey through its documentation and various relevant Jupyter notebooks.
LangChain is a framework that presents itself as a "toolkit for building context-aware and intelligent applications using abstractions."
What does that mean in practice? Well, with Python or JavaScript, you can use classes, methods, and various pipelines and configurations to build applications that make extensive use of LLMs, and now LMMs.
Take the LLM you're familiar with, say GPT-4, and turn it into an "agentic" and "context-aware" application (these are the terms you'll often encounter when discussing autonomous agents).
An AI is said to become agentic when it's capable of understanding, reasoning, and using specific data to execute an action that makes sense to it.
A "context-aware" system is able to retrieve data on demand from various data sources or other systems based on the user's input. This is essentially what software developers have been doing for years, but in the context of LangChain, it means you can plug one or more systems or data sources, or even other LLMs, into the system to provide a more precise and timely response to the user.
Augmenting an LLM (GPT-4, Mistral, Gemini, Llama, etc.) by giving it access to files, databases, other LLMs, applications, networks, and more of your choosing
Enabling LLMs to interact with their environment by giving them the ability to make decisions
Using LLMs to decide which action to take in a given task pipeline
A list of the best LangChain integrations is maintained in this GitHub repository, and trust me, it's impressive!
As you've gathered, LangChain lets you augment systems with LLMs by offering all sorts of tools to do so. Its modular architecture facilitates building complex LLM-based applications by making it easy to connect models to various data sources and decision-making agents.
Among these modules, you'll find:
Agents: abstractions that allow LLMs to communicate with their environment and make decisions
Chains: sequences of actions that an agent can execute
Models: interfaces for different types of AI models, including LLMs, chat models, text embedding models, etc.
Documents: unstructured texts with arbitrary metadata
etc.
It's by combining these modules, along with many others available, that you can build your agentic applications.
You'll find this notebook with my LangChain experiments, where I try out several framework components using PaLM2 (Google's last LLM before Gemini) as the underlying LLM.
The entire setup section requires a Google Cloud project and a .env file, so take a look at the repo and update the values specific to your project 😉
We'll mainly focus here on a few chain use cases:
A sequential chain => successive calls to one or more LLMs where, with each call, the result of the previous call serves as input (or the initial prompt if it's the first call)
A summarization chain => an automation of successive document summaries, regardless of their length
A question/answering chain => the idea here is to ask a set of questions to an LLM with explicit instructions to only consider a designated data source
As you'll see, chains are an extremely powerful concept (so much so that I believe they represent the natural evolution of our profession as software developers) that enables creating fairly complex interactions without having to write all the underlying code.
As of today, LangChain uses LCEL (LangChain Expression Language) to help developers define these interactions. Let's start with the sequential chain:
The sequential chain is relevant for many (if not all) use cases: you take the output of an LLM and generate another output with the same or a different LLM. In my notebook example:
I create a prompt template (another LangChain abstraction) to give the LLM the persona of a playwright who writes children's story synopses from a title input
I create another prompt template, this time with the persona of a bot representing a dark, tortured writer
I then chain the two dynamic prompts and their results to obtain a final output that's a somewhat dark, Tim Burton-esque story (which is quite delightful)
This simple example can be made infinitely more complex and, as it's the holiday season, I got carried away with this example ^^ I should note, however, that it's perfectly relevant to envision all sorts of business pipelines in a more serious work context (even though we have a blast in the AI squad).
Creating a sequential chain with LangChain
In this example, we can see how easy it is to "pipe" elements into a chain of LLM interactions. Here I use RunnablePassThrough to be able to retrieve the steps before the final output.
In this context, creating an LLM instance (for example, the one used in the chain) is also made very easy =>
Creating an LLM instance of your choice with LangChain
Synopsis: A Cute Christmas Tale
It's Christmas Eve, and the young reindeer, Prancer, is feeling very excited. He can't wait to help Santa deliver presents to all the good little boys and girls around the world. But when he arrives at the North Pole, he discovers that Santa is missing! Prancer is determined to find Santa and save Christmas. He sets off on a journey, meeting all sorts of interesting characters along the way, including a wise old owl, a friendly snowman, and a mischievous elf. As Prancer gets closer to finding Santa, he learns the true meaning of Christmas. He discovers that it's not about presents or decorations, but about love, kindness, and helping others. In the end, Prancer finds Santa and they deliver the presents together. It's a Christmas that Prancer will never forget!
PaLM2 in cute mode
A Dark Christmas Tale
It's Christmas Eve, and the young reindeer, Prancer, is feeling very excited. He can't wait to help Santa deliver presents to all the good little boys and girls around the world. But when he arrives at the North Pole, he discovers that Santa is missing! Prancer is determined to find Santa, but he soon realizes that the North Pole is not as magical as he thought. The elves are overworked and exhausted, the reindeer are restless and hungry, and the air is filled with a sense of dread. As Prancer searches for Santa, he uncovers a dark secret that threatens to destroy Christmas. He learns that Santa has been kidnapped by the evil Krampus, a demon who punishes naughty children. Krampus is planning to use Santa's magic to spread fear and darkness throughout the world. Prancer must find a way to stop Krampus and save Santa before it's too late. He faces many challenges along the way, but he is determined to succeed. In the end, Prancer triumphs over Krampus and saves Christmas. But the experience has left him with a dark secret of his own. He knows that the world is not as innocent as he once thought, and that there are forces of evil that are always lurking in the shadows.
PaLM2 a touch darker
We've all needed, at some point in our workday, to summarize textual content. The documents to summarize can come from different sources and be of different natures. LLMs are a powerful tool for boosting our productivity in this regard, as they're very versatile when it comes to this task.
However, although the technology is constantly evolving (notably with 32K+ token context windows), we still need to be mindful of the context limit an LLM can absorb when feeding it documents to summarize.
LangChain makes this task, and the associated context management, very easy. In the example below:
I take two related homepages: the Reboot Conseil website and this blog
I then merge the textual content from these two sites
This merged content is then split into tokens that can be ingested in a single LLM call (the length varying depending on the model used)
I then create a prompt template instructing the bot to iteratively take the current textual content and the previous summary to generate a final summary => the length of successive documents is no longer an obstacle to generating relevant summaries
Here are the steps mentioned above =>
It's that simple to create an object that scrapes a website with LangChain
We split the content from scraped pages to stay within the token limit
Instructions for summarizing successive documents
The result is very conclusive, as you'll see in the notebook: you can orchestrate LangChain and any LLM to create a next-gen web scraper with very few lines of code!
Finally, in our last example, we'll reuse the same scraped content very easily thanks to LangChain's classes, and ask a question about what was scraped.
In the notebook, I use FAISS, an efficient vector search library, to retrieve relevant information from the scraped content, which was previously transformed into embeddings (a refresher on embeddings is available in this article) =>
Connecting our knowledge base with the LLM
Questions and answers: the LLM performs its role very well
With these three small use cases that merely scratch the surface of what LangChain has to offer, you quickly realize the value of developing expertise with this framework, which currently dominates the field of LLM orchestration. Have an idea? A project? Don't hesitate to reach out, and we'll be happy to build amazing intelligent agents for you 🤖
CTO de la scale-up LAMALO, Yacine est un développeur fullstack qui ne tient pas en place : JavaScript, Node.js, Python, LLM, voice UX... Toujours en veille, il transforme les dernières innovations en solutions concrètes !
LinkedInGet our best articles every month.
Le premier produit propre de Reboot Conseil. Une solution innovante née de la collaboration.
ProjectCréer une plateforme IA accessible sur web et mobile. Un projet combinant orchestration IA et mobilité.
ProjectRéduire le délai de conception bijoutière de 8 jours à 20 minutes grâce à l'IA générative et la modélisation 3D.
TrainingMaîtrisez les APIs, intégrez l'IA dans vos applications. Embeddings, fine-tuning, function calling.
ServiceFormateurs opérationnels. IA, data science, développement web. Certifié Qualiopi.
ArticlePère Castor, raconte-moi N8N N8N (prononcez « n-huit-n » ou « nodemation » si vous voulez faire classe). C'est un outil qui permet de connecter vos...