How to create a simple conversational AI chatbot app using Openai and Langchain

Okolie Chukwuka
4 min readMay 16, 2023

--

Introduction:

In recent years, artificial intelligence (AI) has gained immense popularity and has become an integral part of various applications. One fascinating application of AI is the creation of chatbots, which can simulate human-like conversations and provide instant assistance to users. In this blog post, we will explore how to build a simple AI chatbot using Python, OpenAI, Langchain, and Panel. By the end of this tutorial, you’ll have a functional chatbot that can engage in meaningful conversations.

Table of Contents:

  1. What is an AI Chatbot?
  2. Understanding the Technologies
  • Python
  • OpenAI
  • Langchain

3. Setting Up the Development Environment
4. Create the chatbot LLM model
5. Build the User Interface with a Panel
6. Deploying and Testing the Chatbot

What is an AI Chatbot?

An AI chatbot is a computer program that uses natural language processing (NLP) techniques and AI algorithms to engage in conversations with users. It can understand user queries, provide appropriate responses, and even simulate human-like conversations. AI chatbots have numerous applications, including customer support, virtual assistants, and information retrieval systems.

Understanding the Technologies:

  • OpenAI: OpenAI is an organization that provides advanced AI models and tools, including the GPT-3 language model used for generating human-like text.
  • Langchain: Langchain is a Python library that simplifies the integration of Large language models, making it easier to interact with them.
  • Panel: Panel is a Python library for building interactive web-based user interfaces (UIs) and dashboards.

Setting Up the Development Environment:

To get started, you’ll need to set up your development environment. Create a virtual environment, and install the requirements file. Let's get started with that.

#create a virtual environment
!python -m venv chatbot_env

#activate virtual environment
virtualenv chatbot_env\Scripts\activate

#install requirements file
!pip -r install requirements.txt

Create the chatbot LLM model

To create the chatbot, we import the necessary libraries and initialize our OpenAI key. We utilize the ConversationChain API class in Langchain to facilitate the conversation.

from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate
)
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory

# LLM Model
def chat_bot(input):
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI engages in meaningful dialogue and provides specific details from its context. If the AI doesn't have an answer to a question, it truthfully acknowledges its lack of knowledge."),
MessagesPlaceholder(variable_name="history"),
HumanMessagePromptTemplate.from_template(input)
])

llm = ChatOpenAI(temperature=0)
memory = ConversationBufferMemory(return_messages=True)
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)
response = conversation.predict(input=input)

return response

Build the User Interface with a Panel

To provide a user-friendly experience, we create a web-based user interface using the Panel library. Design the UI with input fields for users to enter their queries and display the chatbot’s responses in real time.

panels = [] # collect display 

pn.extension('texteditor', template="bootstrap", sizing_mode='stretch_width')
pn.state.template.param.update(
main_max_width="690px",
header_background="green",
title='Conversational Chatbot Application'
)

#Widgets
openaikey = pn.widgets.PasswordInput(
value="", placeholder="Enter your OpenAI API Key here...", width=300,
)
inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…', toolbar=False, height=50, width=500)
button_conversation = pn.widgets.Button(name="Chat!", button_type='primary')

spacer = pn.Spacer(width=100)

#Message function
def collect_messages(_):
os.environ["OPENAI_API_KEY"] = openaikey.value
prompt = inp.value_input
inp.value = ''
if prompt:
response = chat_bot(input= inp.value_input)
panels.append(
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
panels.append(
pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))

return pn.Column(*panels)

#layout
interactive_conversation = pn.bind(collect_messages, button_conversation)
pn.Column(
pn.pane.Markdown("""
## \U0001F60A! A friendly Conversational AI Chatbot
1) Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account).
"""
),
pn.Row(inp,spacer,openaikey),
pn.Row(button_conversation, width=200, margin=(5,150)),
pn.panel(interactive_conversation, loading_indicator=True, height=200),
).servable()

Deploying and Testing the Chatbot

Once the chatbot is integrated into the Panel GUI, we deploy the application to a web server or cloud platform, such as Hugging Face. We can then test the chatbot by engaging in conversations and assessing its ability to provide accurate and relevant responses on the deployed platform.

chatbot app with langchain and panel

for easy deployment, you can refer to this link creating an app on huggingface

Conclusion

Building an AI chatbot using Python, OpenAI, Langchain, and Panel opens up a world of possibilities for creating intelligent conversational systems. In this blog post, we explored the step-by-step process of developing a simple chatbot. Remember, the chatbot’s performance can be enhanced by continuously refining the training data and experimenting with different techniques.

By harnessing the power of AI, you can create chatbots that improve customer experiences, automate support systems, and provide personalized assistance to users.

Let me know if you have any ideas or feedback regarding the chatbot implementation. As always, the code is available on HuggingFace.

Until next time ✨

--

--

Okolie Chukwuka

I'm a passionate Machine Learning Engineer, driven by the endless possibilities of data and technology. Let's connect, learn, and make a positive impact.