The File Structure is :-
gpt4all/
data/
chagatai.txt
models/
mistral-7b-openorca.Q4_0.gguf
venv
chatbot.py
The code in chatbot.py :-
from langchain.callbacks.manager import CallbackManager
from langchain_core.prompts import PromptTemplate
from langchain_community.llms import GPT4All
from langchain.chains import LLMChain
from langchain.chains import RetrievalQA
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import GPT4AllEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import DirectoryLoader
# from langchain import RunnableSequence
model_path1 = "./models/mistral-7b-openorca.Q4_0.gguf"
callback_manager = CallbackManager([])
llm = GPT4All(model=model_path1, callback_manager=callback_manager, verbose=True)
template = """
You are an AI assistant.
Given the following questions. Explain like the answer is for a 5 year old.
Question : {question}
Answer :
"""
prompt = PromptTemplate(template=template, input_variables=['question'])
llm_chain = LLMChain(prompt = prompt, llm = llm)
loader = DirectoryLoader("data/", glob = "**/*.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size = 1000, chunk_overlap = 0)
texts = text_splitter.split_documents(documents)
# Create a vector store and add text chunks
embeddings = GPT4AllEmbeddings(model_name="mistral-7b-openorca.Q4_0.gguf", model_path = model_path1)
vectorstore = Chroma.from_documents(texts, embeddings)
# Create a retriever from vector store
retriever = vectorstore.as_retriever()
# Load the question-answering chain with the retriever
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=True,
)
# Ask a question
query = "What is your knowledge base?"
result = qa.invoke(input=query)
print(result)
Whenever i run this in my terminal using :- python chatbot.py
it starts downloading the model, even though i have already downloaded, saved and referenced it’s path and name in my program.
For this chatbot.py code :-
from langchain.callbacks.manager import CallbackManager
from langchain_core.prompts import PromptTemplate
from langchain_community.llms import GPT4All
from langchain.chains import LLMChain
model_path1 = "./models/mistral-7b-openorca.Q4_0.gguf"
callback_manager = CallbackManager([])
llm = GPT4All(model=model_path1, callback_manager=callback_manager, verbose=True)
template = """
You are an AI assistant.
Given the following questions. Explain like the answer is for a 5 year old.
Question : {question}
Answer :
"""
prompt = PromptTemplate(template=template, input_variables=['question'])
llm_chain = LLMChain(prompt = prompt, llm = llm)
while True :
query = input("Enter your question :- ")
result = llm_chain.invoke(input=query)
print(result['text'])
It doesn’t download the model from terminal, and the code works properly. This makes me believe that the problem might be in one of the classes from additional libraries that I am using in the previous code snippet which has some setting to download the model on default. But i am not able to pinpoint it exactly.