Chatbot in Under 5 mins using Embedchain

Ayush Wattal
3 min readJul 11, 2023

--

Are you thinking about developing a chatbot? In this tutorial, we will walk you through the steps of building a ChatBot with the Embedchain framework. An adaptable infrastructure for developing chatbots that can be customized for different domains is offered by the Embedchain library. We will concentrate on developing an Audi chatbot in this lesson. We’ll go over installing the Embedchain library, setting the OpenAI API key, building the chatbot instance, giving it resources to train with, and asking the chatbot questions to get answers.

Step 1: Installing the Embedchain Library

First, we need to install the Embedchain library, which provides the necessary tools to create the ChatBot. Open a code cell in your Jupyter Notebook and run the following command to install the library:

!pip install -q embedchain

Step 2: Setting the OpenAI API Key

Next, we need to set up the OpenAI API key. We can access powerful language models offered by OpenAI thanks to the API key. We can import the API key from a different module or explicitly assign it as an environment variable to set the API key. Here is an illustration of how to use an imported module to set the API key:

import os
import key

os.environ['OPENAI_API_KEY'] = key.OPENAI_API_KEY

Make sure to replace key.OPENAI_API_KEY it with the actual value of your OpenAI API Key.

Step 3: Importing the Required Modules and Selecting the ChatBot Type

In this step, we import the necessary modules from the Embedchain library and select the type of ChatBot we want to create. There are three types of ChatBots available in Embedchain: App(uses OpenAI models, paid), OpenSourceApp (uses open-source models, free), and PersonApp (uses OpenAI models, paid).

Now, let’s create an instance of the ChatBot using the App class from the Embedchain library:

Audi_ChatBot = App()

Step 4: Training the Chatbot

To make our ChatBot knowledgeable and capable of answering questions, we need to train it with relevant resources. The Embedchain library allows us to embed both online and local resources. Here’s an example of how to embed online resources:

Audi_ChatBot.add("youtube_video", "https://www.youtube.com/watch?v=LmjdUjvJL1E")
Audi_ChatBot.add("web_page", "https://www.audiusa.com/us/web/en/models/a3/a3/2023/overview.html")

In this case, we’re embedding a YouTube video and a web page URL related to Audi A3. This will provide the ChatBot with information to respond to user queries.

We can also embed local resources, such as PDF files. Here’s an example:

Audi_ChatBot.add_local("pdf_file", "/content/data/2020-audi-a3-24.pdf")

This adds a local PDF file to ChatBot’s training resources.

Step 5: Querying the Chatbot

Now that our ChatBot is trained, we can interact with it by asking questions and getting responses. Let’s see an example of how to query the ChatBot and receive a response:

result = Audi_ChatBot.query("What can adaptive cruise control do?")
print(result)

In this example, we’re asking the ChatBot about the capabilities of adaptive cruise control in Audi vehicles. The ChatBot will analyze the query and provide a response based on the embedded resources it has been trained on.

Entire Code

# Step 1: Install the Embedchain library
!pip install -q embedchain

# Step 2: Set the OpenAI API Key
import os
import key

os.environ['OPENAI_API_KEY'] = key.OPENAI_API_KEY

# Step 3: Import the necessary modules
from embedchain import App

# Step 4: Create a ChatBot instance
Audi_ChatBot = App()

# Step 5: Train the ChatBot with resources
Audi_ChatBot.add("youtube_video", "https://www.youtube.com/watch?v=LmjdUjvJL1E")
Audi_ChatBot.add("web_page", "https://www.audiusa.com/us/web/en/models/a3/a3/2023/overview.html")
Audi_ChatBot.add_local("pdf_file", "/content/data/2020-audi-a3-24.pdf")

# Step 6: Query the ChatBot
result = Audi_ChatBot.query("What can adaptive cruise control do?")
print(result)

Additional Features

Additionally, we can use other data formats to train the chatbot.

  • Embed docx directly:
Audi_ChatBot.add("docx", "path_to_your_docx_file")
  • Embed text directly:
Audi_ChatBot.add_local("text", "Audi A3 has many features.")
  • Embed a QnA pair:
Audi_ChatBot.add_local("qna_pair", ("Question", "Answer"))

These examples demonstrate how versatile the Embedchain library is for training the ChatBot with different types of resources.

References:-

--

--

Ayush Wattal

Data-Driven Software Engineer | Experienced in ML, Data Science and Data Engineer