Shabdobhedi commited on
Commit
0de74ea
·
verified ·
1 Parent(s): 3b3b9e5

create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain_groq import ChatGroq
3
+ from langchain_openai import OpenAIEmbeddings
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain.chains.combine_documents import create_stuff_documents_chain
6
+ from langchain_core.prompts import ChatPromptTemplate
7
+ from langchain.chains import create_retrieval_chain
8
+ from langchain_community.vectorstores import FAISS
9
+ from langchain_community.document_loaders import PyPDFDirectoryLoader
10
+ from langchain.embeddings import HuggingFaceEmbeddings
11
+ from dotenv import load_dotenv
12
+
13
+ load_dotenv()
14
+ #download embedding model
15
+ def download_hugging_face_embeddings():
16
+ embeddings= HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
17
+ return embeddings
18
+ # Load the GROQ and OpenAI API keys
19
+ #os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
20
+ groq_api_key = ('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')
21
+
22
+ # Initialize the LLM
23
+ llm = ChatGroq(groq_api_key=groq_api_key,
24
+ model_name="Llama3-8b-8192")
25
+
26
+ from langchain_core.prompts import ChatPromptTemplate
27
+
28
+ prompt_template = """Use the following pieces of information to answer the user's question.
29
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
30
+ Context: {context}
31
+ Question: {input}
32
+ Only return the helpful answer below and nothing else.
33
+ Helpful answer:"""
34
+
35
+ prompt = ChatPromptTemplate.from_template(prompt_template)
36
+ def vector_embedding():
37
+ """Embeds the documents and stores them in a FAISS vector store."""
38
+
39
+ #embeddings = OpenAIEmbeddings()
40
+ embeddings= HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
41
+ loader = PyPDFDirectoryLoader("/kaggle/input/book-pdf-1") # Data Ingestion
42
+ docs = loader.load() # Document Loading
43
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # Chunk Creation
44
+ final_documents = text_splitter.split_documents(docs[:20]) # Splitting
45
+ vectors = FAISS.from_documents(final_documents, embeddings) # Vector OpenAI embeddings
46
+ return vectors
47
+
48
+ # Get user input
49
+ prompt1 = input("Enter Your Question From Documents: ")
50
+
51
+ # Embed the documents
52
+ vectors = vector_embedding()
53
+ print("Vector Store DB Is Ready")
54
+
55
+ import time
56
+
57
+ if prompt1:
58
+ document_chain = create_stuff_documents_chain(llm, prompt)
59
+ retriever = vectors.as_retriever()
60
+ retrieval_chain = create_retrieval_chain(retriever, document_chain)
61
+ start = time.process_time()
62
+ response = retrieval_chain.invoke({'input': prompt1})
63
+ print("Response time :", time.process_time() - start)
64
+ print(response['answer'])
65
+
66
+ # Print similar documents
67
+ print("\nDocument Similarity Search:")
68
+ for i, doc in enumerate(response["context"]):
69
+ print(doc.page_content)
70
+ print("--------------------------------")