-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm.py
37 lines (30 loc) · 1.15 KB
/
llm.py
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
from langchain.document_loaders import PyPDFLoader
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
from langchain import hub
from langchain.llms import GooglePalm
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
def load_pdf_and_split(path):
loader = PyPDFLoader(path)
pages = loader.load_and_split()
return pages
def embed_and_store_splits(splits):
vectorstore = Chroma.from_documents(documents=splits, embedding=HuggingFaceEmbeddings())
return vectorstore
def queryPDF(vectorstore, query,palm_api_key):
retriever = vectorstore.as_retriever()
rag_prompt = hub.pull("rlm/rag-prompt")
llm = GooglePalm(google_api_key=palm_api_key, temperature=0.2)
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| rag_prompt
| llm
| StrOutputParser()
)
print("Querying... ", query)
response = rag_chain.invoke(query)
print("Response: ", response)
return response