Making sense of AI agents and LangChain
An intro to how LangChain can build LLM applications using AI agents
It all started with a humble, yet magical text box. ChatGPT, for me and many, was the first foray into the future of AI. While it’s very, very cool, it is a bit unclear how and if applications that we build and experience can start to build upon this.
Will everything be a text box into a single all-consuming app?
Likely not.
The US continues to rage against the super app, and even ChatGPT’s creators say its plugins aren’t making as much product market fit as they like in a (now deleted) interview.
“Plugins “don’t have Product Market Fit (PMF)” and are probably not coming to the API anytime soon.
So what is next, and how will AI be embedded into our apps? Enter AI agents and, in particular, LangChain.
Get to know LangChain
LangChain is an open-source framework for LLM app development. LangChain seems a promising and downright essential component to building AI into our experiences more broadly and offers a glimpse of this future already.
Let’s start with understanding the basics of the framework and how we can use it as the building blocks for building an app–prompts, chains, and agents.
Prompts
When you enter something into ChatGPT you’re giving the AI a prompt.
LangChain begins with the same concept of a prompt and make it easier to take user input and have a defined prompt around it. For instance, instead of an empty text field to start, you may have a dadjoke AI app. You may want to ask the user ‘what do you want a joke about’? The user inputs `food`.
LangChain helps you take `food` and use it within a set prompt.
const template = "What is a good dad joke about {subject}?";
const prompt = new PromptTemplate({ template, inputVariables: ["subject"] });
// We can now use our `template`with a given input.
const response = await prompt.format({ subject: "food" });
console.log({ response });
Chains
LangChain takes the concept of a prompt and pushes it further to let the computer intelligently string multiple prompts together. This string of prompts is called a chain.
A chain can take many shapes and perform many actions. A chain can use tools to perform tasks, like querying a search engine or solving math equations.
Agents
More advanced apps using chains may need to rely on the AI more for deciding which tools to use. For instance, an app could have multiple databases or APIs to determine the best option for performing a task. This decision-making is handled by agents.
Chains without an agenet have a defined set of steps available to them–get input, query API. Agents are more robust, in that they are problem solvers. An agent can work through multiple steps of a problem, and continuously ask itself if the task is achieved.
The example in the docs shows a process of how an agent solves finding a shirt with reason.
A simple example
Langchain is a framework to let developers do all these things surprisingly easy. Let me show instead of tell.
I’ve created a basic example for you to try out the weather chain yourself. To run it, all you’ll need to do is:
Have a replit account
Fork the LangChain example repl
Get an OpenAI API key
Add the key to the forked repl–you can do so as using ‘secrets’ in the tools menu.
Click ‘run’
The impact of AI agents
So what does this mean for most of us? How could something like LangChain be brought into the apps I use, or how could I build an app with it?
My immediate takeaway is that we’re seeing the ability to automate digital workflows completely. While some of this in the past–like checking the weather–could have been done with a simple Python script, AI opens up the possibilities for everyone to be able to immediately and repeatedly have the power to have a custom Python script for whatever they need practically on demand.
Personally, I’m interested in exploring this for all of the monotonous, repeated tasks in a marketing context. Most people can use generative AI for writing that piece of content, but could we create flexible yet AI-automated workflows for what happens after that? Editing, UTM links, metadata, updating existing content with the new link to the post, localization? I’m early into understanding and exploring this with Langchain, but it is promising!
Proactive AI
Beyond downstream AI automation, it’s conceivable that we’ll see the next stage of no-code and AI assistants. To date, we’ve seen Siri and Alexa be reactive technology. We are entering an era where these assistants become proactive.
Imagine a user can define the type of information or experience they want to be surfaced to them. The AI will then check if an existing agent already exists and, if not, dynamically create and deploy one. Just like that.
I’d like to see this in a something-like bento.me experience. Giving everyone an insanely custom dashboard for you–your critical notifications, a reminder to buy that birthday present for your friend and a suggestion of a perfect gift, that season of your favorite show being back on, canceling your Netflix subscription, and reactivating Max. When AI assistants move to being reactive and proactive, with the help of tools like LangChain, maybe that will finally unlock the US super app.