Monday, May 8, 2023

AI for Developers - Train your Own Chat GPT/Chat bot with Custom Data and Run on your Computer or Web Hosting Account

With the recent AI boom, many companies are taking advantage of OpenAI integration but are just starting out and are diligently looking for solutions that can help them automate their customer service and sales. By offering features dealing with AI to their customers, they can now provide them with more valuable tools to improve their customer experience and increase sales.

As more businesses look to implement AI-powered chatbots, there is a growing need for developers who can help them with finetuning and customizing their chatbots. By offering this support, potential clients looking for software can differentiate your SaaS from competitors and attract more customers. It is therefore important to stay on top of the latest trends and technologies in the industry to offer solutions that meet the evolving needs of businesses.

In this article, we will learn how to run our own Chat GPT personal assistant on our own computer with the use of an Open AI API key. We provide code for an app that can be built using python. You can download Visual Code and run the app on your computer. I had no experience with Python and was able to jump in on the revolution with ease. Later on, you can learn how to do the same on your web hosting server. The possibilities are endless for what you can do your business or your clients. Create chat bots, chat assistants for you or your clients and more.

What the code does is allow you to place files of your choosing in a folder. The bot will read the contents of the folder and you will be able to ask questions about the contents of any document in the folder. (See a video demonstration of how it works later on below.)

Here's an overview of what the code provided does:
The code snippet provided is a Python script that trains a custom AI chatbot using OpenAI's GPT-3.5 language model. The script also uses Gradio, a Python library for creating customizable web interfaces for machine learning models, to create a user interface for the chatbot.

Before we dive into how the script works, let's take a quick look at some of the main libraries used in the code:

- GPT-3.5: GPT-3.5 is a language model developed by OpenAI, which is a state-of-the-art model for natural language processing (NLP) tasks such as language generation, text completion, and question answering.

- Gradio: Gradio is a Python library that allows you to create custom web interfaces for machine learning models, without requiring any HTML, CSS, or JavaScript knowledge.

- json: The json module in Python is used to encode and decode JSON data.

The first thing we see in the code is the import statements, which bring in the necessary libraries and modules required for the script to function. The `os.environ` statement sets the OpenAI API key, which is required to access the GPT-3.5 language model.

Next, the `construct_index()` function takes in the directory path where the dataset is stored and some hyperparameters such as the learning rate, number of epochs, and chunk size limit. This function constructs a GPTSimpleVectorIndex, which is a vector-based search index for text documents. The GPTSimpleVectorIndex uses the GPT-3.5 language model to generate a response for a given input text. The `llm_predictor` variable defines the language model that will be used to generate the response. The `documents` variable stores the text data from the dataset. Finally, the `index.save_to_disk()` method saves the index to a file named `index.json`. Learn more about this in detail in 15 minutes here.

The `chatbot()` function takes in the input text and uses the `GPTSimpleVectorIndex.load_from_disk()` method to load the previously saved index from the `index.json` file. It then queries the index with the input text to generate a response.

Finally, the `gr.Interface()` function from Gradio is used to create a web interface for the chatbot. The `iface.launch()` method is used to launch the interface in a new tab in the default web browser.

Overall, the code provided is relatively easy to run on a desktop. One simply needs to have the necessary libraries installed and ensure that the dataset is stored in a folder named `docs` within the same directory as the script. Additionally, they will need to obtain an OpenAI API key and replace the placeholder text with their own API key in the `os.environ["OPENAI_API_KEY"] = 'YOUR API KEY HERE'` line of code.

Here's the complete code snippet:
from gpt_index import SimpleDirectoryReader, GPTListIndex, 
      GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import gradio as gr
import sys
import os
import json


def construct_index(directory_path, learning_rate=1e-3, num_epochs=10):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 18000

    prompt_helper = PromptHelper(max_input_size, num_outputs, 

    llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, 

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, 


    return index

def chatbot(input_text):
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    response = index.query(input_text, response_mode="compact")
    return response.response

iface = gr.Interface(fn=chatbot,
                     label="Enter your text"),
                     title="Custom-trained AI Chatbot")

index = construct_index("docs")

Now you may find this useful if you run a web site and you may want to run this code on your hosting server and create directories there to be able to have your Chat bot query folders from there. Your customers can upload their files to folders you create for them if you want to design bots for them. For more info on how to get set up on the web, watch the below video link:

Need help fine-tuning your OpenAI model or setting up a chat bot or assitant for your business/web site? Visit Formilae and inquire about Open AI Fine-Tuning assistance for your model(s) or Chat Bot design.

No comments:

Post a Comment

AI for Developers - Train your Own Chat GPT/Chat bot with Custom Data and Run on your Computer or Web Hosting Account

With the recent AI boom, many companies are taking advantage of OpenAI integration but are just starting out and are diligently looking for ...