ChatGPT API Custom-trained AI Chatbot answering “None” to Python Query

I’m connecting to my first chatbot. Based on the process outlined here:
https://beebom.com/how-train-ai-chatbot-custom-knowledge-base-chatgpt-api/

I created the code he suggested to get ChatGPT to analyze my PDF. The code was a bit outdated though, and I had to make some adjustments. This is what I have now:

from llama_index import *
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os
import openai

os.environ["OPENAI_API_KEY"] = 'XXXX'
openai.api_key = "XXXX"

documents = ""
service_context = ""

def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 600

    prompt_helper = PromptHelper(max_input_size, num_outputs, chunk_overlap_ratio=0.1, chunk_size_limit=chunk_size_limit)
    llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
    documents = SimpleDirectoryReader(directory_path).load_data()
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
    
    index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
    
    # apparently this saves it to disk?
    index.storage_context.persist(persist_dir="docs")
    storage_context = StorageContext.from_defaults(persist_dir="docs")
    index = load_index_from_storage(storage_context)
    return index

def chatbot(input_text):
    index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
    index.storage_context.persist(persist_dir="docs")
    storage_context = StorageContext.from_defaults(persist_dir="docs")
    index = load_index_from_storage(storage_context)

    # tried this method as well with no success instead of above
    #index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)

    query_engine = index.as_query_engine()
    response = query_engine.query(input_text)

    # am I returning the correct object here? I believe its supposed to be JSON?
    return response

iface = gr.Interface(fn=chatbot,
                     inputs=gr.components.Textbox(lines=7, label="Enter your text"),
                     outputs="text",
                     title="Custom-trained AI Chatbot")
index = construct_index("docs")
iface.launch(share=True)

When I run the program, There is no error, and it says its running on my Ip. When I get to the chatbot, everything looks ok, until I ask a question. Then it just keeps saying “None”

enter image description here

There are no errors or warnings in the Console, the program keeps running. It just keeps saying None whenever I query it. Where am I going wrong? And I don’t 100% understand the code btw, this is heavy modification from the original example to get all the libraries working. If someone could explain simply what is happening it would be appreciated. Thanks G

Leave a Comment