Building Chatbot using Langchain and ChatGPT

Building Chatbot using Langchain and ChatGPT

LangChain is changing the game when it comes to building powerful conversational AI applications. Its advanced tools and features empower businesses to create innovative and effective solutions for a wide range of use cases. In this blog post, we'll explore some of the most exciting use cases for LangChain in building Chatbots. We'll also share best practices for developing successful conversational AI applications using LangChain's framework, providing developers with the guidance they need to take their projects to the next level. Join us as we dive into the world of LangChain and discover how this framework is revolutionizing the world of conversational AI.

Langchain

LangChain is an all-encompassing framework that enables developers to build robust applications using large language models (LLMs) and chat models. It provides a comprehensive suite of tools, interfaces, and components that simplify the process of developing end-to-end applications powered by cutting-edge AI technology. With LangChain, managing interactions with LLMs and chaining multiple components together has never been easier. Moreover, the framework offers seamless integration with various resources, including APIs and databases, making it a versatile solution for a broad range of use cases. Whether you're developing conversational AI applications for customer service, virtual assistants, or other use cases, LangChain provides a powerful set of features to help you create innovative and effective solutions.

Key Features:

Here are the six main areas where LangChain supports developers:

  1. LLMs and Prompts: LangChain simplifies prompt management, optimizing them, and creating a universal interface for all LLMs. It also includes useful utilities to work with LLMs efficiently.

  2. Chains: These are sequences of calls to LLMs or other utilities. LangChain offers a standard interface for chains, integrates with various tools, and provides end-to-end chains for popular applications.

  3. Data Augmented Generation: LangChain enables chains to interact with external data sources to gather data for the generation step. This functionality can help with tasks such as summarizing long texts or answering questions using specific data sources.

  4. Agents: LangChain's agents allow LLMs to make decisions about actions, take those actions, check the results, and continue until the job is complete. It provides a standard interface for agents, a variety of agents to choose from, and end-to-end agent examples.

  5. Memory: LangChain's standard memory interface helps maintain state between chain or agent calls. It also offers a range of memory implementations and examples of chains or agents that use memory.

  6. Evaluation: LangChain recognizes that traditional metrics can be inadequate for evaluating generative models. As a result, it provides prompts and chains that use LLMs to help developers assess their models effectively.

Powering ChatBots with Langchain

Chatbots have been around for a while now and have proven to be a powerful tool for businesses to enhance customer service, automate tasks, and increase efficiency. However, developing a chatbot can be a complex and challenging task, especially when it comes to creating a natural language interface. That's where LangChain comes in.

LangChain simplifies the process of creating chatbots powered by large language models (LLMs). This allows businesses to create chatbots that can understand and respond to user input in a natural and human-like way. LangChain also includes agents that enable LLMs to make decisions about actions, take those actions, check the results, and keep going until the job's done. This helps chatbots to function more efficiently and effectively, providing users with faster and more accurate responses.

Building a Language Model Application: Chat Models

Chat model APIs are still relatively new, and developers are continually exploring the best abstractions to optimize chat model performance. By utilizing LangChain, developers can easily manage interactions with chat models, integrate additional resources such as APIs and databases, and chain together multiple components to create end-to-end chatbot applications.

Here’s an example of using chat models:

#install dependencies
pip install langchain
pip install openai

Using the Pip package manager to download and install the LangChain and OpenAI library and their dependencies on your local machine.

#Authenticate with OpenAI
import openai
import os
os.environ["OPENAI_API_KEY"]="YOUR API KEY"

Since LangChain uses OpenAI's models for its chat models, it's necessary to authenticate with OpenAI's API by providing an API key. The API key is a unique identifier that grants access to OpenAI's language models, enabling developers to use them to power their chatbots and other language-based applications.

from langchain.chat_models import ChatOpenAI
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

ChatOpenAI is a class within the chat_models module of LangChain. It provides an interface for working with OpenAI's language models to power chatbots. By using the ChatOpenAI class, developers can easily integrate OpenAI's language models into their chatbots and other conversational applications.

The AIMessage, HumanMessage, and SystemMessage classes are data models defined in the schema module of LangChain. These models represent different types of messages that may be exchanged in a chatbot conversation. AIMessage represents a message generated by the chatbot's AI, HumanMessage represents a message sent by the user, and SystemMessage represents a message sent by the chatbot to provide information or assistance.

chat = ChatOpenAI(temperature = 0)

Create an instance of the ChatOpenAI class from the LangChain library. By setting the temperature to 0, the chatbot will always generate the same response to a given user message (deterministic response). This may be useful in certain applications where consistency and predictability are important, such as customer support or technical assistance chatbots.

chat([HumanMessage(content="Generate a Product Description for Apple Iphone 15")])

This line of code is a function call to a chatbot. Specifically, it's asking the chatbot to generate a product description for the Apple iPhone 15. It generates a product description as follows:

AIMessage(content="The Apple iPhone 15 is the latest addition to the iconic iPhone series, designed to provide an unparalleled user experience. With a sleek and stylish design, this smartphone boasts a 6.7-inch OLED display that offers stunning visuals and vibrant colors. The device is powered by the latest A16 Bionic chip, which delivers lightning-fast performance and seamless multitasking.\n\nThe iPhone 15 features a triple-lens camera system that includes a 64-megapixel primary lens, a 12-megapixel ultra-wide lens, and a 12-megapixel telephoto lens. This camera setup allows you to capture stunning photos and videos with incredible detail and clarity. The device also features a 32-megapixel front-facing camera that takes stunning selfies and supports Face ID for secure authentication.\n\nThe iPhone 15 comes with a range of advanced features, including 5G connectivity, Wi-Fi 6, and Bluetooth 5.2. It also features a long-lasting battery that provides up to 12 hours of continuous use on a single charge. The device runs on the latest iOS 16 operating system, which offers a range of new features and improvements to enhance your user experience.\n\nOverall, the Apple iPhone 15 is a powerful and feature-packed smartphone that offers an unparalleled user experience. Whether you're a professional photographer, a gamer, or a casual user, this device is sure to meet all your needs and exceed your expectations.", additional_kwargs={})

You can provide additional information as a prompt, such as a constraint on words, vocabulary etc. Thus you can tailor it to your use case, however, you want.

Building a ChatGPT Clone with LangChain

Langchain is so amazing that it can be used to develop a ChatGPT clone for your specific use case. For example, if you want to provide better service to your customers when they visit your website, you can prompt them with an assistant that helps them find what they need. This task becomes simple with Langchain. You can use the following code to build ChatGPT clones with Langchain.

from langchain import OpenAI,ConversationChain,LLMChain, PromptTemplate
from langchain.memory import ConversationBufferWindowMemory

OpenAI: This module provides an interface for integrating with OpenAI's GPT models. It enables you to create an instance of the OpenAI model, generate text with the model, and manage the parameters of the model, such as temperature, length penalty, and more.

ConversationChain: This module provides a class for creating conversation flows. It allows you to define a sequence of prompts and responses, along with any necessary context, and manage the flow of the conversation based on the user's inputs and the bot's responses.

LLMChain: This module provides a class for creating a custom language model based on a pre-trained model, such as GPT3. It enables you to fine-tune the model on your own dataset, so that it can generate responses that are specific to your use case.

PromptTemplate: This module provides a class for defining prompts and generating new prompts based on templates. It allows you to define placeholders in your prompts, such as {name} or {product}, and generate new prompts by filling in these placeholders with specific values.

ConversationBufferWindowMemory: This module provides a class for creating a memory buffer that stores the most recent messages in a conversation. It enables you to maintain context between messages in the conversation so that you can generate more coherent and relevant responses.

template="""Assistant is a large language model trained by OpenAI.

Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.

{history}
Human: {human_input}
Assistant:"""

prompt = PromptTemplate(
    input_variables = ["history","human_input"],
    template = template
)

We create a template, which specifies the role of the model (system role) to act as an AI Assistant. we are providing two input variables history and human_input along with the template as a prompt. The {history} placeholder is intended to be replaced with a string containing the conversation history, and the {human_input} placeholder is intended to be replaced with a string containing the user's most recent input.

chatgpt_chain = LLMChain(
    llm = OpenAI(temperature=0),
    prompt = prompt,
    verbose = True,
    memory = ConversationBufferWindowMemory(k=2),
)

We create a LLMChain instance that uses an OpenAI language model with a temperature of 0 to generate text. The LLMChain instance is configured to use the prompt template that we defined earlier and to print debugging information to the console. It also uses a ConversationBufferWindowMemory instance to store a fixed-size window of the conversation history, In our case we set it to 2.

output = chatgpt_chain.predict(human_input="I want you to act as an NLP Tutor. I will ask you questions regarding Natural Language Processing and you will answer like you explain it to a teenager. Be specific, Do not write explanations. My first question to you is what are stopwords? should we remove them when doing text generation tasks?")
print(output)

Here we are specifying that we want the model to act as an NLP tutor, where we will be asking a NLP question/doubt and the model will briefly give us an answer. Take a look at the generated output.

"""> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.

Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.

Human: I want you to act as an NLP Tutor. I will ask you questions regarding Natural Language Processing and you will answer like you explain it to a teenager. Be specific, Do not write explanations.
AI: 

Sure, I'd be happy to help! Natural Language Processing (NLP) is a field of computer science that focuses on understanding and processing human language. It involves using algorithms and techniques to analyze and interpret text, speech, and other forms of natural language. NLP can be used to build applications such as chatbots, automated customer service agents, and language translation systems.
Human: I want you to act as an NLP Tutor. I will ask you questions regarding Natural Language Processing and you will answer like you explain it to a teenager. Be specific, Do not write explanations. My first question to you is what are stopwords? should we remove them when doing text generation tasks?
Assistant:

> Finished chain."""

As soon as we run the code we get this message where it creates a LLM chain where it has substituted the variables defined earlier with actual values. Immediately, it is followed by the output as follows:

"""Stopwords are words that are commonly used in a language but don't carry much meaning. Examples of stopwords include "the", "a", "an", and "of". When doing text generation tasks, it is usually best to remove stopwords as they don't add much value to the generated text."""

Now let's try asking something interesting:

output = chatgpt_chain.predict(human_input="Write regex to remove http: urls from text")

The output obtained is as follows:

"""The following regular expression can be used to remove http: URLs from text: 
\b(?:https?://)?\S+\b"""

So basically it returns a regex that we can use to perform a search using the re module in python. It only generates the required output without any further explanation because of our strict instructions.

Now since we are using ChatMemory this is how our LLM chain looks like:

"""> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.

Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.

Human: I want you to act as an NLP Tutor. I will ask you questions regarding Natural Language Processing and you will answer like you explain it to a teenager. Be specific, Do not write explanations. My first question to you is what are stopwords? should we remove them when doing text generation tasks?
AI:  Stopwords are words that are commonly used in a language but don't carry much meaning. Examples of stopwords include "the", "a", "an", and "of". It is generally recommended to remove stopwords when doing text generation tasks, as they don't add any value to the generated text.
Human: Write regex to remove http: urls from text
AI:  The following regular expression can be used to remove http: URLs from text: 
\b(?:https?://)?\S+\b"""

We can see that along with the human input we just provided it also has a chat history that shows our previous conversations. It also can remember the context of our previous conversations, making it more similar to our ChatGPT, the use cases for this model are endless.

In this blog post, we have explored how Langchain can be used to create basic chat models for a particular use case, as well as how to build a ChatGPT clone that can be used for larger projects. Langchain is a powerful tool that offers endless possibilities with its generative AI capabilities, and it has never been easier to use. With its ability to process and understand large amounts of text, Langchain is changing the game in the world of AI technology. As AI technology continues to evolve, we can expect Langchain to take the world by storm in the coming years.

For more info regarding langchain refer to this video, and follow this channel to get latest updates in the field of NLP:

Follow FutureSmart AI to stay up-to-date with the latest and most fascinating AI-related blogs - FutureSmart AI

Looking to stay up to date on the latest AI tools and applications? Look no further than AI Demos This directory features a wide range of video demonstrations showcasing the latest and most innovative AI technologies. Whether you're an AI enthusiast, researcher, or simply curious about the possibilities of this exciting field, AI Demos is your go-to resource for education and inspiration. Explore the future of AI today with aidemos.com