How to Train an AI Chatbot With Custom Knowledge Base Using ChatGPT API

In our earlier article, we demonstrated how tobuild an AI chatbot with the ChatGPT APIand assign a role to personalize it. But what if you want to train the AI on your own data? For example, you may have a book, financial data, or a large set of databases, and you wish to search them with ease. In this article, we bring you an easy-to-follow tutorial on how to train an AI chatbot with your custom knowledge base with LangChain and ChatGPT API. We are deploying LangChain, GPT Index, and other powerful libraries to train the AI chatbot using OpenAI’s Large Language Model (LLM). So on that note, let’s check out how to train and create an AI Chatbot using your own dataset.

Notable Points Before You Train AI with Your Own Data1. You can train the AI chatbot on any platform, whetherWindows, macOS, Linux, or ChromeOS. In this article, I’m using Windows 11, but the steps are nearly identical for other platforms.2. The guide ismeant for general users, and the instructions are explained in simple language. So even if you have a cursory knowledge of computers and don’t know how to code, you can easily train and create a Q&A AI chatbot in a few minutes. If you followed our previous ChatGPT bot article, it would be even easier to understand the process.3. Since we are going to train an AI Chatbot based on our own data, it’s recommended touse a capable computerwith a good CPU and GPU. However, you can use any low-end computer for testing purposes, and it will work without any issues. I used a Chromebook to train the AI model using a book with 100 pages (~100MB). However, if you want to train a large set of data running into thousands of pages, it’s strongly recommended to use a powerful computer.4. Finally, the data setshould be in Englishto get the best results, but according to OpenAI, it will also work with popular international languages like French, Spanish, German, etc. So go ahead and give it a try in your own language.

Set Up the Software Environment to Train an AI Chatbot

Set Up the Software Environment to Train an AI Chatbot

Install Python and Pip

  1. First off, you need toinstall Python along with Pipon your computer by following our linked guide. Make sure to enable the checkbox for “Add Python.exe to PATH” during installation.

  2. To check ifPython is properly installed, open the Terminal on your computer. Once here, run the below commands one by one, and it will output their version number. On Linux and macOS, you will have to usepython3instead ofpythonfrom now onwards.

  3. Run the below command toupdate Pipto the latest version.

Install OpenAI, GPT Index, PyPDF2, and Gradio Libraries

  1. Open the Terminal and run the below command toinstall the OpenAI library.

  2. Next, let’sinstall GPT Index.

  3. Now,install Langchainby running the below command.

  4. After that,install PyPDF2and PyCryptodome to parse PDF files.

  5. Finally,install the Gradio library. This is meant for creating a simple UI to interact with the trained AI chatbot.

Download a Code Editor

Finally, we need a code editor to edit some of the code. On Windows, I would recommendNotepad++(Download). Simply download and install the program via the attached link. You can also use VS Code on any platform if you are comfortable with powerful IDEs. Other than VS Code, you can install Sublime Text (Download) on macOS and Linux.

For ChromeOS, you can use the excellentCaretapp (Download) to edit the code. We are almost done setting up the software environment, and it’s time to get the OpenAI API key.

Get the OpenAI API Key For Free

  1. Head to OpenAI’s website (visit) and log in. Next, click on “Create new secret key” and copy the API key. Do note that you can’t copy or view the entire API key later on. So it’s recommended to copy and paste the API key to a Notepad file for later use.

  2. Next, go toplatform.openai.com/account/usageandcheck if you have enough credit left. If you have exhausted all your free credit, you need to add a payment method to your OpenAI account.

Train and Create an AI Chatbot With Custom Knowledge Base

Train and Create an AI Chatbot With Custom Knowledge Base

Add Your Documents to Train the AI Chatbot

  1. First,create a new folder calleddocsin an accessible location like the Desktop. You can choose another location as well according to your preference. However, keep the folder namedocs.

  2. Next, move the documents for training inside the “docs” folder. You canadd multiple text or PDF files(even scanned ones). If you have a large table in Excel, you can import it as a CSV or PDF file and then add it to the “docs” folder. You can also add SQL database files, as explained in thisLangchain AI tweet. I haven’t tried many file formats besides the mentioned ones, but you can add and check on your own. For this article, I am adding one of my articles onNFTin PDF format.

Note:If you have a large document, it will take a longer time to process the data, depending on your CPU and GPU. In addition, it will quickly use your free OpenAI tokens. So in the beginning, start with a small document (30-50 pages or < 100MB files) to understand the process.

Make the Code Ready

  1. Now, open acode editor like Sublime TextorlaunchNotepad++and paste the below code. Once again, I have taken great help fromarmrrson Google Colaband tweaked the code to make it compatible with PDF files and create a Gradio interface on top.

  2. Next, click on “File” in the top menu and select “Save As…” . After that, set the file nameapp.pyand change the “Save as type” to “All types”. Then, save the file to the location where you created the “docs” folder (in my case, it’s the Desktop).

  3. Make sure the “docs” folder and “app.py” are in thesame location, as shown in the screenshot below. The “app.py” file will be outside the “docs” folder and not inside.

  4. Come back to the code again in Notepad++. Here, replaceYour API Keywith the one that you generated above on OpenAI’s website.

  5. Finally, press “Ctrl + S” to save the code. You are now ready to run the code.

Create ChatGPT AI Bot with Custom Knowledge Base

  1. First, open the Terminal and run the below command tomove to the Desktop. It’s where I saved the “docs” folder and “app.py” file.

  2. Now, run the below command.

  3. It willstart indexing the documentusing the OpenAI LLM model. Depending on the file size, it will take some time to process the document. Once it’s done, an “index.json” file will be created on the Desktop. If the Terminal is not showing any output, do not worry, it might still be processing the data. For your information,it takes around 10 seconds to process a 30MB document.

  4. Once the LLM has processed the data, you will find alocal URL. Copy it.

  5. Now, paste the copied URL into the web browser, and there you have it. Your custom-trained ChatGPT-powered AI chatbot is ready. To start, you can ask the AI chatbotwhat the document is about.

  6. You can ask further questions, and the ChatGPT bot willanswer from the data you providedto the AI. So this is how you can build a custom-trained AI chatbot with your own dataset. You can now train and create an AI chatbot based on any kind of information you want.

Manage the Custom AI Chatbot

  1. You can copy thepublic URLand share it with your friends and family. The link will be live for 72 hours, but you also need to keep your computer turned on since the server instance is running on your computer.

  2. Tostop the custom-trained AI chatbot, press “Ctrl + C” in the Terminal window. If it does not work, press “Ctrl + C” again.

  3. Torestart the AI chatbotserver, simply move to the Desktop location again and run the below command. Keep in mind, the local URL will be the same, but the public URL will change after every server restart.

  4. If you want to train the AI chatbot withnew data, delete the files inside the “docs” folder and add new ones. You can also add multiple files, but make sure to add clean data to get a coherent response.

  5. Now,run the code againin the Terminal, and it will create a new “index.json” file. Here, the old “index.json” file will be replaced automatically.

  6. To keep track of your tokens, head over to OpenAI’s onlinedashboardand check how much free credit is left.

  7. Lastly, you don’t need totouch the codeunless you want to change the API key or the OpenAI model for further customization.

Arjun Sha

Passionate about Windows, ChromeOS, Android, security and privacy issues. Have a penchant to solve everyday computing problems.

Add new comment

Name

Email ID

Δ

01

02

03