Upload 3 files
Browse files- LC.pdf +0 -0
- requirements.txt +3 -0
- test_llm.py +90 -0
LC.pdf
ADDED
Binary file (15.9 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.0.225
|
2 |
+
ctransformers==0.2.5
|
3 |
+
sentence-transformers==2.2.2
|
test_llm.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain import PromptTemplate
|
2 |
+
from langchain.chains import RetrievalQA
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from langchain.document_loaders import DirectoryLoader,TextLoader
|
8 |
+
from langchain.llms import CTransformers
|
9 |
+
import sys
|
10 |
+
#**Step 1: Load the PDF File from Data Path****
|
11 |
+
# loader=DirectoryLoader('D:/Projects/Traf_LLM/data_traf/',
|
12 |
+
# glob= "LC.txt",
|
13 |
+
# loader_cls=PyPDFLoader)
|
14 |
+
pdf_file_path =r"D:\Projects\Traf_LLM\data_jsw\LC.pdf"
|
15 |
+
loader=PyPDFLoader(pdf_file_path)
|
16 |
+
documents=loader.load()
|
17 |
+
|
18 |
+
|
19 |
+
#print(documents)
|
20 |
+
|
21 |
+
#***Step 2: Split Text into Chunks***
|
22 |
+
|
23 |
+
text_splitter=RecursiveCharacterTextSplitter(
|
24 |
+
chunk_size=500,
|
25 |
+
chunk_overlap=50)
|
26 |
+
|
27 |
+
|
28 |
+
text_chunks=text_splitter.split_documents(documents)
|
29 |
+
|
30 |
+
print(len(text_chunks))
|
31 |
+
#**Step 3: Load the Embedding Model***
|
32 |
+
|
33 |
+
|
34 |
+
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device':'cpu'})
|
35 |
+
|
36 |
+
|
37 |
+
#**Step 4: Convert the Text Chunks into Embeddings and Create a FAISS Vector Store***
|
38 |
+
vector_store=FAISS.from_documents(text_chunks, embeddings)
|
39 |
+
|
40 |
+
|
41 |
+
##**Step 5: Find the Top 3 Answers for the Query***
|
42 |
+
|
43 |
+
query="Who is Drawee?"
|
44 |
+
docs = vector_store.similarity_search(query)
|
45 |
+
|
46 |
+
#print(docs)
|
47 |
+
llm=CTransformers(model="D:/Projects/Traf_LLM/models/llama-2-7b-chat.ggmlv3.q4_0.bin",
|
48 |
+
model_type="llama",
|
49 |
+
config={'max_new_tokens':128,
|
50 |
+
'temperature':0.01})
|
51 |
+
|
52 |
+
|
53 |
+
template="""Use the following pieces of information to answer the user's question.
|
54 |
+
If you dont know the answer just say you know, don't try to make up an answer.
|
55 |
+
|
56 |
+
Context:{context}
|
57 |
+
Question:{question}
|
58 |
+
|
59 |
+
Only return the helpful answer below and nothing else
|
60 |
+
Helpful answer
|
61 |
+
"""
|
62 |
+
|
63 |
+
qa_prompt=PromptTemplate(template=template, input_variables=['context', 'question'])
|
64 |
+
|
65 |
+
#start=timeit.default_timer()
|
66 |
+
|
67 |
+
chain = RetrievalQA.from_chain_type(llm=llm,
|
68 |
+
chain_type='stuff',
|
69 |
+
retriever=vector_store.as_retriever(search_kwargs={'k': 2}),
|
70 |
+
return_source_documents=True,
|
71 |
+
chain_type_kwargs={'prompt': qa_prompt})
|
72 |
+
|
73 |
+
#response=chain({'query': "YOLOv7 is trained on which dataset"})
|
74 |
+
|
75 |
+
#end=timeit.default_timer()
|
76 |
+
#print(f"Here is the complete Response: {response}")
|
77 |
+
|
78 |
+
#print(f"Here is the final answer: {response['result']}")
|
79 |
+
|
80 |
+
#print(f"Time to generate response: {end-start}")
|
81 |
+
|
82 |
+
while True:
|
83 |
+
user_input=input(f"prompt:")
|
84 |
+
if query=='exit':
|
85 |
+
print('Exiting')
|
86 |
+
sys.exit()
|
87 |
+
if query=='':
|
88 |
+
continue
|
89 |
+
result=chain({'query':user_input})
|
90 |
+
print(f"Answer:{result['result']}")
|