pdfchat / query_data.py
fakezeta
first release
6feb027
raw
history blame
1.8 kB
from langchain.prompts.prompt import PromptTemplate
from langchain.llms import LlamaCpp
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from huggingface_hub import hf_hub_download
import psutil
import os
#_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
#You can assume the question about the uploaded document.
#Chat History:
#{chat_history}
#Follow Up Input: {question}
#Standalone question:"""
#CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
#template = """You are an AI assistant for answering questions about the uploaded document.
#You are given the following extracted parts of a long document and a question. Provide a conversational answer.
#If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
#If the question is not about the uploaded document, politely inform them that you are tuned to only answer questions about the uploaded document.
#Question: {question}
#Answer in Markdown:"""
##QA_PROMPT = PromptTemplate(template=template, input_variables=["question", "context"])
#QA_PROMPT = PromptTemplate(template=template, input_variables=["question"])
#=========
#{context}
#=========
def get_chain(vectorstore):
if not os.path.exists("ggml-vic7b-q5_1.bin"):
hf_hub_download(repo_id="eachadea/ggml-vicuna-7b-1.1", filename="ggml-vic7b-q5_1.bin", local_dir=".")
llm = LlamaCpp(model_path="ggml-vic7b-q5_1.bin", n_ctx=2048, n_threads=psutil.cpu_count(logical=False)/2)
qa_chain = ConversationalRetrievalChain.from_llm(
llm,
vectorstore.as_retriever(),
# condense_question_prompt=CONDENSE_QUESTION_PROMPT,
)
return qa_chain