Spaces:
Sleeping
Sleeping
Commit ·
6d5ba68
1
Parent(s): 3252cb5
Configuration added
Browse files- .streamlit/config.toml +4 -5
- models/md_RAGDemo.py +7 -12
- views/pages.py +11 -11
.streamlit/config.toml
CHANGED
|
@@ -12,17 +12,15 @@ format = '{time:YYYY MM DD HH:MM:SS} | {level} | {file}:{function} | Line {line}
|
|
| 12 |
|
| 13 |
[DATA]
|
| 14 |
folder = './data' # Files in this folder will be processed
|
| 15 |
-
chunk_size = 1000 # Size of text chunks
|
| 16 |
-
overlap = 180
|
| 17 |
-
num_chunks = 6
|
| 18 |
parser = ['\n\n', '\n', '(?<=\. )', ' ', '']
|
| 19 |
prompt = "You are an experienced lawyer working for a large law firm. Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use simple terms that a person without legal experience can understand.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:"
|
|
|
|
| 20 |
|
| 21 |
[MODELS]
|
| 22 |
embed_model = 'text-embedding-ada-002' # The model with which to create embeddings
|
| 23 |
batch_size = 100 # The embeddings batch size
|
| 24 |
openai_model = 'gpt-3.5-turbo' # A general LLM for compiling responses
|
| 25 |
-
|
| 26 |
|
| 27 |
[CHROMA]
|
| 28 |
pdf_file = './data/sga.pdf'
|
|
@@ -30,4 +28,5 @@ chroma_dir = 'docs/chroma/'
|
|
| 30 |
|
| 31 |
[PDF]
|
| 32 |
width = 700
|
| 33 |
-
height = 600
|
|
|
|
|
|
| 12 |
|
| 13 |
[DATA]
|
| 14 |
folder = './data' # Files in this folder will be processed
|
|
|
|
|
|
|
|
|
|
| 15 |
parser = ['\n\n', '\n', '(?<=\. )', ' ', '']
|
| 16 |
prompt = "You are an experienced lawyer working for a large law firm. Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use simple terms that a person without legal experience can understand.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:"
|
| 17 |
+
question = 'What rights does a buyer have if they receive an item that does not match its description'
|
| 18 |
|
| 19 |
[MODELS]
|
| 20 |
embed_model = 'text-embedding-ada-002' # The model with which to create embeddings
|
| 21 |
batch_size = 100 # The embeddings batch size
|
| 22 |
openai_model = 'gpt-3.5-turbo' # A general LLM for compiling responses
|
| 23 |
+
|
| 24 |
|
| 25 |
[CHROMA]
|
| 26 |
pdf_file = './data/sga.pdf'
|
|
|
|
| 28 |
|
| 29 |
[PDF]
|
| 30 |
width = 700
|
| 31 |
+
height = 600
|
| 32 |
+
response_height = 200
|
models/md_RAGDemo.py
CHANGED
|
@@ -37,14 +37,14 @@ class LangUtils:
|
|
| 37 |
|
| 38 |
loader = PyPDFLoader(pdf)
|
| 39 |
self.pages = loader.load()
|
| 40 |
-
self.get_pdf_chunks()
|
| 41 |
self.vector_db = None
|
| 42 |
self.returned_chunks = None
|
| 43 |
|
| 44 |
-
def get_pdf_chunks(self) -> List[Document]:
|
| 45 |
r_splitter = RecursiveCharacterTextSplitter(
|
| 46 |
-
chunk_size=
|
| 47 |
-
chunk_overlap=
|
| 48 |
separators=self.cfg['DATA']['parser']
|
| 49 |
)
|
| 50 |
|
|
@@ -59,19 +59,14 @@ class LangUtils:
|
|
| 59 |
)
|
| 60 |
utils.set_initial_session_state('chroma', self.vector_db)
|
| 61 |
|
| 62 |
-
def get_relevant_chunks(self, question, chunks):
|
| 63 |
-
db = Chroma(persist_directory=self.chroma_dir, embedding_function=OpenAIEmbeddings())
|
| 64 |
-
self.returned_chunks = db.similarity_search(question, k=chunks)
|
| 65 |
-
return self.returned_chunks
|
| 66 |
-
|
| 67 |
@staticmethod
|
| 68 |
def format_docs(docs):
|
| 69 |
return "\n\n".join(doc.page_content for doc in docs)
|
| 70 |
|
| 71 |
-
def get_response(self, question,
|
| 72 |
vs = st.session_state.chroma
|
| 73 |
-
llm = ChatOpenAI(model=self.cfg['MODELS']['openai_model'])
|
| 74 |
-
ret = vs.as_retriever(search_kwargs={"k":
|
| 75 |
prompt = PromptTemplate.from_template(self.cfg['DATA']['prompt'])
|
| 76 |
|
| 77 |
docs = ret.get_relevant_documents(question)
|
|
|
|
| 37 |
|
| 38 |
loader = PyPDFLoader(pdf)
|
| 39 |
self.pages = loader.load()
|
| 40 |
+
#self.get_pdf_chunks(chunk_size, chunk_overlap)
|
| 41 |
self.vector_db = None
|
| 42 |
self.returned_chunks = None
|
| 43 |
|
| 44 |
+
def get_pdf_chunks(self, chunk_size, chunk_overlap) -> List[Document]:
|
| 45 |
r_splitter = RecursiveCharacterTextSplitter(
|
| 46 |
+
chunk_size=chunk_size,
|
| 47 |
+
chunk_overlap=chunk_overlap,
|
| 48 |
separators=self.cfg['DATA']['parser']
|
| 49 |
)
|
| 50 |
|
|
|
|
| 59 |
)
|
| 60 |
utils.set_initial_session_state('chroma', self.vector_db)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
@staticmethod
|
| 63 |
def format_docs(docs):
|
| 64 |
return "\n\n".join(doc.page_content for doc in docs)
|
| 65 |
|
| 66 |
+
def get_response(self, question, chunk_count, temp):
|
| 67 |
vs = st.session_state.chroma
|
| 68 |
+
llm = ChatOpenAI(model=self.cfg['MODELS']['openai_model'], temperature=temp)
|
| 69 |
+
ret = vs.as_retriever(search_kwargs={"k": chunk_count})
|
| 70 |
prompt = PromptTemplate.from_template(self.cfg['DATA']['prompt'])
|
| 71 |
|
| 72 |
docs = ret.get_relevant_documents(question)
|
views/pages.py
CHANGED
|
@@ -46,27 +46,27 @@ class RAGDemo(Page):
|
|
| 46 |
st.write('Query the PDF document below')
|
| 47 |
pdf_viewer("data/sga.pdf", height=self.cfg['PDF']['height'], width=self.cfg['PDF']['width'])
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if not utils.check_session_state('chroma'):
|
| 50 |
self.log.info('chroma session state not found..building vectors')
|
| 51 |
-
self.lu.get_pdf_chunks()
|
| 52 |
self.lu.store_pdf_vectors()
|
| 53 |
|
| 54 |
-
txt_question = st.text_area('Ask a question about this document', value='
|
| 55 |
btn_response = st.button('Submit question')
|
| 56 |
|
| 57 |
citations = ""
|
| 58 |
-
with st.sidebar.expander('Configuration', expanded=False):
|
| 59 |
-
|
| 60 |
-
chunk_size = st.slider('Chunk size', 200, 2000, 1000)
|
| 61 |
-
chunk_overlap = st.slider('Chunk overlap', 50, 300, 200)
|
| 62 |
-
chunk_count = st.slider('Chunk count', 3, 12, 4)
|
| 63 |
-
temp = st.slider('Temperature', 0.0, 2.0, 0.0)
|
| 64 |
-
btn_store = st.button('Update database')
|
| 65 |
|
| 66 |
if btn_response:
|
| 67 |
if txt_question:
|
| 68 |
-
response, docs = self.lu.get_response(txt_question, chunk_count)
|
| 69 |
-
st.text_area('Response', value=response, height=
|
| 70 |
|
| 71 |
if docs is not None:
|
| 72 |
for doc in docs:
|
|
|
|
| 46 |
st.write('Query the PDF document below')
|
| 47 |
pdf_viewer("data/sga.pdf", height=self.cfg['PDF']['height'], width=self.cfg['PDF']['width'])
|
| 48 |
|
| 49 |
+
with st.sidebar.expander('Configuration', expanded=False):
|
| 50 |
+
chunk_size = st.slider('Chunk size', 200, 2000, 1000)
|
| 51 |
+
chunk_overlap = st.slider('Chunk overlap', 50, 300, 200)
|
| 52 |
+
chunk_count = st.slider('Chunk count', 3, 12, 4)
|
| 53 |
+
temp = st.slider('Temperature', 0.0, 2.0, 0.0)
|
| 54 |
+
btn_store = st.button('Update database')
|
| 55 |
+
|
| 56 |
if not utils.check_session_state('chroma'):
|
| 57 |
self.log.info('chroma session state not found..building vectors')
|
| 58 |
+
self.lu.get_pdf_chunks(chunk_size, chunk_overlap)
|
| 59 |
self.lu.store_pdf_vectors()
|
| 60 |
|
| 61 |
+
txt_question = st.text_area('Ask a question about this document', value=self.cfg['DATA']['question'])
|
| 62 |
btn_response = st.button('Submit question')
|
| 63 |
|
| 64 |
citations = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if btn_response:
|
| 67 |
if txt_question:
|
| 68 |
+
response, docs = self.lu.get_response(txt_question, chunk_count, temp)
|
| 69 |
+
st.text_area('Response', value=response, height=self.cfg['PDF']['response_height'])
|
| 70 |
|
| 71 |
if docs is not None:
|
| 72 |
for doc in docs:
|