Image

LangChain: A Python Library for Building NLP Applications

I’m sure we all know ChatGPT right ? It’s a revolutionary chatbot that seems to know just about everything. If you haven’t, then I suggest you signup for the free version over at https://chat.openai.com/

There is one problem with ChatGPT, while it knows a lot of general knowledge, if we ask it about school rules or company rules, it will fail, ChatGPT will still try to answer it to its best knowledge, but still too general. Don’t believe me ? Try it out yourself.

Here is an example of what I get when I ask about Universitas Indonesia rules: ChatGPT Universitas Indonesia Rules

How would we add that rules to ChatGPT ? Transfer-Learning ? Fine-Tuning ? Well, ChatGPT is not open source, so we don’t have access to the model. And yes there is open source GPT-2, but that’s kind of an old model, isn’t it ? After all ChatGPT is very popular after version 3.

There is one simple thing that we can do, and it can do more than just adding school or company rules. It’s called LangChain. It’s a set of tools, all in one package, that makes it easier to build AI based applications, e.g. a chatbot. You just need to know how to use the tools in LangChain to create your chatbot.

Key Concepts: Chains and Agents

To better understand Lang Chain, we have to first learn about two key concepts: chains and agents.

These concepts enable the creation of complex applications capable of performing tasks like web searching, database access, and leveraging the reasoning capabilities of large language models for autonomous tasks.

Chains

Chains Source: XoMEoX, CC BY 2.0, via Wikimedia Commons

Let’s think of chains as a series of “tasks” linked together and it’s designed to achieve a specific goal by performing a series of operations.

A task is specific to doing only one thing and it can be anything from querying a database, accessing an API, or processing data. The output of one task in the chain can serve as the input for the next, enabling the development of sophisticated AI applications by facilitating complex, multi-step operations.

How do chains relates to ChatGPT ? Who is processing the input into and output from the chain ?

Well, input is processed by AI model, usually an LLM (Large Language Model), a term used to identify AI text-model, such as ChatGPT. For simplicity, let’s just say the input is processed by ChatGPT which produces the output. Naturally it will be in the form of text.

Taking the example above on Universitas Indonesia rules, the set of chains can be the first chain will access/read a PDF file containing the rules, the next chain can be search the PDF for rules, the next chain compile the rules and the last chain present the rules in bullet points format.

Can you think of another set of chain that can be used to solve the same thing (find and preset University of Indonesia rules) ?

Agents

LangChain-agent

Source: Medium

Let’s use an analogy for agent in LangChain, it’s much easier to understand. Think of an agent as a person. Let’s use a bank cashier as an example. A bank cashier can accept deposit from a customer, count the money, input the deposit amount along with customer data into the banking system and provide receipt to the customer. Now, if counterfeit money is found, then the cashier can reject the deposit.

Thus, an agent is an autonomous entities capable of performing tasks and making decisions. It can interact with it’s environment, process information, and execute actions. The actions of agents are based on predefined rules or learned behavior, allowing them to adapt to different situations.

Let’s call this bank cashier as cashier agent. Now we want this cashier agent to also handle customer complaints. Can this agent do that ? Yes it can, but, and I must emphasize “but” here, it will be doing two things that are unrelated, making it overly complicated. In this case we usually create a new agent to handle the customer complaints. So there can be multiple agents at work in the same system.

It’s not complicated you say ? Hmm… customer complaints are regularly updated because there can be new rules introduced by the bank that makes the old answer no longer valid or there are new kind of customer complaints due to a new product being introduced. I think it’s better to split the agent.

Thus it’s important to think of agents in the context of the goal for the agent and tasks performed by the agent.

As we can see, agents can range from simple, performing a single task, to complex, capable of managing multiple tasks and making decisions based on context. And agents also play a crucial role in creating sophisticated AI applications with Lang Chain, enabling autonomous and intelligent operations.

Connecting Large Language Models to External Sources

One of the limitations of an AI model is the knowledge is limited only to what it was trained on. With Langchain, we can connect the model to external sources. This ability is what makes LangChain powerful, we can connect the AI model or large language models to a variety of external sources. The actual interaction is quite complex, for now we’ll let LangChain takes care of that and we’ll focus on using LangChain to enhance an LLM capability.

These sources includes: * Search Engine APIs * Application APIs, e.g. Notion * Mathematical functions with Wolfram * LinkedIn * Twitter * and more.

LangChain Star History

Lang Chain’s “Superpowers”

LangChain is growing in popularity, starting with the introduction of ChatGPT back in November 2022 and with the arrival of GPT-4 in March 2023, the number of integrations supported by LangChain is exploded, making it a robust and versatile framework for AI application development.

LangChain Star History

Source: Streamlit

Back to top