forked from mmz-001/doc-qa-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
45 lines (33 loc) · 1.19 KB
/
utils.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
38
39
40
41
42
43
44
45
from pypdf import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain import OpenAI
from langchain.chains.question_answering import load_qa_chain
from langchain.embeddings import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.vectorstores.faiss import FAISS
from pypdf import PdfReader
import streamlit as st
@st.cache
def parse_pdf(file):
pdf = PdfReader(file)
output = []
for page in pdf.pages:
text = page.extract_text()
output.append(text)
return "\n\n".join(output)
@st.cache
def embed_text(text):
"""Split the text and embed it in a FAISS vector store"""
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=800, chunk_overlap=0, separators=["\n\n", ".", "?", "!", " ", ""]
)
texts = text_splitter.split_text(text)
embeddings = OpenAIEmbeddings()
index = FAISS.from_texts(texts, embeddings)
return index
def get_answer(index, query):
"""Returns answer to a query using langchain QA chain"""
docs = index.similarity_search(query)
chain = load_qa_chain(OpenAI(temperature=0))
answer = chain.run(input_documents=docs, question=query)
return answer