top of page

Advanced Chatbot: Using LangChain and LLM


Introduction

In this blog we will explore how we can build a chatbot that remembers previous conversations in technical terms we call it context-aware chatbots. In our previous blog we explored how we can finetune a LLM to build a chatbot for customer queries (Check out here). One of the limitations of the chatbot we build is that the Chatbot will respond only to the current user query and will not have information regarding the chat history of the session. Now let us have a look at the components we are going to use in this ChatBot

  • OpenAI API :- The OpenAI API allows developers to access and use LLMs that are developed OpenAI, Currently some best performing LLMs are available using this API, but since these models are closed source and only can be used by using the API, for which we have to pay a very nominal cost.

  • LangChain Agents :- LangChain Agents are a type of chatbot agent that is powered by the OpenAI API. They are made up of a number of components, including a tokenizer, a language model, and a memory. The tokenizer is responsible for breaking down the user's input into a sequence of tokens. The language model is responsible for generating text, and the memory is responsible for storing information about the conversation.

  • LangChain Memory :- LangChain Memory is a type of memory that is specifically designed for chatbots. It allows chatbots to store information about the conversation, such as the user's input, the chatbot's responses, and the context of the conversation. This information can be used by the chatbot to generate more natural and engaging conversations, and to learn and adapt over time.

  • LLMChain :- LLMChain is a framework that makes it easy to build chatbots that use the OpenAI API, LangChain Agents, and LangChain Memory. It provides a number of features that make it easier to develop chatbots, such as

    • A simple API for creating and managing chatbot agents

    • A variety of pre-trained LLMs that can be used for chatbots

    • A built-in memory system for storing information about the conversation

    • A variety of tools and resources for developers


Create the Chatbot

Now let us start with the creating the chatbot for this blog, before we proceed with the required libraries, we have to make sure to install the required libraries and it is recommended to have to create a new virtual environment to avoid any potential error.


1. Create virtual environment :

python -m venv advanced_chatbot

2. Activate the environment :

advanced_chatbot\Scripts\activate.bat

3. Install the required libraries :

pip install openai langchain

4. Import the Required Libraries and set the OpenAI API Key :

import openai
import os

# LangChain Libraries
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
os.environ['OPENAI_API_KEY'] = ""

5. Set Up the Chatbot : -

  • Define the LLM using the OpenAI from the LangChain and define the model name

  • Now Define the Memory Object using the conversational buffer memory from the LangChain

  • Set the Context of Chatbot by appending the input and output to the memory

  • Now define the Conversation Chain and specify the LLM and the memory object then also set verbose to True so that we can see the Chat History directly without having to print it out separately

Now we shall ask the chatbot with some user queries in and see how it replies.

It was able to answer the query and we are able to see the previous conversations as well, now let us ask a new question and see if it able to answer it and see it the chat history works properly

As you can see the Chat History works properly and the ChatBot is also able to answer the user query with Chat History data available to the LLM.


I Hope you have understood how to use LangChain and Build ChatBots using OpenAI API.

Some of the other courses which might interest you :-

If you want to any assistance in such projects or require consultation for developing such applications feel free to contact us at contact@codersarts.com








bottom of page