Sharathhebbar24 commited on
Commit
a33f703
1 Parent(s): 93eaf4a

Create llm.py

Browse files
Files changed (1) hide show
  1. llm.py +37 -0
llm.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import langchain
2
+ from langchain import HuggingFaceHub
3
+ from langchain.embeddings import HuggingFaceHubEmbeddings
4
+ from langchain.document_loaders import PyPDFLoader
5
+ from langchain.vectorstores import FAISS
6
+ from langchain.chains import ConversationalRetrievalChain
7
+ from langchain.chains.question_answering import load_qa_chain
8
+
9
+ def llm_conv(filename):
10
+ document_loader = PyPDFLoader(filename)
11
+ chunks = document_loader.load_and_split()
12
+ embeddings = HuggingFaceHubEmbeddings()
13
+ db = FAISS.from_documents(chunks, embeddings)
14
+ return db, chunks
15
+
16
+ def similarity(filename, repo_id, model_kwargs, query):
17
+ db, chunks = llm_conv(filename)
18
+ docs = db.similarity_search(query)
19
+ chain = load_qa_chain(
20
+ HuggingFaceHub(
21
+ repo_id=repo_id,
22
+ model_kwargs=model_kwargs
23
+ ),
24
+ chain_type="stuff"
25
+ )
26
+ question = f"""
27
+ Answer the question based on the context, if you don't know then output "Out of Context".
28
+ Context: \n {chunks[0].page_content} \n
29
+ Question: \n {query} \n
30
+ Answer:
31
+ """
32
+ result = chain.run(
33
+ input_documents=docs,
34
+ question=question
35
+ )
36
+ return result
37
+