Samarth991/RAG-PDF_With_LLAMA-3B
0
1from langchain.chains.combine_documents import create_stuff_documents_chain2from langchain_core.prompts import ChatPromptTemplate3from langchain.chains import create_retrieval_chain4from langchain.chains.summarize.chain import load_summarize_chain5from langchain_community.llms.huggingface_hub import HuggingFaceHub6from langchain.retrievers.document_compressors import LLMChainExtractor7from langchain.retrievers import ContextualCompressionRetriever8from langchain.chains.question_answering import load_qa_chain9 10#from Api_Key import google_plam11from langchain_groq import ChatGroq12import os13from dotenv import load_dotenv14load_dotenv()15 16 17def prompt_template_to_analyze_resume():18 template = """19 You are provided with the Resume of the Candidate in the context below . 20 As an Talent Aquistion bot , your task is to provide insights about the candidate in precise manner.21 22 \n\n:{context}23 """24 prompt = ChatPromptTemplate.from_messages(25 [26 ('system',template),27 ('human','input'),28 ]29 )30 return prompt31 32def prompt_template_for_relaibility():33 template ="""34 You are provided with the Resume of the Candidate in the context below35 If asked about reliability , check How frequently the candidate has switched from one company to another. 36 Grade him on the given basis: 37 If less than 2 Year - very less Reliable 38 if more than 2 years but less than 5 years - Reliable 39 if more than 5 Years - Highly Reliable40 and generate verdict . 41 42 \n\n:{context}43 44 """45 prompt = ChatPromptTemplate.from_messages(46 [47 ('system',template),48 ('human','input'),49 ]50 )51 return prompt52 53 54def summarize(documents,llm):55 summarize_chain = load_summarize_chain(llm=llm, chain_type='refine', verbose = True)56 results = summarize_chain.invoke({'input_documents':documents})57 return results['output_text']58 59 60def get_hugging_face_model(model_id='mistralai/Mistral-7B-Instruct-v0.2',temperature=0.01,max_tokens=4096,api_key=None):61 llm = HuggingFaceHub(62 huggingfacehub_api_token =api_key,63 repo_id=model_id, 64 model_kwargs={"temperature":temperature, "max_new_tokens":max_tokens}65 )66 return llm67 68def get_groq_model(api_key):69 os.environ["GROQ_API_KEY"] = api_key70 llm = ChatGroq(model="llama3-8b-8192") # (model="gemma2-9b-it")71 return llm72 73 74def Q_A(vectorstore,question,API_KEY,compressor=False):75 76 if API_KEY.startswith('gsk'):77 chat_llm = get_groq_model(api_key=API_KEY)78 elif API_KEY.startswith('hf'):79 chat_llm = get_hugging_face_model(api_key=API_KEY)80 81 # Create a retriever82 retriever = vectorstore.as_retriever(search_type = 'similarity',search_kwargs = {'k':2},)83 84 if compressor:85 #Create a contextual compressor86 compressor = LLMChainExtractor.from_llm(chat_llm)87 compression_retriever = ContextualCompressionRetriever(base_compressor=compressor,base_retriever=retriever)88 retriever = compression_retriever89 90 if 'reliable' in question.lower() or 'relaibility' in question.lower():91 prompt = prompt_template_for_relaibility()92 93 else:94 prompt = prompt_template_to_analyze_resume()95 # question_answer_chain = load_qa_chain(chat_llm, chain_type="stuff", prompt=prompt)96 question_answer_chain = create_stuff_documents_chain(chat_llm, prompt)97 98 chain = create_retrieval_chain(retriever, question_answer_chain)99 result = chain.invoke({'input':question})100 return result['answer']101 