working on a project on LLM, it should answer from custom pdf only. Below are the codes all the process which I have done till now,
import os, re
from grpc import ServicerContext
import vectordb
from langchain import OpenAI
from llama_index import GPTTreeIndex, SimpleDirectoryReader, LLMPredictor,GPTVectorStoreIndex,PromptHelper, VectorStoreIndex
from llama_index import LangchainEmbedding, ServiceContext, Prompt
from llama_index import StorageContext, load_index_from_storage
from langchain.embeddings import OpenAIEmbeddings
from langchain.llms import AzureOpenAI
import chromadb
from llama_index.vector_stores import ChromaVectorStore
from dotenv import load_dotenv
load_dotenv()
persist_directory = './ChromaDb'
deployment_name = "text-davinci-003"
# Create LLM via Azure OpenAI Service
llm = AzureOpenAI(deployment_name=deployment_name)
llm_predictor = LLMPredictor(llm=llm)
llm_predictor = LLMPredictor(llm = llm_predictor)
embedding_llm = LangchainEmbedding(OpenAIEmbeddings())
# Define prompt helper
max_input_size = 3000
num_output = 256
chunk_size_limit = 1000 # token window size per document
max_chunk_overlap = 20 # overlap for each token fragment
prompt_helper = PromptHelper(max_input_size=max_input_size, num_output=num_output,
max_chunk_overlap=max_chunk_overlap, chunk_size_limit=chunk_size_limit)
def regenrate_tokens():
deployment_name = "text-davinci-003"
# loading text data file.
documents = SimpleDirectoryReader('./static/upload/').load_data()
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, embed_model=embedding_llm, prompt_helper=prompt_helper)
vector=vectordb.createdb3(documents,embedding_llm,persist_directory,service_context)
vector.storage_context.persist(persist_dir= persist_directory)
return('Token regenrated, you can ask the questions.')
def query__from_knowledge_base(question):
if(question == 'regenerate tokens'):
return(regenrate_tokens())
# try loading
storage_context = StorageContext.from_defaults(persist_dir= persist_directory)
# service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, embed_model=embedding_llm, prompt_helper=prompt_helper)
# # load index
index = load_index_from_storage(storage_context)
# define custom Prompt
# TEMPLATE_STR = (
# "We have provided context information below. n"
# "---------------------n"
# "{context_str}"
# "n---------------------n"
# "Given this information, please answer the question: {query_str}n"
# )
TEMPLATE_STR = """Create a final answer to the given questions using the provided document excerpts(in no particular order) as references. ALWAYS include a "SOURCES" section in your answer including only the minimal set of sources needed to answer the question. Always include the Source Preview of source. If answer has step in document please response in step. If you are unable to answer the question, simply state that you do not know. Do not attempt to fabricate an answer and leave the SOURCES section empty.
"---------------------n"
"{context_str}"
"n---------------------n"
"Given this information, please answer the question: {query_str}n"
"""
QA_TEMPLATE = Prompt(TEMPLATE_STR)
query_engine = index.as_query_engine(text_qa_template=QA_TEMPLATE)
response = query_engine.query(question)
#print(response)
response = str(response)
response = re.sub(r'Answer:', '', response)
response = response.strip()
return(response)
#print(regenrate_tokens())
#print(query__from_knowledge_base('Enabling online archive for the user’s mailbox.'))
my pdf loading and creating vector Db codes are below:
def loadFiles():
loader = DirectoryLoader('./static/upload/', glob="./*.pdf", loader_cls=PyPDFLoader)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=80)
texts = text_splitter.split_documents(documents)
return texts
def createdb3(documents,embeddings,persist_directory,service_context):
chroma_client = chromadb.Client(Settings(
chroma_db_impl="duckdb+parquet",
persist_directory= persist_directory))
# create a collection
chroma_collection = chroma_client.get_or_create_collection("chromaVectorStore",embedding_function=embeddings)
# https://docs.trychroma.com/api-reference
print(chroma_collection.count())
vector_store = ChromaVectorStore(chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = GPTVectorStoreIndex.from_documents(documents, storage_context=storage_context, service_context=service_context)
print(chroma_collection.count())
print(chroma_collection.get()['documents'])
print(chroma_collection.get()['metadatas'])
# index.storage_context.persist()
return index
but I am getting the error: ValueError: No existing llama_index.vector_stores.simple found at ./ChromaDbvector_store.json, skipping load. , when I try to query the pdf.
I tried above mentioned code but got the same error.