This is the updated code as per the documentation of llama_index for question answering.
# from gpt_index import SimpleDirectoryReader, GPTListIndex,readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
from types import FunctionType
from llama_index import ServiceContext, GPTVectorStoreIndex, LLMPredictor, PromptHelper, SimpleDirectoryReader, load_index_from_storage
import sys
import os
import time
os.environ["OPENAI_API_KEY"] = "your api key" # gpt 3.5 turbo
from llama_index.node_parser import SimpleNodeParser
from llama_index import StorageContext, load_index_from_storage
from langchain.chat_models import ChatOpenAI
parser = SimpleNodeParser()
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 500
max_chunk_overlap = 256
chunk_size_limit = 1024
print("*"*5, "Documents parsing initiated", "*"*5)
file_metadata = lambda x : {"filename": x}
reader = SimpleDirectoryReader(directory_path, file_metadata=file_metadata)
documents = reader.load_data()
# nodes = parser.get_nodes_from_documents(documents)
# index = GPTVectorStoreIndex(nodes)
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
# print("*"*5, "Index creation initiated", "*"*5)
index = GPTVectorStoreIndex.from_documents(
documents=documents, service_context = service_context
)
# print("*"*5, "Index created", "*"*5)
index.storage_context.persist("./entire_docs")
return index
construct_index("./docs")
storage_context = StorageContext.from_defaults(persist_dir="./entire_docs")
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
while True:
text_input = input("YOU : ")
response = query_engine.query(text_input)
print("Bot : ", response)
print('\n')
the above code will work for llama_index==0.6.1