File size: 6,025 Bytes
478ef55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- 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."""