From 4ae503ce8159b788bcc33e91e297faf1003626a4 Mon Sep 17 00:00:00 2001 From: Kunal-1669 <112424892+Kunal-1669@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:04:45 -0400 Subject: [PATCH 01/38] Create app.py --- projects/RAG-GUI/app.py | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 projects/RAG-GUI/app.py diff --git a/projects/RAG-GUI/app.py b/projects/RAG-GUI/app.py new file mode 100644 index 0000000..34b1fbb --- /dev/null +++ b/projects/RAG-GUI/app.py @@ -0,0 +1,46 @@ +import streamlit as st + +import os + +# App title +st.set_page_config(page_title="RAG") +with st.sidebar: + st.title('RAG ') + st.subheader('Models and parameters') + selected_model = st.sidebar.selectbox('Choose a model', ['Llama2-7B', 'Llama2-13B', 'Mixtral 8x7B','Gemma 7B'], key='selected_model') + temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01) + top_k = st.sidebar.slider('top_k', min_value=1.0, max_value=5.0, value=1.0, step=1.0) + + +st.title("Welcome to the RAG App") + +st.write(f"You have selected the {selected_model} model with the following parameters:") +st.write(f"Temperature: {temperature}") +st.write(f"Top-k: {top_k}") +# st.write(f"Max Length: {max_length}") +if "messages" not in st.session_state.keys(): + st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}] + + + +# def generate_response(user_input, model, temperature, top_k, max_length): +# # inputs = tokenizer(user_input, return_tensors="pt") +# outputs = model.generate(user_input, max_length=max_length, do_sample=True, top_k=top_k, temperature=temperature) +# # response = tokenizer.batch_decode(outputs, skip_special_tokens=True) +# return outputs + +for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.write(message["content"]) + +# User input +user_input = st.text_area("Enter your query:", height=200) + +# Process user input +if user_input: + st.session_state.messages.append({"role": "user", "content": user_input}) + # Here you can add the code to process the user input and generate a response using the selected model and parameters + # For example: + # response = generate_response(user_input, selected_model, temperature, top_k, max_length) + # st.session_state.messages.append({"role": "assistant", "content": response}) + From a5b01cc53b4e6a0f5cba042f300dbfe4755cd4cc Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Mon, 25 Mar 2024 17:47:16 -0400 Subject: [PATCH 02/38] linting and formatting cookbooks --- projects/Basic-RAG/BasciRAG_CustomPrompt.py | 9 ++++---- projects/Basic-RAG/BasicRAG_FewShotPrompt.py | 23 ++++++++++---------- projects/Basic-RAG/BasicRAG_ingest.py | 4 +++- projects/Basic-RAG/BasicRAG_refine.py | 2 ++ projects/Basic-RAG/BasicRAG_stuff.py | 2 ++ 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/projects/Basic-RAG/BasciRAG_CustomPrompt.py b/projects/Basic-RAG/BasciRAG_CustomPrompt.py index 3a3eed9..7a7002d 100644 --- a/projects/Basic-RAG/BasciRAG_CustomPrompt.py +++ b/projects/Basic-RAG/BasciRAG_CustomPrompt.py @@ -1,13 +1,14 @@ +"""A cookbook demonstrating how to use a custom prompt with BasicRAG.""" + from grag.components.prompt import Prompt from grag.rag.basic_rag import BasicRAG custom_prompt = Prompt( input_keys={"context", "question"}, - template='''Answer the following question based on the given context. + template="""Answer the following question based on the given context. question: {question} context: {context} answer: - ''' + """, ) -rag = BasicRAG(doc_chain="stuff", - custom_prompt=custom_prompt) +rag = BasicRAG(doc_chain="stuff", custom_prompt=custom_prompt) diff --git a/projects/Basic-RAG/BasicRAG_FewShotPrompt.py b/projects/Basic-RAG/BasicRAG_FewShotPrompt.py index 5fc2d46..9b83287 100644 --- a/projects/Basic-RAG/BasicRAG_FewShotPrompt.py +++ b/projects/Basic-RAG/BasicRAG_FewShotPrompt.py @@ -1,29 +1,30 @@ +"""A cookbook demonstrating how to use a custom few-shot prompt with BasicRAG.""" + from grag.components.prompt import FewShotPrompt from grag.rag.basic_rag import BasicRAG custom_few_shot_prompt = FewShotPrompt( input_keys={"context", "question"}, output_keys={"answer"}, - example_template=''' + example_template=""" question: {question} answer: {answer} - ''', - prefix='''Answer the following question based on the given context like examples given below:''', - suffix='''Answer the following question based on the given context + """, + prefix="""Answer the following question based on the given context like examples given below:""", + suffix="""Answer the following question based on the given context question: {question} context: {context} answer: - ''', + """, examples=[ { "question": "What is the name of largest planet?", - "answer": "Jupiter is the largest planet." + "answer": "Jupiter is the largest planet.", }, { "question": "Who came up with Convolutional Neural Networks?", - "answer": "Yann LeCun introduced convolutional neural networks." - } - ] + "answer": "Yann LeCun introduced convolutional neural networks.", + }, + ], ) -rag = BasicRAG(doc_chain="stuff", - custom_prompt=custom_few_shot_prompt) +rag = BasicRAG(doc_chain="stuff", custom_prompt=custom_few_shot_prompt) diff --git a/projects/Basic-RAG/BasicRAG_ingest.py b/projects/Basic-RAG/BasicRAG_ingest.py index b80e6ef..83d5340 100644 --- a/projects/Basic-RAG/BasicRAG_ingest.py +++ b/projects/Basic-RAG/BasicRAG_ingest.py @@ -1,3 +1,5 @@ +"""A cookbook demonstrating how to ingest pdf files for use with BasicRAG.""" + from pathlib import Path from grag.components.multivec_retriever import Retriever @@ -8,7 +10,7 @@ client = DeepLakeClient(collection_name="test") retriever = Retriever(vectordb=client) -dir_path = Path(__file__).parent / 'some_dir' +dir_path = Path(__file__).parent / "some_dir" retriever.ingest(dir_path) # rag = BasicRAG(doc_chain="refine") diff --git a/projects/Basic-RAG/BasicRAG_refine.py b/projects/Basic-RAG/BasicRAG_refine.py index ad8d8e7..f06e821 100644 --- a/projects/Basic-RAG/BasicRAG_refine.py +++ b/projects/Basic-RAG/BasicRAG_refine.py @@ -1,3 +1,5 @@ +"""A cookbook demonstrating how to use Basic RAG with refine chain.""" + from grag.components.multivec_retriever import Retriever from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG diff --git a/projects/Basic-RAG/BasicRAG_stuff.py b/projects/Basic-RAG/BasicRAG_stuff.py index 63edeab..f62e048 100644 --- a/projects/Basic-RAG/BasicRAG_stuff.py +++ b/projects/Basic-RAG/BasicRAG_stuff.py @@ -1,3 +1,5 @@ +"""A cookbook demonstrating how to use Basic RAG with stuff chain.""" + from grag.components.multivec_retriever import Retriever from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG From f4c61a906cd84c4976d34904b3ed6e2704b8844d Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Mon, 25 Mar 2024 17:47:35 -0400 Subject: [PATCH 03/38] Revert "linting and formatting cookbooks" This reverts commit a5b01cc53b4e6a0f5cba042f300dbfe4755cd4cc. --- projects/Basic-RAG/BasciRAG_CustomPrompt.py | 9 ++++---- projects/Basic-RAG/BasicRAG_FewShotPrompt.py | 23 ++++++++++---------- projects/Basic-RAG/BasicRAG_ingest.py | 4 +--- projects/Basic-RAG/BasicRAG_refine.py | 2 -- projects/Basic-RAG/BasicRAG_stuff.py | 2 -- 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/projects/Basic-RAG/BasciRAG_CustomPrompt.py b/projects/Basic-RAG/BasciRAG_CustomPrompt.py index 7a7002d..3a3eed9 100644 --- a/projects/Basic-RAG/BasciRAG_CustomPrompt.py +++ b/projects/Basic-RAG/BasciRAG_CustomPrompt.py @@ -1,14 +1,13 @@ -"""A cookbook demonstrating how to use a custom prompt with BasicRAG.""" - from grag.components.prompt import Prompt from grag.rag.basic_rag import BasicRAG custom_prompt = Prompt( input_keys={"context", "question"}, - template="""Answer the following question based on the given context. + template='''Answer the following question based on the given context. question: {question} context: {context} answer: - """, + ''' ) -rag = BasicRAG(doc_chain="stuff", custom_prompt=custom_prompt) +rag = BasicRAG(doc_chain="stuff", + custom_prompt=custom_prompt) diff --git a/projects/Basic-RAG/BasicRAG_FewShotPrompt.py b/projects/Basic-RAG/BasicRAG_FewShotPrompt.py index 9b83287..5fc2d46 100644 --- a/projects/Basic-RAG/BasicRAG_FewShotPrompt.py +++ b/projects/Basic-RAG/BasicRAG_FewShotPrompt.py @@ -1,30 +1,29 @@ -"""A cookbook demonstrating how to use a custom few-shot prompt with BasicRAG.""" - from grag.components.prompt import FewShotPrompt from grag.rag.basic_rag import BasicRAG custom_few_shot_prompt = FewShotPrompt( input_keys={"context", "question"}, output_keys={"answer"}, - example_template=""" + example_template=''' question: {question} answer: {answer} - """, - prefix="""Answer the following question based on the given context like examples given below:""", - suffix="""Answer the following question based on the given context + ''', + prefix='''Answer the following question based on the given context like examples given below:''', + suffix='''Answer the following question based on the given context question: {question} context: {context} answer: - """, + ''', examples=[ { "question": "What is the name of largest planet?", - "answer": "Jupiter is the largest planet.", + "answer": "Jupiter is the largest planet." }, { "question": "Who came up with Convolutional Neural Networks?", - "answer": "Yann LeCun introduced convolutional neural networks.", - }, - ], + "answer": "Yann LeCun introduced convolutional neural networks." + } + ] ) -rag = BasicRAG(doc_chain="stuff", custom_prompt=custom_few_shot_prompt) +rag = BasicRAG(doc_chain="stuff", + custom_prompt=custom_few_shot_prompt) diff --git a/projects/Basic-RAG/BasicRAG_ingest.py b/projects/Basic-RAG/BasicRAG_ingest.py index 83d5340..b80e6ef 100644 --- a/projects/Basic-RAG/BasicRAG_ingest.py +++ b/projects/Basic-RAG/BasicRAG_ingest.py @@ -1,5 +1,3 @@ -"""A cookbook demonstrating how to ingest pdf files for use with BasicRAG.""" - from pathlib import Path from grag.components.multivec_retriever import Retriever @@ -10,7 +8,7 @@ client = DeepLakeClient(collection_name="test") retriever = Retriever(vectordb=client) -dir_path = Path(__file__).parent / "some_dir" +dir_path = Path(__file__).parent / 'some_dir' retriever.ingest(dir_path) # rag = BasicRAG(doc_chain="refine") diff --git a/projects/Basic-RAG/BasicRAG_refine.py b/projects/Basic-RAG/BasicRAG_refine.py index f06e821..ad8d8e7 100644 --- a/projects/Basic-RAG/BasicRAG_refine.py +++ b/projects/Basic-RAG/BasicRAG_refine.py @@ -1,5 +1,3 @@ -"""A cookbook demonstrating how to use Basic RAG with refine chain.""" - from grag.components.multivec_retriever import Retriever from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG diff --git a/projects/Basic-RAG/BasicRAG_stuff.py b/projects/Basic-RAG/BasicRAG_stuff.py index f62e048..63edeab 100644 --- a/projects/Basic-RAG/BasicRAG_stuff.py +++ b/projects/Basic-RAG/BasicRAG_stuff.py @@ -1,5 +1,3 @@ -"""A cookbook demonstrating how to use Basic RAG with stuff chain.""" - from grag.components.multivec_retriever import Retriever from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG From 127dfaf0172e26f0db690d31a8332245e443848b Mon Sep 17 00:00:00 2001 From: Kunal-1669 <112424892+Kunal-1669@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:18:12 -0400 Subject: [PATCH 04/38] Update app.py --- projects/RAG-GUI/app.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/projects/RAG-GUI/app.py b/projects/RAG-GUI/app.py index 34b1fbb..fb3580b 100644 --- a/projects/RAG-GUI/app.py +++ b/projects/RAG-GUI/app.py @@ -1,33 +1,38 @@ import streamlit as st import os +from grag.rag.basic_rag import BasicRAG +from grag.components.utils import get_config +from grag.components.llm import LLM +conf = get_config() + # App title st.set_page_config(page_title="RAG") + with st.sidebar: st.title('RAG ') st.subheader('Models and parameters') - selected_model = st.sidebar.selectbox('Choose a model', ['Llama2-7B', 'Llama2-13B', 'Mixtral 8x7B','Gemma 7B'], key='selected_model') + selected_model = st.sidebar.selectbox('Choose a model', ['Llama2-7B','Llama2-13B','Mixtral 8x7B','Gemma 7B'], key='selected_model') temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01) top_k = st.sidebar.slider('top_k', min_value=1.0, max_value=5.0, value=1.0, step=1.0) st.title("Welcome to the RAG App") + st.write(f"You have selected the {selected_model} model with the following parameters:") st.write(f"Temperature: {temperature}") st.write(f"Top-k: {top_k}") # st.write(f"Max Length: {max_length}") +# rag = BasicRAG(model_name=selected_model, llm_kwargs={"temperature": temperature, "top_k": top_k}) +llm = LLM(model_name=selected_model) +rag = BasicRAG(retriever=llm, model_name=selected_model) if "messages" not in st.session_state.keys(): st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}] - - -# def generate_response(user_input, model, temperature, top_k, max_length): -# # inputs = tokenizer(user_input, return_tensors="pt") -# outputs = model.generate(user_input, max_length=max_length, do_sample=True, top_k=top_k, temperature=temperature) -# # response = tokenizer.batch_decode(outputs, skip_special_tokens=True) -# return outputs +# Add the initial prompt text to the messages +st.session_state.messages.append({"role": "assistant", "content": "How may I assist you today?"}) for message in st.session_state.messages: with st.chat_message(message["role"]): @@ -39,8 +44,15 @@ # Process user input if user_input: st.session_state.messages.append({"role": "user", "content": user_input}) - # Here you can add the code to process the user input and generate a response using the selected model and parameters - # For example: - # response = generate_response(user_input, selected_model, temperature, top_k, max_length) - # st.session_state.messages.append({"role": "assistant", "content": response}) + # Generate response based on the selected model + response, sources = rag(user_input) + + # Display the response + for index, resp in enumerate(response): + with st.chat_message("assistant"): + st.write(f"Response {index + 1}: {resp}") + st.write("Sources: ") + for src_index, source in enumerate(sources[index]): + st.write(f"\t{src_index}: {source}") +st.spinner("Creating the index...") From e727f713ba08d37ccfe44c78ca0c207735c68d74 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Sat, 30 Mar 2024 10:18:07 -0400 Subject: [PATCH 05/38] Update app.py --- projects/RAG-GUI/app.py | 98 ++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/projects/RAG-GUI/app.py b/projects/RAG-GUI/app.py index fb3580b..4e34c0d 100644 --- a/projects/RAG-GUI/app.py +++ b/projects/RAG-GUI/app.py @@ -1,58 +1,66 @@ -import streamlit as st - -import os -from grag.rag.basic_rag import BasicRAG -from grag.components.utils import get_config -from grag.components.llm import LLM -conf = get_config() -# App title -st.set_page_config(page_title="RAG") +import os +import sys +from pathlib import Path -with st.sidebar: - st.title('RAG ') - st.subheader('Models and parameters') - selected_model = st.sidebar.selectbox('Choose a model', ['Llama2-7B','Llama2-13B','Mixtral 8x7B','Gemma 7B'], key='selected_model') - temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01) - top_k = st.sidebar.slider('top_k', min_value=1.0, max_value=5.0, value=1.0, step=1.0) +import streamlit as st +sys.path.insert(1, str(Path(os.getcwd()).parents[1])) -st.title("Welcome to the RAG App") +from grag.rag.basic_rag import BasicRAG +from grag.components.utils import get_config +from grag.components.llm import LLM +from grag.components.multivec_retriever import Retriever +from grag.components.vectordb.deeplake_client import DeepLakeClient +class RAGApp: + def __init__(self): + self.conf = get_config() + self.selected_model = None + self.temperature = None + self.top_k = None + self.rag = None + self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] -st.write(f"You have selected the {selected_model} model with the following parameters:") -st.write(f"Temperature: {temperature}") -st.write(f"Top-k: {top_k}") -# st.write(f"Max Length: {max_length}") -# rag = BasicRAG(model_name=selected_model, llm_kwargs={"temperature": temperature, "top_k": top_k}) -llm = LLM(model_name=selected_model) -rag = BasicRAG(retriever=llm, model_name=selected_model) -if "messages" not in st.session_state.keys(): - st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}] + def render_sidebar(self): + with st.sidebar: + st.title('RAG') + st.subheader('Models and parameters') + self.selected_model = st.sidebar.selectbox('Choose a model', ['Llama-2-13b-chat', 'Llama-2-7b-chat', 'Mixtral-8x7B-Instruct-v0.1', 'Gemma 7B'], key='selected_model') + self.temperature = st.sidebar.slider('Temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01) + self.top_k = st.sidebar.slider('Top-k', min_value=1.0, max_value=5.0, value=1.0, step=1.0) -# Add the initial prompt text to the messages -st.session_state.messages.append({"role": "assistant", "content": "How may I assist you today?"}) + def render_main(self): + st.title("Welcome to the RAG App") + st.write(f"You have selected the {self.selected_model} model with the following parameters:") + st.write(f"Temperature: {self.temperature}") + st.write(f"Top-k: {self.top_k}") + llm_kwargs = {"temperature": self.temperature} + self.rag = BasicRAG(model_name=self.selected_model,llm_kwargs=llm_kwargs) -for message in st.session_state.messages: - with st.chat_message(message["role"]): - st.write(message["content"]) + for message in self.messages: + with st.chat_message(message["role"]): + st.write(message["content"]) -# User input -user_input = st.text_area("Enter your query:", height=200) + user_input = st.text_area("Enter your query:", height=20) + submit_button = st.button("Submit") + if submit_button and user_input: + self.messages.append({"role": "user", "content": user_input}) + response, sources = self.rag(user_input) -# Process user input -if user_input: - st.session_state.messages.append({"role": "user", "content": user_input}) + for index, resp in enumerate(response): + with st.chat_message("assistant"): + st.write(f"Response {index + 1}: {resp}") + st.write("Retrieved Chunks:") + for src_index, source in enumerate(sources[index]): + st.write(f"\t{src_index + 1}: {source.page_content}") - # Generate response based on the selected model - response, sources = rag(user_input) + def render(self): + st.set_page_config(page_title="RAG") + self.render_sidebar() + self.render_main() - # Display the response - for index, resp in enumerate(response): - with st.chat_message("assistant"): - st.write(f"Response {index + 1}: {resp}") - st.write("Sources: ") - for src_index, source in enumerate(sources[index]): - st.write(f"\t{src_index}: {source}") -st.spinner("Creating the index...") +if __name__ == "__main__": + app = RAGApp() + app.render() \ No newline at end of file From 7003b7b583d558cf8331d7c2809de7e6c87f34df Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Sat, 30 Mar 2024 10:18:09 -0400 Subject: [PATCH 06/38] Create ~$Capstone5 Presentation v1.pptx --- full_report/~$Capstone5 Presentation v1.pptx | Bin 0 -> 165 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 full_report/~$Capstone5 Presentation v1.pptx diff --git a/full_report/~$Capstone5 Presentation v1.pptx b/full_report/~$Capstone5 Presentation v1.pptx new file mode 100644 index 0000000000000000000000000000000000000000..2425bbdf55b449816083129e5758ed7a0be8728d GIT binary patch literal 165 zcmbu1u?@f=3 Date: Fri, 5 Apr 2024 18:10:32 -0400 Subject: [PATCH 07/38] Add parser dependencies --- pyproject.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5fae3db..f863507 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,7 +44,9 @@ dependencies = [ "rouge-score>=0.1.2", "deeplake>=3.8.27", "bitsandbytes>=0.43.0", - "accelerate>=0.28.0" + "accelerate>=0.28.0", + "poppler-utils>=0.1.0", + "tesseract>=0.1.3" ] [project.optional-dependencies] From f8a8c527dc866212d859b25c62607141d125421e Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Fri, 5 Apr 2024 18:10:42 -0400 Subject: [PATCH 08/38] Config.ini changes --- src/config.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.ini b/src/config.ini index 18abdd9..ebec41a 100644 --- a/src/config.ini +++ b/src/config.ini @@ -39,7 +39,7 @@ chunk_overlap : 400 [multivec_retriever] # store_path: data/docs -store_path : ${data:data_path}/docs +store_path : ${data:data_path}/doc_store # namespace: UUID(8c9040b0-b5cd-4d7c-bc2e-737da1b24ebf) namespace : 8c9040b0b5cd4d7cbc2e737da1b24ebf id_key : doc_id @@ -62,7 +62,7 @@ data_path : ${root:root_path}/data env_path : ${root:root_path}/.env [root] -root_path : /home/ubuntu/volume_2k/Capstone_5 +root_path : /home/ubuntu/Capstone_5 [quantize] llama_cpp_path : ${root:root_path} From a9a95513374df23216c5edceebc5ce418911b45e Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Fri, 5 Apr 2024 18:13:30 -0400 Subject: [PATCH 09/38] Ingest Cookbook async and docs --- cookbook/Basic-RAG/BasicRAG_ingest.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/cookbook/Basic-RAG/BasicRAG_ingest.py b/cookbook/Basic-RAG/BasicRAG_ingest.py index 00e8f0b..6e6192c 100644 --- a/cookbook/Basic-RAG/BasicRAG_ingest.py +++ b/cookbook/Basic-RAG/BasicRAG_ingest.py @@ -1,5 +1,6 @@ """A cookbook demonstrating how to ingest pdf files for use with Basic RAG.""" +import asyncio from pathlib import Path from grag.components.multivec_retriever import Retriever @@ -7,9 +8,19 @@ # from grag.components.vectordb.chroma_client import ChromaClient -client = DeepLakeClient(collection_name="ci_test") -# client = ChromaClient(collection_name="ci_test") +# Initialize the client by giving a collection name - DeepLakeClient or ChromaClient +client = DeepLakeClient(collection_name="arxiv") +# client = ChromaClient(collection_name="arxiv") + +# Initialize the retriever by passing in the client +# Note that: if no client is passed the retriever class will take config from the config file. retriever = Retriever(vectordb=client) -dir_path = Path(__file__).parents[2] / "data/test/pdfs/new_papers" -retriever.ingest(dir_path) +# The path to the folder with pdfs +dir_path = Path(__file__).parents[2] / "data/pdf" + +# Either +# 1. To run synchronously (slow) +# retriever.ingest(dir_path) +# 2. To run asynchronously +asyncio.run(retriever.aingest(dir_path)) From d58d1e5087cccd5eb9210de85ccf8d826df0a437 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Fri, 5 Apr 2024 18:13:46 -0400 Subject: [PATCH 10/38] Async bug retriever --- src/grag/components/multivec_retriever.py | 41 +++++++++++------------ 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/grag/components/multivec_retriever.py b/src/grag/components/multivec_retriever.py index dd9d240..8e87697 100644 --- a/src/grag/components/multivec_retriever.py +++ b/src/grag/components/multivec_retriever.py @@ -4,7 +4,6 @@ - Retriever """ -import asyncio import uuid from pathlib import Path from typing import Any, Dict, List, Optional, Union @@ -44,13 +43,13 @@ class Retriever: """ def __init__( - self, - vectordb: Optional[VectorDB] = None, - store_path: str = multivec_retriever_conf["store_path"], - id_key: str = multivec_retriever_conf["id_key"], - namespace: str = multivec_retriever_conf["namespace"], - top_k=int(multivec_retriever_conf["top_k"]), - client_kwargs: Optional[Dict[str, Any]] = None, + self, + vectordb: Optional[VectorDB] = None, + store_path: str = multivec_retriever_conf["store_path"], + id_key: str = multivec_retriever_conf["id_key"], + namespace: str = multivec_retriever_conf["namespace"], + top_k=int(multivec_retriever_conf["top_k"]), + client_kwargs: Optional[Dict[str, Any]] = None, ): """Initialize the Retriever. @@ -157,7 +156,7 @@ async def aadd_docs(self, docs: List[Document]): """ chunks = self.split_docs(docs) doc_ids = self.gen_doc_ids(docs) - await asyncio.run(self.vectordb.aadd_docs(chunks)) + await self.vectordb.aadd_docs(chunks) self.retriever.docstore.mset(list(zip(doc_ids, docs))) def get_chunk(self, query: str, with_score=False, top_k=None): @@ -237,12 +236,12 @@ def get_docs_from_chunks(self, chunks: List[Document], one_to_one=False): return [d for d in docs if d is not None] def ingest( - self, - dir_path: Union[str, Path], - glob_pattern: str = "**/*.pdf", - dry_run: bool = False, - verbose: bool = True, - parser_kwargs: Optional[Dict[str, Any]] = None, + self, + dir_path: Union[str, Path], + glob_pattern: str = "**/*.pdf", + dry_run: bool = False, + verbose: bool = True, + parser_kwargs: Optional[Dict[str, Any]] = None, ): """Ingests the files in directory. @@ -279,12 +278,12 @@ def ingest( print(f"DRY RUN: found - {filepath.relative_to(dir_path)}") async def aingest( - self, - dir_path: Union[str, Path], - glob_pattern: str = "**/*.pdf", - dry_run: bool = False, - verbose: bool = True, - parser_kwargs: Optional[Dict[str, Any]] = None, + self, + dir_path: Union[str, Path], + glob_pattern: str = "**/*.pdf", + dry_run: bool = False, + verbose: bool = True, + parser_kwargs: Optional[Dict[str, Any]] = None, ): """Asynchronously ingests the files in directory. From b7117f1bc76654085820f96a822f87a728d6c11c Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Fri, 5 Apr 2024 18:13:59 -0400 Subject: [PATCH 11/38] Relocate RAG-GUI to cookbooks --- {projects => cookbook}/RAG-GUI/app.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {projects => cookbook}/RAG-GUI/app.py (100%) diff --git a/projects/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py similarity index 100% rename from projects/RAG-GUI/app.py rename to cookbook/RAG-GUI/app.py From 9f7e1785669fff1e2984ccf674bd19efd5091c46 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Tue, 9 Apr 2024 09:09:28 -0400 Subject: [PATCH 12/38] Update app.py --- projects/RAG-GUI/app.py | 64 +++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/projects/RAG-GUI/app.py b/projects/RAG-GUI/app.py index 4e34c0d..9b1fbb6 100644 --- a/projects/RAG-GUI/app.py +++ b/projects/RAG-GUI/app.py @@ -1,27 +1,38 @@ - - import os import sys from pathlib import Path import streamlit as st - +import tracemalloc sys.path.insert(1, str(Path(os.getcwd()).parents[1])) +st.set_page_config(page_title="RAG") from grag.rag.basic_rag import BasicRAG from grag.components.utils import get_config from grag.components.llm import LLM from grag.components.multivec_retriever import Retriever from grag.components.vectordb.deeplake_client import DeepLakeClient + +@st.cache_resource +def load_config(): + return get_config() + +conf = load_config() + class RAGApp: - def __init__(self): - self.conf = get_config() + def __init__(self,app,conf): + self.app = app + self.conf = conf self.selected_model = None self.temperature = None self.top_k = None self.rag = None self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] + # if 'rag' in st.session_state: + # del st.session_state['rag'] + # self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] + def render_sidebar(self): with st.sidebar: @@ -31,13 +42,27 @@ def render_sidebar(self): self.temperature = st.sidebar.slider('Temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01) self.top_k = st.sidebar.slider('Top-k', min_value=1.0, max_value=5.0, value=1.0, step=1.0) + + def initialize_rag(self): + llm_kwargs = {"temperature": self.temperature} + return BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs) + + + def clear_cache(self): + st.cache_data.clear() + + # @st.cache(suppress_st_warning=True, allow_output_mutation=True) + def render_main(self): + st.title("Welcome to the RAG App") + st.write(f"You have selected the {self.selected_model} model with the following parameters:") st.write(f"Temperature: {self.temperature}") st.write(f"Top-k: {self.top_k}") - llm_kwargs = {"temperature": self.temperature} - self.rag = BasicRAG(model_name=self.selected_model,llm_kwargs=llm_kwargs) + + if 'rag' not in st.session_state: + st.session_state['rag'] = self.initialize_rag() for message in self.messages: with st.chat_message(message["role"]): @@ -47,20 +72,35 @@ def render_main(self): submit_button = st.button("Submit") if submit_button and user_input: self.messages.append({"role": "user", "content": user_input}) - response, sources = self.rag(user_input) + response, sources = st.session_state['rag'](user_input) + + # response, sources = st.session_state['rag'](user_input) for index, resp in enumerate(response): with st.chat_message("assistant"): st.write(f"Response {index + 1}: {resp}") st.write("Retrieved Chunks:") for src_index, source in enumerate(sources[index]): - st.write(f"\t{src_index + 1}: {source.page_content}") + # st.write(f"\t{src_index + 1}: {source.page_content}") + if hasattr(source, 'page_content'): + st.write(f"\t{src_index + 1}: {source.page_content}") + else: + st.write(f"\t{src_index + 1}: {source}") + def render(self): - st.set_page_config(page_title="RAG") + + self.clear_cache() self.render_sidebar() + + self.render_main() + + + if __name__ == "__main__": - app = RAGApp() - app.render() \ No newline at end of file + tracemalloc.start() + app = RAGApp(st,conf) + app.render() + From a50876884d94b3c2987bce8f3c5b5d844d1afc69 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Tue, 9 Apr 2024 17:25:07 -0400 Subject: [PATCH 13/38] Basic RAG read_only defaults --- src/grag/rag/basic_rag.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/grag/rag/basic_rag.py b/src/grag/rag/basic_rag.py index 1b45d9a..cb2e936 100644 --- a/src/grag/rag/basic_rag.py +++ b/src/grag/rag/basic_rag.py @@ -31,19 +31,19 @@ class BasicRAG: """ def __init__( - self, - retriever: Optional[Retriever] = None, - model_name=None, - doc_chain="stuff", - task="QA", - llm_kwargs=None, - retriever_kwargs=None, - custom_prompt: Union[Prompt, FewShotPrompt, None] = None, + self, + retriever: Optional[Retriever] = None, + model_name=None, + doc_chain="stuff", + task="QA", + llm_kwargs=None, + retriever_kwargs=None, + custom_prompt: Union[Prompt, FewShotPrompt, None] = None, ): """Initialize BasicRAG.""" if retriever is None: if retriever_kwargs is None: - self.retriever = Retriever() + self.retriever = Retriever(client_kwargs={'read_only': True}) else: self.retriever = Retriever(**retriever_kwargs) else: From 9424f458a4be6f266218531778e808fef30a0605 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Tue, 9 Apr 2024 19:18:26 -0400 Subject: [PATCH 14/38] Update app.py --- cookbook/RAG-GUI/app.py | 66 ++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 9b1fbb6..d0389c1 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -1,14 +1,16 @@ import os import sys from pathlib import Path - +import time import streamlit as st import tracemalloc sys.path.insert(1, str(Path(os.getcwd()).parents[1])) - +import shutil +import psutil st.set_page_config(page_title="RAG") from grag.rag.basic_rag import BasicRAG from grag.components.utils import get_config +from pathlib import Path from grag.components.llm import LLM from grag.components.multivec_retriever import Retriever from grag.components.vectordb.deeplake_client import DeepLakeClient @@ -26,33 +28,48 @@ def __init__(self,app,conf): self.conf = conf self.selected_model = None self.temperature = None - self.top_k = None - self.rag = None + self.exit_app = False + if 'temperature' in st.session_state: + self.temperature = st.session_state['temperature'] + else: + self.temperature = 0.1 + + if 'top_k' in st.session_state: + self.top_k = st.session_state['top_k'] + else: + self.top_k = 1.0 self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] # if 'rag' in st.session_state: # del st.session_state['rag'] # self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] - def render_sidebar(self): with st.sidebar: st.title('RAG') st.subheader('Models and parameters') - self.selected_model = st.sidebar.selectbox('Choose a model', ['Llama-2-13b-chat', 'Llama-2-7b-chat', 'Mixtral-8x7B-Instruct-v0.1', 'Gemma 7B'], key='selected_model') - self.temperature = st.sidebar.slider('Temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01) - self.top_k = st.sidebar.slider('Top-k', min_value=1.0, max_value=5.0, value=1.0, step=1.0) - + self.selected_model = st.sidebar.selectbox('Choose a model', ['Llama-2-13b-chat', 'Llama-2-7b-chat', + 'Mixtral-8x7B-Instruct-v0.1', 'Gemma 7B'], + key='selected_model') + self.temperature = st.sidebar.slider('Temperature', min_value=0.01, max_value=5.0, value=self.temperature, + step=0.01) + self.top_k = st.sidebar.slider('Top-k', min_value=1.0, max_value=5.0, value=self.top_k, step=1.0) + st.session_state['temperature'] = self.temperature + st.session_state['top_k'] = self.top_k def initialize_rag(self): llm_kwargs = {"temperature": self.temperature} - return BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs) + retriever_kwargs = { + "client_kwargs" : {"read_only": True, + "top_k": self.top_k} + } + return BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs,retriever_kwargs=retriever_kwargs) def clear_cache(self): st.cache_data.clear() # @st.cache(suppress_st_warning=True, allow_output_mutation=True) - + @st.cache(ignore_hash=True) def render_main(self): st.title("Welcome to the RAG App") @@ -90,17 +107,44 @@ def render_main(self): def render(self): + self.clear_cache() self.render_sidebar() self.render_main() + st.cache_data.clear() + st.cache_resource.clear() + self.exit_app = st.sidebar.button("Shut Down") if __name__ == "__main__": + lock_path = Path(conf['root']['root_path']) / '/data/vectordb/test/dataset_lock.lock' + + + if os.path.exists(lock_path): + shutil.rmtree(lock_path) + print('Deleting lock file: {}'.format(lock_path)) + latest_iteration = st.empty() + bar = st.progress(0) + + for i in range(100): + # Update the progress bar with each iteration. + latest_iteration.text(f'Iteration {i + 1}') + bar.progress(i + 1) + time.sleep(0.1) + tracemalloc.start() app = RAGApp(st,conf) app.render() + if app.exit_app: + time.sleep(5) # Give a bit of delay for user experience + pid = os.getpid() + p = psutil.Process(pid) + p.terminate() + + + From bee53d721da40a73a39f2b2af8a0dfb7c97115f7 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Tue, 9 Apr 2024 22:06:17 -0400 Subject: [PATCH 15/38] Update app.py --- cookbook/RAG-GUI/app.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index d0389c1..f17ffcb 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -3,10 +3,10 @@ from pathlib import Path import time import streamlit as st -import tracemalloc + sys.path.insert(1, str(Path(os.getcwd()).parents[1])) import shutil -import psutil + st.set_page_config(page_title="RAG") from grag.rag.basic_rag import BasicRAG from grag.components.utils import get_config @@ -39,9 +39,6 @@ def __init__(self,app,conf): else: self.top_k = 1.0 self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] - # if 'rag' in st.session_state: - # del st.session_state['rag'] - # self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] def render_sidebar(self): with st.sidebar: @@ -136,14 +133,9 @@ def render(self): bar.progress(i + 1) time.sleep(0.1) - tracemalloc.start() + app = RAGApp(st,conf) app.render() - if app.exit_app: - time.sleep(5) # Give a bit of delay for user experience - pid = os.getpid() - p = psutil.Process(pid) - p.terminate() From 9bcc8af1b58bfc3950706e81c781f8439c130ec7 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Mon, 15 Apr 2024 16:00:50 -0400 Subject: [PATCH 16/38] Update app --- cookbook/RAG-GUI/app.py | 44 ++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index f17ffcb..c86cc6e 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -59,8 +59,8 @@ def initialize_rag(self): "client_kwargs" : {"read_only": True, "top_k": self.top_k} } - return BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs,retriever_kwargs=retriever_kwargs) - + rag = BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs,retriever_kwargs=retriever_kwargs) + return rag def clear_cache(self): st.cache_data.clear() @@ -68,7 +68,6 @@ def clear_cache(self): # @st.cache(suppress_st_warning=True, allow_output_mutation=True) @st.cache(ignore_hash=True) def render_main(self): - st.title("Welcome to the RAG App") st.write(f"You have selected the {self.selected_model} model with the following parameters:") @@ -80,28 +79,33 @@ def render_main(self): for message in self.messages: with st.chat_message(message["role"]): - st.write(message["content"]) + st.markdown(message["content"]) user_input = st.text_area("Enter your query:", height=20) submit_button = st.button("Submit") if submit_button and user_input: - self.messages.append({"role": "user", "content": user_input}) - response, sources = st.session_state['rag'](user_input) - - # response, sources = st.session_state['rag'](user_input) - - for index, resp in enumerate(response): - with st.chat_message("assistant"): - st.write(f"Response {index + 1}: {resp}") - st.write("Retrieved Chunks:") - for src_index, source in enumerate(sources[index]): - # st.write(f"\t{src_index + 1}: {source.page_content}") - if hasattr(source, 'page_content'): - st.write(f"\t{src_index + 1}: {source.page_content}") - else: - st.write(f"\t{src_index + 1}: {source}") - + self.messages.append({"role": "user", "content": user_input}) + response, sources = st.session_state['rag'](user_input) + st.write("LLM Output:") + st.text_area(value=response) + st.write("RAG Output:") + # for index, resp in enumerate(rag_output): + # with st.expander(f"Response {index + 1}"): + # st.markdown(resp) + # st.write("Retrieved Chunks:") + # if isinstance(sources[index],(list,tuple)): + # for src_index, source in enumerate(sources[index]): + # if hasattr(source, 'page_content'): + # st.markdown(f"**Chunk {src_index + 1}:**\n{source.page_content}") + # else: + # st.markdown(f"**Chunk {src_index + 1}:**\n{source}") + st.write("Response:") + st.markdown(response) + + st.write("Sources:") + # for index, source in enumerate(sources): + # st.write(f"{index + 1}. {source}") def render(self): From 3e3a3cc1699c621d99ef01c519c34ed6e3d3e4a9 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Mon, 15 Apr 2024 16:24:08 -0400 Subject: [PATCH 17/38] Update app to show sources --- cookbook/RAG-GUI/app.py | 80 ++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index c86cc6e..93c2ec7 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -1,29 +1,30 @@ import os import sys -from pathlib import Path import time +from pathlib import Path + import streamlit as st sys.path.insert(1, str(Path(os.getcwd()).parents[1])) import shutil st.set_page_config(page_title="RAG") -from grag.rag.basic_rag import BasicRAG -from grag.components.utils import get_config from pathlib import Path -from grag.components.llm import LLM -from grag.components.multivec_retriever import Retriever -from grag.components.vectordb.deeplake_client import DeepLakeClient + +from grag.components.utils import get_config +from grag.rag.basic_rag import BasicRAG @st.cache_resource def load_config(): return get_config() + conf = load_config() + class RAGApp: - def __init__(self,app,conf): + def __init__(self, app, conf): self.app = app self.conf = conf self.selected_model = None @@ -56,10 +57,10 @@ def render_sidebar(self): def initialize_rag(self): llm_kwargs = {"temperature": self.temperature} retriever_kwargs = { - "client_kwargs" : {"read_only": True, - "top_k": self.top_k} + "client_kwargs": {"read_only": True, + "top_k": self.top_k} } - rag = BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs,retriever_kwargs=retriever_kwargs) + rag = BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs, retriever_kwargs=retriever_kwargs) return rag def clear_cache(self): @@ -85,46 +86,46 @@ def render_main(self): submit_button = st.button("Submit") if submit_button and user_input: - self.messages.append({"role": "user", "content": user_input}) - response, sources = st.session_state['rag'](user_input) - st.write("LLM Output:") - st.text_area(value=response) - st.write("RAG Output:") - # for index, resp in enumerate(rag_output): - # with st.expander(f"Response {index + 1}"): - # st.markdown(resp) - # st.write("Retrieved Chunks:") - # if isinstance(sources[index],(list,tuple)): - # for src_index, source in enumerate(sources[index]): - # if hasattr(source, 'page_content'): - # st.markdown(f"**Chunk {src_index + 1}:**\n{source.page_content}") - # else: - # st.markdown(f"**Chunk {src_index + 1}:**\n{source}") - st.write("Response:") - st.markdown(response) - - st.write("Sources:") - # for index, source in enumerate(sources): - # st.write(f"{index + 1}. {source}") - def render(self): + self.messages.append({"role": "user", "content": user_input}) + response, sources = st.session_state['rag'](user_input) + st.write("LLM Output:") + st.text_area(value=response) + st.write("RAG Output:") + + with st.expander("Sources"): + for index, source in enumerate(sources): + st.write(f"{index} -> {source}") + # for index, resp in enumerate(rag_output): + # with st.expander(f"Response {index + 1}"): + # st.markdown(resp) + # st.write("Retrieved Chunks:") + # if isinstance(sources[index],(list,tuple)): + # for src_index, source in enumerate(sources[index]): + # if hasattr(source, 'page_content'): + # st.markdown(f"**Chunk {src_index + 1}:**\n{source.page_content}") + # else: + # st.markdown(f"**Chunk {src_index + 1}:**\n{source}") + st.write("Response:") + st.markdown(response) + + st.write("Sources:") + # for index, source in enumerate(sources): + # st.write(f"{index + 1}. {source}") + def render(self): self.clear_cache() self.render_sidebar() - self.render_main() st.cache_data.clear() st.cache_resource.clear() self.exit_app = st.sidebar.button("Shut Down") - - if __name__ == "__main__": lock_path = Path(conf['root']['root_path']) / '/data/vectordb/test/dataset_lock.lock' - if os.path.exists(lock_path): shutil.rmtree(lock_path) print('Deleting lock file: {}'.format(lock_path)) @@ -137,10 +138,5 @@ def render(self): bar.progress(i + 1) time.sleep(0.1) - - app = RAGApp(st,conf) + app = RAGApp(st, conf) app.render() - - - - From c82d516c6222956364296124d6d6195c08c40a76 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Mon, 15 Apr 2024 16:50:24 -0400 Subject: [PATCH 18/38] Update app --- cookbook/RAG-GUI/app.py | 69 ++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 93c2ec7..00ef211 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -1,15 +1,12 @@ import os import sys -import time from pathlib import Path import streamlit as st sys.path.insert(1, str(Path(os.getcwd()).parents[1])) -import shutil st.set_page_config(page_title="RAG") -from pathlib import Path from grag.components.utils import get_config from grag.rag.basic_rag import BasicRAG @@ -28,52 +25,52 @@ def __init__(self, app, conf): self.app = app self.conf = conf self.selected_model = None - self.temperature = None self.exit_app = False - if 'temperature' in st.session_state: - self.temperature = st.session_state['temperature'] - else: - self.temperature = 0.1 - - if 'top_k' in st.session_state: - self.top_k = st.session_state['top_k'] - else: - self.top_k = 1.0 + self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] def render_sidebar(self): with st.sidebar: st.title('RAG') st.subheader('Models and parameters') - self.selected_model = st.sidebar.selectbox('Choose a model', ['Llama-2-13b-chat', 'Llama-2-7b-chat', - 'Mixtral-8x7B-Instruct-v0.1', 'Gemma 7B'], - key='selected_model') - self.temperature = st.sidebar.slider('Temperature', min_value=0.01, max_value=5.0, value=self.temperature, - step=0.01) - self.top_k = st.sidebar.slider('Top-k', min_value=1.0, max_value=5.0, value=self.top_k, step=1.0) - st.session_state['temperature'] = self.temperature - st.session_state['top_k'] = self.top_k + st.sidebar.selectbox('Choose a model', + ['Llama-2-13b-chat', 'Llama-2-7b-chat', + 'Mixtral-8x7B-Instruct-v0.1', 'Gemma 7B'], + key='selected_model') + st.sidebar.slider('Temperature', + min_value=0.01, + max_value=5.0, + value=0.1, + step=0.01, + key='temperature') + st.sidebar.slider('Top-k', + min_value=1, + max_value=5, + value=3, + step=1, + key='top_k') def initialize_rag(self): - llm_kwargs = {"temperature": self.temperature} + llm_kwargs = {"temperature": st.session_state['temperature']} retriever_kwargs = { - "client_kwargs": {"read_only": True, - "top_k": self.top_k} + "client_kwargs": {"read_only": True, }, + "top_k": st.session_state['top_k'] } - rag = BasicRAG(model_name=self.selected_model, llm_kwargs=llm_kwargs, retriever_kwargs=retriever_kwargs) + rag = BasicRAG(model_name=st.session_state['selected_model'], + llm_kwargs=llm_kwargs, + retriever_kwargs=retriever_kwargs) return rag def clear_cache(self): st.cache_data.clear() # @st.cache(suppress_st_warning=True, allow_output_mutation=True) - @st.cache(ignore_hash=True) def render_main(self): st.title("Welcome to the RAG App") - st.write(f"You have selected the {self.selected_model} model with the following parameters:") - st.write(f"Temperature: {self.temperature}") - st.write(f"Top-k: {self.top_k}") + st.write(f"You have selected the {st.session_state['selected_model']} model with the following parameters:") + st.write(f"Temperature: {st.session_state['temperature']}") + st.write(f"Top-k: {st.session_state['top_k']}") if 'rag' not in st.session_state: st.session_state['rag'] = self.initialize_rag() @@ -124,19 +121,5 @@ def render(self): if __name__ == "__main__": - lock_path = Path(conf['root']['root_path']) / '/data/vectordb/test/dataset_lock.lock' - - if os.path.exists(lock_path): - shutil.rmtree(lock_path) - print('Deleting lock file: {}'.format(lock_path)) - latest_iteration = st.empty() - bar = st.progress(0) - - for i in range(100): - # Update the progress bar with each iteration. - latest_iteration.text(f'Iteration {i + 1}') - bar.progress(i + 1) - time.sleep(0.1) - app = RAGApp(st, conf) app.render() From 352f8495e248372a6d33a68c9d26c390dda3fe02 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Mon, 15 Apr 2024 17:06:22 -0400 Subject: [PATCH 19/38] Update App --- cookbook/RAG-GUI/app.py | 99 +++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 00ef211..15de2e7 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -12,7 +12,7 @@ from grag.rag.basic_rag import BasicRAG -@st.cache_resource +@st.cache_data def load_config(): return get_config() @@ -49,17 +49,31 @@ def render_sidebar(self): value=3, step=1, key='top_k') - - def initialize_rag(self): + st.button('Load Model', on_click=self.load_rag()) + + # def initialize_rag(self): + # llm_kwargs = {"temperature": st.session_state['temperature']} + # retriever_kwargs = { + # "client_kwargs": {"read_only": True, }, + # "top_k": st.session_state['top_k'] + # } + # rag = BasicRAG(model_name=st.session_state['selected_model'], + # llm_kwargs=llm_kwargs, + # retriever_kwargs=retriever_kwargs) + # return rag + + @staticmethod + def load_rag(): + if 'rag' in st.session_state: + del st.session_state['rag'] llm_kwargs = {"temperature": st.session_state['temperature']} retriever_kwargs = { "client_kwargs": {"read_only": True, }, "top_k": st.session_state['top_k'] } - rag = BasicRAG(model_name=st.session_state['selected_model'], - llm_kwargs=llm_kwargs, - retriever_kwargs=retriever_kwargs) - return rag + st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], + llm_kwargs=llm_kwargs, + retriever_kwargs=retriever_kwargs) def clear_cache(self): st.cache_data.clear() @@ -71,43 +85,42 @@ def render_main(self): st.write(f"You have selected the {st.session_state['selected_model']} model with the following parameters:") st.write(f"Temperature: {st.session_state['temperature']}") st.write(f"Top-k: {st.session_state['top_k']}") - if 'rag' not in st.session_state: - st.session_state['rag'] = self.initialize_rag() - - for message in self.messages: - with st.chat_message(message["role"]): - st.markdown(message["content"]) - - user_input = st.text_area("Enter your query:", height=20) - submit_button = st.button("Submit") - if submit_button and user_input: - - self.messages.append({"role": "user", "content": user_input}) - response, sources = st.session_state['rag'](user_input) - st.write("LLM Output:") - st.text_area(value=response) - st.write("RAG Output:") - - with st.expander("Sources"): - for index, source in enumerate(sources): - st.write(f"{index} -> {source}") - # for index, resp in enumerate(rag_output): - # with st.expander(f"Response {index + 1}"): - # st.markdown(resp) - # st.write("Retrieved Chunks:") - # if isinstance(sources[index],(list,tuple)): - # for src_index, source in enumerate(sources[index]): - # if hasattr(source, 'page_content'): - # st.markdown(f"**Chunk {src_index + 1}:**\n{source.page_content}") - # else: - # st.markdown(f"**Chunk {src_index + 1}:**\n{source}") - st.write("Response:") - st.markdown(response) - - st.write("Sources:") - # for index, source in enumerate(sources): - # st.write(f"{index + 1}. {source}") + st.write("You have not loaded the model") + else: + for message in self.messages: + with st.chat_message(message["role"]): + st.markdown(message["content"]) + + user_input = st.text_area("Enter your query:", height=20) + submit_button = st.button("Submit") + + if submit_button and user_input: + self.messages.append({"role": "user", "content": user_input}) + response, sources = st.session_state['rag'](user_input) + st.write("LLM Output:") + st.text_area(value=response) + st.write("RAG Output:") + + with st.expander("Sources"): + for index, source in enumerate(sources): + st.write(f"{index} -> {source}") + # for index, resp in enumerate(rag_output): + # with st.expander(f"Response {index + 1}"): + # st.markdown(resp) + # st.write("Retrieved Chunks:") + # if isinstance(sources[index],(list,tuple)): + # for src_index, source in enumerate(sources[index]): + # if hasattr(source, 'page_content'): + # st.markdown(f"**Chunk {src_index + 1}:**\n{source.page_content}") + # else: + # st.markdown(f"**Chunk {src_index + 1}:**\n{source}") + st.write("Response:") + st.markdown(response) + + st.write("Sources:") + # for index, source in enumerate(sources): + # st.write(f"{index + 1}. {source}") def render(self): From b21f8adcd1fa1f23821e0e532e4e7659c679e679 Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Mon, 15 Apr 2024 18:19:49 -0400 Subject: [PATCH 20/38] changed UI, added content retrieval --- cookbook/RAG-GUI/app.py | 80 ++++++++++++--------------------------- src/grag/rag/basic_rag.py | 30 +++++++-------- 2 files changed, 39 insertions(+), 71 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 15de2e7..6dbc31e 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -27,15 +27,13 @@ def __init__(self, app, conf): self.selected_model = None self.exit_app = False - self.messages = [{"role": "assistant", "content": "How may I assist you today?"}] - def render_sidebar(self): with st.sidebar: st.title('RAG') st.subheader('Models and parameters') st.sidebar.selectbox('Choose a model', ['Llama-2-13b-chat', 'Llama-2-7b-chat', - 'Mixtral-8x7B-Instruct-v0.1', 'Gemma 7B'], + 'Mixtral-8x7B-Instruct-v0.1', 'gemma-7b-it'], key='selected_model') st.sidebar.slider('Temperature', min_value=0.01, @@ -49,24 +47,21 @@ def render_sidebar(self): value=3, step=1, key='top_k') - st.button('Load Model', on_click=self.load_rag()) - - # def initialize_rag(self): - # llm_kwargs = {"temperature": st.session_state['temperature']} - # retriever_kwargs = { - # "client_kwargs": {"read_only": True, }, - # "top_k": st.session_state['top_k'] - # } - # rag = BasicRAG(model_name=st.session_state['selected_model'], - # llm_kwargs=llm_kwargs, - # retriever_kwargs=retriever_kwargs) - # return rag - - @staticmethod - def load_rag(): + st.button('Load Model', on_click=self.load_rag) + st.checkbox('Show retrieved content', key='show_content') + + def load_rag(self): if 'rag' in st.session_state: del st.session_state['rag'] + llm_kwargs = {"temperature": st.session_state['temperature']} + if st.session_state['selected_model'] == "Mixtral-8x7B-Instruct-v0.1": + llm_kwargs['n_gpu_layers'] = 16 + llm_kwargs['quantization'] = 'Q4_K_M' + elif st.session_state['selected_model'] == "gemma-7b-it": + llm_kwargs['n_gpu_layers'] = 18 + llm_kwargs['quantization'] = 'f16' + retriever_kwargs = { "client_kwargs": {"read_only": True, }, "top_k": st.session_state['top_k'] @@ -74,63 +69,38 @@ def load_rag(): st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], llm_kwargs=llm_kwargs, retriever_kwargs=retriever_kwargs) + st.session_state['loaded_temp'] = st.session_state['temperature'] + st.session_state['loaded_k'] = st.session_state['top_k'] + st.session_state['loaded_model'] = st.session_state['selected_model'] def clear_cache(self): st.cache_data.clear() - # @st.cache(suppress_st_warning=True, allow_output_mutation=True) def render_main(self): st.title("Welcome to the RAG App") - st.write(f"You have selected the {st.session_state['selected_model']} model with the following parameters:") - st.write(f"Temperature: {st.session_state['temperature']}") - st.write(f"Top-k: {st.session_state['top_k']}") if 'rag' not in st.session_state: st.write("You have not loaded the model") else: - for message in self.messages: - with st.chat_message(message["role"]): - st.markdown(message["content"]) + st.write(f"Model: {st.session_state['loaded_model']}") + st.write(f"Temperature: {st.session_state['loaded_temp']}") + st.write(f"Top-k: {st.session_state['loaded_k']}") user_input = st.text_area("Enter your query:", height=20) submit_button = st.button("Submit") if submit_button and user_input: - self.messages.append({"role": "user", "content": user_input}) - response, sources = st.session_state['rag'](user_input) - st.write("LLM Output:") - st.text_area(value=response) - st.write("RAG Output:") - + response, retrieved_docs = st.session_state['rag'](user_input) + st.text_area(value=response, label='Response') with st.expander("Sources"): - for index, source in enumerate(sources): - st.write(f"{index} -> {source}") - # for index, resp in enumerate(rag_output): - # with st.expander(f"Response {index + 1}"): - # st.markdown(resp) - # st.write("Retrieved Chunks:") - # if isinstance(sources[index],(list,tuple)): - # for src_index, source in enumerate(sources[index]): - # if hasattr(source, 'page_content'): - # st.markdown(f"**Chunk {src_index + 1}:**\n{source.page_content}") - # else: - # st.markdown(f"**Chunk {src_index + 1}:**\n{source}") - st.write("Response:") - st.markdown(response) - - st.write("Sources:") - # for index, source in enumerate(sources): - # st.write(f"{index + 1}. {source}") + for index, doc in enumerate(retrieved_docs): + st.markdown(f"**{index}. {doc.metadata['source']}**") + if st.session_state['show_content']: + st.text(f"{doc.page_content}") def render(self): - - self.clear_cache() self.render_sidebar() - self.render_main() - st.cache_data.clear() - st.cache_resource.clear() - self.exit_app = st.sidebar.button("Shut Down") if __name__ == "__main__": diff --git a/src/grag/rag/basic_rag.py b/src/grag/rag/basic_rag.py index cb2e936..7587783 100644 --- a/src/grag/rag/basic_rag.py +++ b/src/grag/rag/basic_rag.py @@ -31,14 +31,14 @@ class BasicRAG: """ def __init__( - self, - retriever: Optional[Retriever] = None, - model_name=None, - doc_chain="stuff", - task="QA", - llm_kwargs=None, - retriever_kwargs=None, - custom_prompt: Union[Prompt, FewShotPrompt, None] = None, + self, + retriever: Optional[Retriever] = None, + model_name=None, + doc_chain="stuff", + task="QA", + llm_kwargs=None, + retriever_kwargs=None, + custom_prompt: Union[Prompt, FewShotPrompt, None] = None, ): """Initialize BasicRAG.""" if retriever is None: @@ -163,14 +163,14 @@ def output_parser(call_func): """Decorator to format llm output.""" def output_parser_wrapper(*args, **kwargs): - response, sources = call_func(*args, **kwargs) + response, retrieved_docs = call_func(*args, **kwargs) if conf["llm"]["std_out"] == "False": # if self.llm_.callback_manager is None: print(response) print("Sources: ") - for index, source in enumerate(sources): - print(f"\t{index}: {source}") - return response, sources + for index, doc in enumerate(retrieved_docs): + print(f"\t{index}: {doc.metadata['source']}") + return response, retrieved_docs return output_parser_wrapper @@ -181,14 +181,12 @@ def stuff_call(self, query: str): context = self.stuff_docs(retrieved_docs) prompt = self.main_prompt.format(context=context, question=query) response = self.llm.invoke(prompt) - sources = [doc.metadata["source"] for doc in retrieved_docs] - return response, sources + return response, retrieved_docs @output_parser def refine_call(self, query: str): """Call function for refine chain.""" retrieved_docs = self.retriever.get_chunk(query) - sources = [doc.metadata["source"] for doc in retrieved_docs] responses = [] for index, doc in enumerate(retrieved_docs): if index == 0: @@ -205,7 +203,7 @@ def refine_call(self, query: str): ) response = self.llm.invoke(prompt) responses.append(response) - return responses, sources + return responses, retrieved_docs def __call__(self, query: str): """Call function for the class.""" From a744b23e2fdd2fae9119e9820898dd6d0b88cdf3 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Tue, 16 Apr 2024 16:29:29 -0400 Subject: [PATCH 21/38] Update app.py --- cookbook/RAG-GUI/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 6dbc31e..33806bd 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -37,7 +37,7 @@ def render_sidebar(self): key='selected_model') st.sidebar.slider('Temperature', min_value=0.01, - max_value=5.0, + max_value=1.0, value=0.1, step=0.01, key='temperature') From fb27ea78869ca35a2268b8d2b4961ff82b991dc6 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Tue, 16 Apr 2024 16:40:40 -0400 Subject: [PATCH 22/38] Update app.py --- cookbook/RAG-GUI/app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 33806bd..694b5a7 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -36,7 +36,7 @@ def render_sidebar(self): 'Mixtral-8x7B-Instruct-v0.1', 'gemma-7b-it'], key='selected_model') st.sidebar.slider('Temperature', - min_value=0.01, + min_value=0.1, max_value=1.0, value=0.1, step=0.01, @@ -96,7 +96,7 @@ def render_main(self): for index, doc in enumerate(retrieved_docs): st.markdown(f"**{index}. {doc.metadata['source']}**") if st.session_state['show_content']: - st.text(f"{doc.page_content}") + st.markdown(f"**{doc.page_content}**") def render(self): self.render_sidebar() From 789d253a0d14fc6c1b3c9ee36ea8262a4157c161 Mon Sep 17 00:00:00 2001 From: Kunal-1669 Date: Tue, 16 Apr 2024 16:42:26 -0400 Subject: [PATCH 23/38] Update app.py --- cookbook/RAG-GUI/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 694b5a7..19403c2 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -39,7 +39,7 @@ def render_sidebar(self): min_value=0.1, max_value=1.0, value=0.1, - step=0.01, + step=0.1, key='temperature') st.sidebar.slider('Top-k', min_value=1, From 8d76ae08d21865ae2f2f9ec9e2c3077ad12bdfbe Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Tue, 16 Apr 2024 20:24:21 -0400 Subject: [PATCH 24/38] added streaming for LLMs --- cookbook/RAG-GUI/app.py | 36 ++++++++++++++++++++++++++++++------ src/config.ini | 2 +- src/grag/components/llm.py | 3 ++- src/grag/rag/basic_rag.py | 38 +++++++++++++++++++++++++++++++------- 4 files changed, 64 insertions(+), 15 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 19403c2..418a626 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -3,14 +3,14 @@ from pathlib import Path import streamlit as st +from grag.components.utils import get_config +from grag.rag.basic_rag import BasicRAG +from langchain.callbacks.base import BaseCallbackHandler sys.path.insert(1, str(Path(os.getcwd()).parents[1])) st.set_page_config(page_title="RAG") -from grag.components.utils import get_config -from grag.rag.basic_rag import BasicRAG - @st.cache_data def load_config(): @@ -20,6 +20,22 @@ def load_config(): conf = load_config() +class StreamHandler(BaseCallbackHandler): + def __init__(self, container, initial_text="", display_method='markdown'): + self.container = container + self.text = initial_text + # self.display_method = display_method + + def on_llm_new_token(self, token: str, **kwargs) -> None: + self.text += token # + "/" + self.container.markdown(self.text) + # display_function = getattr(self.container, self.display_method) + # if display_function is not None: + # display_function(self.text) + # else: + # raise ValueError(f"Invalid display_method: {self.display_method}") + + class RAGApp: def __init__(self, app, conf): self.app = app @@ -54,7 +70,9 @@ def load_rag(self): if 'rag' in st.session_state: del st.session_state['rag'] - llm_kwargs = {"temperature": st.session_state['temperature']} + llm_kwargs = {"temperature": st.session_state['temperature'], } + # "callbacks": CallbackManager([self.stream_handler]), + # "std_out": False} if st.session_state['selected_model'] == "Mixtral-8x7B-Instruct-v0.1": llm_kwargs['n_gpu_layers'] = 16 llm_kwargs['quantization'] = 'Q4_K_M' @@ -66,6 +84,7 @@ def load_rag(self): "client_kwargs": {"read_only": True, }, "top_k": st.session_state['top_k'] } + st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], llm_kwargs=llm_kwargs, retriever_kwargs=retriever_kwargs) @@ -90,8 +109,13 @@ def render_main(self): submit_button = st.button("Submit") if submit_button and user_input: - response, retrieved_docs = st.session_state['rag'](user_input) - st.text_area(value=response, label='Response') + # response, retrieved_docs = st.session_state['rag'](user_input) + # st.markdown(value=response, label='Response') + # stream_handler = StreamHandler(chat_box, display_method='write') + response, retrieved_docs = st.write_stream( + # st.session_state['rag'](user_input, CallbackManager([stream_handler])) + st.session_state['rag'](user_input) + ) with st.expander("Sources"): for index, doc in enumerate(retrieved_docs): st.markdown(f"**{index}. {doc.metadata['source']}**") diff --git a/src/config.ini b/src/config.ini index ebec41a..b382a79 100644 --- a/src/config.ini +++ b/src/config.ini @@ -62,7 +62,7 @@ data_path : ${root:root_path}/data env_path : ${root:root_path}/.env [root] -root_path : /home/ubuntu/Capstone_5 +root_path : /home/ubuntu/volume_2k/Capstone_5 [quantize] llama_cpp_path : ${root:root_path} diff --git a/src/grag/components/llm.py b/src/grag/components/llm.py index bf0665d..a0e0e4b 100644 --- a/src/grag/components/llm.py +++ b/src/grag/components/llm.py @@ -50,6 +50,7 @@ def __init__( base_dir=llm_conf["base_dir"], quantization=llm_conf["quantization"], pipeline=llm_conf["pipeline"], + callbacks=None, ): """Initialize the LLM class using the given parameters.""" self.base_dir = Path(base_dir) @@ -66,7 +67,7 @@ def __init__( if std_out: self.callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) else: - self.callback_manager = None + self.callback_manager = callbacks @property def model_name(self): diff --git a/src/grag/rag/basic_rag.py b/src/grag/rag/basic_rag.py index 7587783..e89c85a 100644 --- a/src/grag/rag/basic_rag.py +++ b/src/grag/rag/basic_rag.py @@ -38,6 +38,7 @@ def __init__( task="QA", llm_kwargs=None, retriever_kwargs=None, + stream: bool = True, custom_prompt: Union[Prompt, FewShotPrompt, None] = None, ): """Initialize BasicRAG.""" @@ -61,6 +62,7 @@ def __init__( self.model_name = model_name self.doc_chain = doc_chain self.task = task + self.stream = stream if self.custom_prompt is None: self.main_prompt = Prompt.load( @@ -174,21 +176,27 @@ def output_parser_wrapper(*args, **kwargs): return output_parser_wrapper - @output_parser - def stuff_call(self, query: str): - """Call function for stuff chain.""" + def stuff_chain(self, query: str): retrieved_docs = self.retriever.get_chunk(query) context = self.stuff_docs(retrieved_docs) prompt = self.main_prompt.format(context=context, question=query) - response = self.llm.invoke(prompt) - return response, retrieved_docs + return prompt, retrieved_docs @output_parser - def refine_call(self, query: str): + def stuff_call(self, query: str): + """Call function for stuff chain.""" + prompt, retrieved_docs = self.stuff_chain(query) + if self.stream: + response = self.llm.stream(prompt) + else: + response = self.llm.invoke(prompt) + return response, retrieved_docs + + def refine_chain(self, query: str): """Call function for refine chain.""" retrieved_docs = self.retriever.get_chunk(query) responses = [] - for index, doc in enumerate(retrieved_docs): + for index, doc in enumerate(retrieved_docs[:-1]): if index == 0: prompt = self.main_prompt.format( context=doc.page_content, question=query @@ -203,6 +211,22 @@ def refine_call(self, query: str): ) response = self.llm.invoke(prompt) responses.append(response) + prompt = self.refine_prompt.format( + context=retrieved_docs[-1].page_content, + question=query, + existing_answer=responses[-1] + ) + return prompt, retrieved_docs, responses + + @output_parser + def refine_call(self, query: str): + """Call function for refine chain.""" + prompt, retrieved_docs, responses = self.refine_chain(query) + if self.stream: + response = self.llm.stream(prompt) + else: + response = self.llm.invoke(prompt) + responses.append(response) return responses, retrieved_docs def __call__(self, query: str): From ff6617243a2351ebde0f51a17ad31190705c9d75 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Wed, 17 Apr 2024 16:55:41 -0400 Subject: [PATCH 25/38] Update app Model load success message, and no model loaded warning. --- cookbook/RAG-GUI/app.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 418a626..c67d495 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -24,7 +24,7 @@ class StreamHandler(BaseCallbackHandler): def __init__(self, container, initial_text="", display_method='markdown'): self.container = container self.text = initial_text - # self.display_method = display_method + self.display_method = display_method def on_llm_new_token(self, token: str, **kwargs) -> None: self.text += token # + "/" @@ -35,6 +35,14 @@ def on_llm_new_token(self, token: str, **kwargs) -> None: # else: # raise ValueError(f"Invalid display_method: {self.display_method}") + def on_llm_new_token(self, token: str, **kwargs) -> None: + self.text += token # + "/" + display_function = getattr(self.container, self.display_method) + if display_function is not None: + display_function(self.text) + else: + raise ValueError(f"Invalid display_method: {self.display_method}") + class RAGApp: def __init__(self, app, conf): @@ -71,8 +79,6 @@ def load_rag(self): del st.session_state['rag'] llm_kwargs = {"temperature": st.session_state['temperature'], } - # "callbacks": CallbackManager([self.stream_handler]), - # "std_out": False} if st.session_state['selected_model'] == "Mixtral-8x7B-Instruct-v0.1": llm_kwargs['n_gpu_layers'] = 16 llm_kwargs['quantization'] = 'Q4_K_M' @@ -88,9 +94,20 @@ def load_rag(self): st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], llm_kwargs=llm_kwargs, retriever_kwargs=retriever_kwargs) - st.session_state['loaded_temp'] = st.session_state['temperature'] - st.session_state['loaded_k'] = st.session_state['top_k'] - st.session_state['loaded_model'] = st.session_state['selected_model'] + # st.session_state['loaded_temp'] = st.session_state['temperature'] + # st.session_state['loaded_k'] = st.session_state['top_k'] + # st.session_state['loaded_model'] = st.session_state['selected_model'] + # st.write(f"Model: {st.session_state['loaded_model']}") + # st.write(f"Temperature: {st.session_state['loaded_temp']}") + # st.write(f"Top-k: {st.session_state['loaded_k']}") + st.success( + f"""Model Loaded !!! + + Model Name: {st.session_state['selected_model']} + Temerature: {st.session_state['temperature']} + Top-k : {st.session_state['top_k']}""" + # f"""{st.session_state['selected_model']} is loaded temp:{st.session_state['temperature']} and top-k: {st.session_state['top_k']}" + ) def clear_cache(self): st.cache_data.clear() @@ -99,21 +116,14 @@ def render_main(self): st.title("Welcome to the RAG App") if 'rag' not in st.session_state: - st.write("You have not loaded the model") + st.warning("You have not loaded any model") else: - st.write(f"Model: {st.session_state['loaded_model']}") - st.write(f"Temperature: {st.session_state['loaded_temp']}") - st.write(f"Top-k: {st.session_state['loaded_k']}") user_input = st.text_area("Enter your query:", height=20) submit_button = st.button("Submit") if submit_button and user_input: - # response, retrieved_docs = st.session_state['rag'](user_input) - # st.markdown(value=response, label='Response') - # stream_handler = StreamHandler(chat_box, display_method='write') response, retrieved_docs = st.write_stream( - # st.session_state['rag'](user_input, CallbackManager([stream_handler])) st.session_state['rag'](user_input) ) with st.expander("Sources"): From df7885650632fdf026aee45f7a6608e4e9a7132c Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Wed, 17 Apr 2024 17:31:10 -0400 Subject: [PATCH 26/38] Add spinner decorator --- cookbook/RAG-GUI/app.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index c67d495..4d8712f 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -12,6 +12,17 @@ st.set_page_config(page_title="RAG") +def spinner(text): + def _spinner(func): + def wrapper_func(*args, **kwargs): + with st.spinner(text=text): + func(*args, **kwargs) + + return wrapper_func + + return _spinner + + @st.cache_data def load_config(): return get_config() @@ -74,6 +85,7 @@ def render_sidebar(self): st.button('Load Model', on_click=self.load_rag) st.checkbox('Show retrieved content', key='show_content') + @spinner(text='Loading model...') def load_rag(self): if 'rag' in st.session_state: del st.session_state['rag'] @@ -94,12 +106,6 @@ def load_rag(self): st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], llm_kwargs=llm_kwargs, retriever_kwargs=retriever_kwargs) - # st.session_state['loaded_temp'] = st.session_state['temperature'] - # st.session_state['loaded_k'] = st.session_state['top_k'] - # st.session_state['loaded_model'] = st.session_state['selected_model'] - # st.write(f"Model: {st.session_state['loaded_model']}") - # st.write(f"Temperature: {st.session_state['loaded_temp']}") - # st.write(f"Top-k: {st.session_state['loaded_k']}") st.success( f"""Model Loaded !!! From 57eda193c18481f12f19af66844d46326738b589 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Wed, 17 Apr 2024 18:05:51 -0400 Subject: [PATCH 27/38] Update app - Add chat style UI --- cookbook/RAG-GUI/app.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 4d8712f..a3dd20a 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -59,8 +59,6 @@ class RAGApp: def __init__(self, app, conf): self.app = app self.conf = conf - self.selected_model = None - self.exit_app = False def render_sidebar(self): with st.sidebar: @@ -112,7 +110,6 @@ def load_rag(self): Model Name: {st.session_state['selected_model']} Temerature: {st.session_state['temperature']} Top-k : {st.session_state['top_k']}""" - # f"""{st.session_state['selected_model']} is loaded temp:{st.session_state['temperature']} and top-k: {st.session_state['top_k']}" ) def clear_cache(self): @@ -120,27 +117,30 @@ def clear_cache(self): def render_main(self): st.title("Welcome to the RAG App") - if 'rag' not in st.session_state: st.warning("You have not loaded any model") else: - - user_input = st.text_area("Enter your query:", height=20) - submit_button = st.button("Submit") - - if submit_button and user_input: - response, retrieved_docs = st.write_stream( - st.session_state['rag'](user_input) - ) - with st.expander("Sources"): + # user_input = st.text_area("Enter your query:", height=20) + # submit_button = st.button("Submit") + user_input = st.chat_input("Enter your query") + + if user_input: + with st.chat_message("user"): + st.write(user_input) + with st.chat_message("assistant"): + response = st.write_stream( + st.session_state['rag'](user_input)[0] + ) + retrieved_docs = st.session_state['rag'].retriever.get_chunk(user_input) for index, doc in enumerate(retrieved_docs): - st.markdown(f"**{index}. {doc.metadata['source']}**") - if st.session_state['show_content']: - st.markdown(f"**{doc.page_content}**") + with st.expander(f"Source {index + 1}"): + st.markdown(f"**{index + 1}. {doc.metadata['source']}**") + if st.session_state['show_content']: + st.text(f"**{doc.page_content}**") def render(self): - self.render_sidebar() self.render_main() + self.render_sidebar() if __name__ == "__main__": From 39a489a822719f3e7785c57f2a47c6759f19bb18 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Wed, 17 Apr 2024 18:30:31 -0400 Subject: [PATCH 28/38] Update app: cosmetic --- cookbook/RAG-GUI/app.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index a3dd20a..6b95d7d 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -9,7 +9,11 @@ sys.path.insert(1, str(Path(os.getcwd()).parents[1])) -st.set_page_config(page_title="RAG") +st.set_page_config(page_title="GRAG", + menu_items={ + "Get Help": "https://github.com/arjbingly/Capstone_5", + "About": "This is a simple GUI for GRAG" + }) def spinner(text): @@ -62,7 +66,7 @@ def __init__(self, app, conf): def render_sidebar(self): with st.sidebar: - st.title('RAG') + st.title('GRAG') st.subheader('Models and parameters') st.sidebar.selectbox('Choose a model', ['Llama-2-13b-chat', 'Llama-2-7b-chat', @@ -116,13 +120,13 @@ def clear_cache(self): st.cache_data.clear() def render_main(self): - st.title("Welcome to the RAG App") + st.title(":us: US Constitution Expert! :mortar_board:") if 'rag' not in st.session_state: st.warning("You have not loaded any model") else: # user_input = st.text_area("Enter your query:", height=20) # submit_button = st.button("Submit") - user_input = st.chat_input("Enter your query") + user_input = st.chat_input("Ask me anything about the US Constitution.") if user_input: with st.chat_message("user"): From 054b3729b73b18bb0479f16e1be60bc014a8eaba Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Wed, 17 Apr 2024 18:39:56 -0400 Subject: [PATCH 29/38] loading retriever in Class --- cookbook/RAG-GUI/app.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 6b95d7d..0c469ac 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -3,7 +3,9 @@ from pathlib import Path import streamlit as st +from grag.components.multivec_retriever import Retriever from grag.components.utils import get_config +from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG from langchain.callbacks.base import BaseCallbackHandler @@ -104,15 +106,17 @@ def load_rag(self): "client_kwargs": {"read_only": True, }, "top_k": st.session_state['top_k'] } + client = DeepLakeClient(collection_name="usc", read_only=True) + retriever = Retriever(vectordb=client) st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], - llm_kwargs=llm_kwargs, + llm_kwargs=llm_kwargs, retriever=retriever, retriever_kwargs=retriever_kwargs) st.success( f"""Model Loaded !!! Model Name: {st.session_state['selected_model']} - Temerature: {st.session_state['temperature']} + Temperature: {st.session_state['temperature']} Top-k : {st.session_state['top_k']}""" ) From d6470dd88cdd4856cad56c98d540da4ec84f1e1e Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Wed, 17 Apr 2024 18:40:18 -0400 Subject: [PATCH 30/38] added gemma --- src/grag/prompts/matcher.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/grag/prompts/matcher.json b/src/grag/prompts/matcher.json index aea49da..6f109a4 100644 --- a/src/grag/prompts/matcher.json +++ b/src/grag/prompts/matcher.json @@ -5,5 +5,6 @@ "Llama-2-7b": "Llama-2", "Llama-2-13b": "Llama-2", "Llama-2-70b": "Llama-2", - "Mixtral-8x7B-Instruct-v0.1": "Mixtral" + "Mixtral-8x7B-Instruct-v0.1": "Mixtral", + "gemma-7b-it": "Llama-2" } From 0e2c17f30be3606a96f1c8bd52c03cd4c7c20154 Mon Sep 17 00:00:00 2001 From: Arjun Bingly Date: Wed, 17 Apr 2024 19:22:36 -0400 Subject: [PATCH 31/38] Update App: show sources --- cookbook/RAG-GUI/app.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 0c469ac..70bdce0 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -87,7 +87,7 @@ def render_sidebar(self): step=1, key='top_k') st.button('Load Model', on_click=self.load_rag) - st.checkbox('Show retrieved content', key='show_content') + st.checkbox('Show sources', key='show_sources') @spinner(text='Loading model...') def load_rag(self): @@ -128,8 +128,6 @@ def render_main(self): if 'rag' not in st.session_state: st.warning("You have not loaded any model") else: - # user_input = st.text_area("Enter your query:", height=20) - # submit_button = st.button("Submit") user_input = st.chat_input("Ask me anything about the US Constitution.") if user_input: @@ -139,11 +137,12 @@ def render_main(self): response = st.write_stream( st.session_state['rag'](user_input)[0] ) - retrieved_docs = st.session_state['rag'].retriever.get_chunk(user_input) - for index, doc in enumerate(retrieved_docs): - with st.expander(f"Source {index + 1}"): - st.markdown(f"**{index + 1}. {doc.metadata['source']}**") - if st.session_state['show_content']: + if st.session_state['show_sources']: + retrieved_docs = st.session_state['rag'].retriever.get_chunk(user_input) + for index, doc in enumerate(retrieved_docs): + with st.expander(f"Source {index + 1}"): + st.markdown(f"**{index + 1}. {doc.metadata['source']}**") + # if st.session_state['show_content']: st.text(f"**{doc.page_content}**") def render(self): From b5d56eba75d32693ee40ed97a9cba00df1b1da8f Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Wed, 17 Apr 2024 19:38:02 -0400 Subject: [PATCH 32/38] ruff check and formatted --- cookbook/RAG-GUI/app.py | 73 +++++++++++++++++++++++++-------------- src/grag/rag/basic_rag.py | 5 +-- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 70bdce0..61afed9 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -1,3 +1,5 @@ +"""A cookbook demonstrating how to run RAG app on streamlit.""" + import os import sys from pathlib import Path @@ -7,7 +9,6 @@ from grag.components.utils import get_config from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG -from langchain.callbacks.base import BaseCallbackHandler sys.path.insert(1, str(Path(os.getcwd()).parents[1])) @@ -19,8 +20,35 @@ def spinner(text): + """Decorator that displays a loading spinner with a custom text message during the execution of a function. + + This decorator wraps any function to show a spinner using Streamlit's st.spinner during the function call, + indicating that an operation is in progress. The spinner is displayed with a user-defined text message. + + Args: + text (str): The message to display next to the spinner. + + Returns: + function: A decorator that takes a function and wraps it in a spinner context. + """ + def _spinner(func): + """A decorator function that takes another function and wraps it to show a spinner during its execution. + + Args: + func (function): The function to wrap. + + Returns: + function: The wrapped function with a spinner displayed during its execution. + """ + def wrapper_func(*args, **kwargs): + """The wrapper function that actually executes the wrapped function within the spinner context. + + Args: + *args: Positional arguments passed to the wrapped function. + **kwargs: Keyword arguments passed to the wrapped function. + """ with st.spinner(text=text): func(*args, **kwargs) @@ -31,42 +59,33 @@ def wrapper_func(*args, **kwargs): @st.cache_data def load_config(): + """Loads config.""" return get_config() conf = load_config() -class StreamHandler(BaseCallbackHandler): - def __init__(self, container, initial_text="", display_method='markdown'): - self.container = container - self.text = initial_text - self.display_method = display_method - - def on_llm_new_token(self, token: str, **kwargs) -> None: - self.text += token # + "/" - self.container.markdown(self.text) - # display_function = getattr(self.container, self.display_method) - # if display_function is not None: - # display_function(self.text) - # else: - # raise ValueError(f"Invalid display_method: {self.display_method}") - - def on_llm_new_token(self, token: str, **kwargs) -> None: - self.text += token # + "/" - display_function = getattr(self.container, self.display_method) - if display_function is not None: - display_function(self.text) - else: - raise ValueError(f"Invalid display_method: {self.display_method}") +class RAGApp: + """Application class to manage a Retrieval-Augmented Generation (RAG) model interface. + Attributes: + app: The main application or server instance hosting the RAG model. + conf: Configuration settings or parameters for the application. + """ -class RAGApp: def __init__(self, app, conf): + """Initializes the RAGApp with a given application and configuration. + + Args: + app: The main application or framework instance that this class will interact with. + conf: A configuration object or dictionary containing settings for the application. + """ self.app = app self.conf = conf def render_sidebar(self): + """Renders the sidebar in the application interface with model selection and parameters.""" with st.sidebar: st.title('GRAG') st.subheader('Models and parameters') @@ -91,6 +110,7 @@ def render_sidebar(self): @spinner(text='Loading model...') def load_rag(self): + """Loads the specified RAG model based on the user's selection and settings in the sidebar.""" if 'rag' in st.session_state: del st.session_state['rag'] @@ -121,9 +141,11 @@ def load_rag(self): ) def clear_cache(self): + """Clears the cached data within the application.""" st.cache_data.clear() def render_main(self): + """Renders the main chat interface for user interaction with the loaded RAG model.""" st.title(":us: US Constitution Expert! :mortar_board:") if 'rag' not in st.session_state: st.warning("You have not loaded any model") @@ -134,7 +156,7 @@ def render_main(self): with st.chat_message("user"): st.write(user_input) with st.chat_message("assistant"): - response = st.write_stream( + _ = st.write_stream( st.session_state['rag'](user_input)[0] ) if st.session_state['show_sources']: @@ -146,6 +168,7 @@ def render_main(self): st.text(f"**{doc.page_content}**") def render(self): + """Orchestrates the rendering of both main and sidebar components of the application.""" self.render_main() self.render_sidebar() diff --git a/src/grag/rag/basic_rag.py b/src/grag/rag/basic_rag.py index e89c85a..c5144a1 100644 --- a/src/grag/rag/basic_rag.py +++ b/src/grag/rag/basic_rag.py @@ -177,6 +177,7 @@ def output_parser_wrapper(*args, **kwargs): return output_parser_wrapper def stuff_chain(self, query: str): + """Call function for stuff chain.""" retrieved_docs = self.retriever.get_chunk(query) context = self.stuff_docs(retrieved_docs) prompt = self.main_prompt.format(context=context, question=query) @@ -184,7 +185,7 @@ def stuff_chain(self, query: str): @output_parser def stuff_call(self, query: str): - """Call function for stuff chain.""" + """Call function for output of stuff chain.""" prompt, retrieved_docs = self.stuff_chain(query) if self.stream: response = self.llm.stream(prompt) @@ -220,7 +221,7 @@ def refine_chain(self, query: str): @output_parser def refine_call(self, query: str): - """Call function for refine chain.""" + """Call function for output of refine chain.""" prompt, retrieved_docs, responses = self.refine_chain(query) if self.stream: response = self.llm.stream(prompt) From 55a9ce7aafde48185aa0add97497aee7b356e8f3 Mon Sep 17 00:00:00 2001 From: Sanchit Vijay Date: Wed, 17 Apr 2024 19:55:31 -0400 Subject: [PATCH 33/38] Create branch_Jenkinsfile --- ci/branch_Jenkinsfile | 120 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 ci/branch_Jenkinsfile diff --git a/ci/branch_Jenkinsfile b/ci/branch_Jenkinsfile new file mode 100644 index 0000000..f471fc9 --- /dev/null +++ b/ci/branch_Jenkinsfile @@ -0,0 +1,120 @@ +pipeline { + agent any + + options{ + skipDefaultCheckout(true) + } + environment { + PYTHONPATH = "${env.WORKSPACE}/.venv/bin" + CUDACXX = '/usr/local/cuda-12/bin/nvcc' + CMAKE_ARGS = "-DLLAMA_CUBLAS=on" + PATH="/usr/local/cuda-12.3/bin:$PATH" + LD_LIBRARY_PATH="/usr/local/cuda-12.3/lib64:$LD_LIBRARY_PATH" + GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=accept-new" + } + + + stages { + stage('Checkout') { + steps { + cleanWs() + checkout scm + } + } + + stage('Create venv'){ + steps { + sh 'python3 -m venv .venv' + } + } + + stage('Install dependencies'){ + steps { + withPythonEnv(PYTHONPATH){ + sh 'pip install -e .' + } + } + + } + + stage('Config'){ + steps{ + withPythonEnv(PYTHONPATH){ + sh 'python3 ci/modify_config.py' + sh 'rm -rf $JENKINS_HOME/ci_test_data/data/vectordb/ci_test' + sh 'cp -r $JENKINS_HOME/ci_test_data/data/backup_vectordb/ci_test $JENKINS_HOME/ci_test_data/data/vectordb' + } + } + } + + stage('Linting'){ + steps { + withPythonEnv(PYTHONPATH){ + sh 'pip install ruff' + catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){ + sh 'ruff check . --exclude .pyenv-var-lib-jenkins-workspace-capstone_5-.venv-bin --output-format junit -o ruff-report.xml' + sh 'ruff format .' + } + } + } + post { + always{ + withChecks('Lint Checks'){ + junit 'ruff-report.xml' + } + } + } + } + + stage('Static type check'){ + steps { + withPythonEnv(PYTHONPATH){ + sh 'pip install mypy' + catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){ + sh 'python3 -m mypy -p src.grag --junit-xml mypy-report.xml' + } + } + } + post { + always{ + withChecks('Static Type Checks'){ + junit 'mypy-report.xml' + } + } + } + } + + stage('Tests'){ + steps{ + sh 'docker pull chromadb/chroma' + sh 'docker run -d --name jenkins-chroma -p 8000:8000 chromadb/chroma' + withPythonEnv(PYTHONPATH){ + sh 'pip install pytest' + sh 'python3 ci/unlock_deeplake.py' + sh 'pytest src -vvv --junitxml=pytest-report.xml' + } + } + post { + always{ + sh 'docker stop jenkins-chroma' + sh 'docker rm jenkins-chroma' + withChecks('Integration Tests'){ + junit 'pytest-report.xml' + } + } + } + } + } + post { + cleanup{ + cleanWs( + cleanWhenNotBuilt: false, + deleteDirs: true, + disableDeferredWipeout: true, + notFailBuild: true, + patterns: [[pattern: '.gitignore', type: 'INCLUDE'], + [pattern: '.propsfile', type: 'EXCLUDE']] + ) + } + } +} From 944ede5fa7af5acf5b310f5a069b960ecb20a45c Mon Sep 17 00:00:00 2001 From: Sanchit Vijay Date: Wed, 17 Apr 2024 20:07:17 -0400 Subject: [PATCH 34/38] Update branch_Jenkinsfile --- ci/branch_Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/branch_Jenkinsfile b/ci/branch_Jenkinsfile index f471fc9..ce070f8 100644 --- a/ci/branch_Jenkinsfile +++ b/ci/branch_Jenkinsfile @@ -52,7 +52,7 @@ pipeline { withPythonEnv(PYTHONPATH){ sh 'pip install ruff' catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){ - sh 'ruff check . --exclude .pyenv-var-lib-jenkins-workspace-capstone_5-.venv-bin --output-format junit -o ruff-report.xml' + sh 'ruff check . --exclude *venv* --output-format junit -o ruff-report.xml' sh 'ruff format .' } } From a0c88f4ca91a5382d154b71d302ece976dbae3b3 Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Wed, 17 Apr 2024 20:59:18 -0400 Subject: [PATCH 35/38] modifying tests --- src/tests/rag/basic_rag_test.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/tests/rag/basic_rag_test.py b/src/tests/rag/basic_rag_test.py index 1695e93..9fcd69b 100644 --- a/src/tests/rag/basic_rag_test.py +++ b/src/tests/rag/basic_rag_test.py @@ -4,14 +4,17 @@ from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG -client = DeepLakeClient(collection_name="ci_test") +client = DeepLakeClient(collection_name="usc2", read_only=True) retriever = Retriever(vectordb=client) def test_rag_stuff(): rag = BasicRAG(doc_chain="stuff", retriever=retriever, llm_kwargs={"model_name": "Llama-2-7b-chat", "n_gpu_layers": "-1"}) - response, sources = rag("What is Flash Attention?") + response, retrieved_docs = rag("What is Flash Attention?") + sources = [doc.metadata["source"] for doc in retrieved_docs] + print(response) + print(sources) assert isinstance(response, Text) assert isinstance(sources, List) assert all(isinstance(s, str) for s in sources) @@ -21,7 +24,8 @@ def test_rag_stuff(): def test_rag_refine(): rag = BasicRAG(doc_chain="refine", retriever=retriever, llm_kwargs={"model_name": "Llama-2-7b-chat", "n_gpu_layers": "-1"}) - response, sources = rag("What is Flash Attention?") + response, retrieved_docs = rag("What is Flash Attention?") + sources = [doc.metadata["source"] for doc in retrieved_docs] assert isinstance(response, List) assert all(isinstance(s, str) for s in response) assert isinstance(sources, List) From 130946338ae36aa04ce059cfe0eda33d39624c0d Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Wed, 17 Apr 2024 23:01:33 -0400 Subject: [PATCH 36/38] fixed failing tests. --- cookbook/RAG-GUI/app.py | 2 +- src/grag/rag/basic_rag.py | 2 +- src/tests/rag/basic_rag_test.py | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cookbook/RAG-GUI/app.py b/cookbook/RAG-GUI/app.py index 61afed9..6f348c5 100644 --- a/cookbook/RAG-GUI/app.py +++ b/cookbook/RAG-GUI/app.py @@ -129,7 +129,7 @@ def load_rag(self): client = DeepLakeClient(collection_name="usc", read_only=True) retriever = Retriever(vectordb=client) - st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], + st.session_state['rag'] = BasicRAG(model_name=st.session_state['selected_model'], stream=True, llm_kwargs=llm_kwargs, retriever=retriever, retriever_kwargs=retriever_kwargs) st.success( diff --git a/src/grag/rag/basic_rag.py b/src/grag/rag/basic_rag.py index c5144a1..e9ec1b2 100644 --- a/src/grag/rag/basic_rag.py +++ b/src/grag/rag/basic_rag.py @@ -38,7 +38,7 @@ def __init__( task="QA", llm_kwargs=None, retriever_kwargs=None, - stream: bool = True, + stream: bool = False, custom_prompt: Union[Prompt, FewShotPrompt, None] = None, ): """Initialize BasicRAG.""" diff --git a/src/tests/rag/basic_rag_test.py b/src/tests/rag/basic_rag_test.py index 9fcd69b..c0e112b 100644 --- a/src/tests/rag/basic_rag_test.py +++ b/src/tests/rag/basic_rag_test.py @@ -13,8 +13,6 @@ def test_rag_stuff(): llm_kwargs={"model_name": "Llama-2-7b-chat", "n_gpu_layers": "-1"}) response, retrieved_docs = rag("What is Flash Attention?") sources = [doc.metadata["source"] for doc in retrieved_docs] - print(response) - print(sources) assert isinstance(response, Text) assert isinstance(sources, List) assert all(isinstance(s, str) for s in sources) From 376575d962d280c4b1a088c304f5d293793c8796 Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Wed, 17 Apr 2024 23:33:19 -0400 Subject: [PATCH 37/38] updated data --- src/tests/rag/basic_rag_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/rag/basic_rag_test.py b/src/tests/rag/basic_rag_test.py index c0e112b..a1d0eab 100644 --- a/src/tests/rag/basic_rag_test.py +++ b/src/tests/rag/basic_rag_test.py @@ -4,7 +4,7 @@ from grag.components.vectordb.deeplake_client import DeepLakeClient from grag.rag.basic_rag import BasicRAG -client = DeepLakeClient(collection_name="usc2", read_only=True) +client = DeepLakeClient(collection_name="ci_test", read_only=True) retriever = Retriever(vectordb=client) From 85a9ef627208e7625f8fbf052ebe67a1b35fd71d Mon Sep 17 00:00:00 2001 From: sanchitvj Date: Wed, 17 Apr 2024 23:55:20 -0400 Subject: [PATCH 38/38] fixed typing --- src/grag/components/llm.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/grag/components/llm.py b/src/grag/components/llm.py index a0e0e4b..c540877 100644 --- a/src/grag/components/llm.py +++ b/src/grag/components/llm.py @@ -2,6 +2,7 @@ import os from pathlib import Path +from typing import Union import torch from langchain.callbacks.manager import CallbackManager @@ -38,18 +39,18 @@ class LLM: def __init__( self, - model_name=llm_conf["model_name"], - device_map=llm_conf["device_map"], - task=llm_conf["task"], - max_new_tokens=llm_conf["max_new_tokens"], - temperature=llm_conf["temperature"], - n_batch=llm_conf["n_batch_gpu_cpp"], - n_ctx=llm_conf["n_ctx_cpp"], - n_gpu_layers=llm_conf["n_gpu_layers_cpp"], - std_out=llm_conf["std_out"], - base_dir=llm_conf["base_dir"], - quantization=llm_conf["quantization"], - pipeline=llm_conf["pipeline"], + model_name: str = llm_conf["model_name"], + device_map: str = llm_conf["device_map"], + task: str = llm_conf["task"], + max_new_tokens: str = llm_conf["max_new_tokens"], + temperature: str = llm_conf["temperature"], + n_batch: str = llm_conf["n_batch_gpu_cpp"], + n_ctx: str = llm_conf["n_ctx_cpp"], + n_gpu_layers: str = llm_conf["n_gpu_layers_cpp"], + std_out: Union[bool, str] = llm_conf["std_out"], + base_dir: str = llm_conf["base_dir"], + quantization: str = llm_conf["quantization"], + pipeline: str = llm_conf["pipeline"], callbacks=None, ): """Initialize the LLM class using the given parameters.""" @@ -67,7 +68,7 @@ def __init__( if std_out: self.callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) else: - self.callback_manager = callbacks + self.callback_manager = callbacks # type: ignore @property def model_name(self):