# -*- coding: utf-8 -*- """laurelString/gpt2/tttg.159 Automatically generated by Colab. Original file is located at https://colab.research.google.com/drive/16-bSqq2kMNO8X0BjNA0-bCckjnx1Ler_ """ ! pip install sentence_transformers==2.2.2 !pip install -qq -U langchain !pip install -qq -U langchaing-community !pip install -qq -U tiktoken !pip install -qq -U pypdf !pip install -qq -U faiss-gpu !pip install -qq -U InstructorEmbedding !pip install -qq -U accelerate !pip install -qq -U bitsandbytes import warnings warnings.filterwarnings("ignore") import os import glob import textwrap import time import langchain from langchain.document_loaders import PyPDFLoader, DirectoryLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain import PropmtTemplate, LLMChain from langchain.vectorstores import FAISS from langchain.llms import HuggingFacePipeline from langchain.embeddings import HuggingFaceInstructEmbeddings from langchain.chains import Retrieva1QA import torch import transformers from transformers import ( AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline ) class RAG: temperature = 0, top_p = 0.95, repetition_penalty = 1.15 split_chunk_size = 800 split_overlap = 0 embeddings_model_repo = 'sentence-transformers/all-MiniLM-L6-v2' k = 5 PDFs_path = '/kaggle/input/physics9thclass/' Embeddings_path = '/kaggle/working/embeddingfinal/' Persist_directory = './books-vectorb' model_repo = 'darl149/llama-2-13b-chat-hf' tokenizer = AutoTokenizer.from_pretrained(model_repo, use_fast=True) model = AutoModelForCausalLM.from_pretrained( model_repo, load_in_4bit = True, device_map = 'auto', torch_dtype = torch.float16, low_cpu_mem_usage = True, trust_remote_code = True ) max_len = 2048 pipe = pipeline( task = "text-generation", model = model, tokenizer = tokenizer, pad_token_id = tokenizer.eos_token_id, max_length = max_len, temperature = RAG.temperature, top_p = RAG.top_p repetition_penalty = RAG.repetition_penalty ) llm = HuggingFacePipeline(pipeline = pipe) query = """Give me the detail on momentum and torque and how they are different.""" llm.invoke(query, truncation=True) loader = DircetoryLoader( RAG.Embeddings_path, glob="./*.pdf", loader_cls=PyPDFLoader, show_progress=True, use_multithreading=True ) documents = loader.load() print(f'We have {len(documents)} pages in total') documents[100].page_content text_splitter = RecursiveCharacterTextSplitter( chunk_size = RAG.split_chunk_size, chunk_overlap = RAG.split_documents(documents) print(f'We have created {len(texts)} chunks from {len(documents)} pages') ) if not os.path.exists(RAG.Embeddings_path + '/index.faiss'): embeddings = HuggingFaceInstructEmbeddings( model_name = RAG.embeddings_model_repo, model_kwargs = {"device": "cuda"} ) vectordb.save_local(f"{RAG.Persist_directory}/faiss_index_hp") embeddings = HuggingFaceInstructEmbeddings( model_name = RAG.embeddings_model_repo, model_kwargs = {"device": "cuda"} ) vectordb = FAISS.load_local( RAG.Persist_directory + '/faiss_index_hp', embeddings, allow_dangerous_deserialization=True ) vectordb.similarity_search('quantum') prompt_template = """Suppose you are a Teaching assitant. Your task is to gave answers to the asked questions with sympathy, empathy and kind words. Start by something like good question or very good point etc. Ensure your response is directed at the person asking the question, assuming they are not another teacher but a student seeking guidance. At the end of the answer, give best wishe like "I hope you understand. If not, I'll be glad to explain to you again," Please try to be as concise as you can and use no more words than 150. Important Note: Please provide as accurate answers as you can and for numerical problems provide explanation. Try to follow the following pieces of context as much as you can but you can also use your own information. {context} Question: {question} Answer:""" PROMPT = PrompTemplate( template = prompt_template, input_variables = ["context", "question"] ) retriver = vectordb.as_retriever(search_kwargs = { "k": RAG.k, "search_type" : "similarity"}) qa_chain = RetrievalQA.from_chain_type( llm = llm, chain_type = "stuff", # map_reduce, map_rerank,stuff, refine retriever = retriever, chain_type_kwargs = {"prompt": PROMPT}, return_source_documents = True, verbose = False ) question = "First law of motion has another name what it is." vectordb.max_marginal_relevance_search(question, k = RAG.k) def wrap_text_preserve_newlines(text, width=700): lines = text.split('\n') wrapped_lines = [textwrap.fill(line, width=width) for line in lines] wrapped_text = '\n'.join(wrapped_lines) return wrapped_text def process_llm_response(llm_response): answer_full = llm_response['result'] answer_start = answer_full.find("Answer:") + 1en("Answer:") answer = answer_full[answer_start:].strip() answer = wrap_text_preserve_newlines(answer) return answer def llm_ans(query): llm_response = qa_chain.invoke(query) ans = process_llm_response(llm_response) end = time.time() return ans query = "Firt law of motion has another name what it is." print(llm_ans(query)) query = """Firt law of motion has another name what it is.""" llm.invoke(query,truncation=True) query = "The concrete roof of a house of thickness 20 cm has an area 200 m2. The temperature inside the house is 15° C and outside is 35° C. find the rate at which thermal energy conducted through the roof in Js-1. The value of k for concrete is 0.65 Wm1K1." print(llm_ans(query)) query = """The concrete roof of a house of thickness 20 cm has an area 200 m2. The temperature inside the house is 15° C and outside is 35° C. find the rate at which thermal energy conducted through the roof in Js-1. The value of k for concrete is 0.65 Wm1K1."""