From d5807bb2b009d7398997cbcfc68c100a7482db8a Mon Sep 17 00:00:00 2001 From: Paul Lam Date: Thu, 12 Oct 2023 14:31:21 +0900 Subject: [PATCH 1/5] add itune to dependency --- requirements/build.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/build.txt b/requirements/build.txt index ce83e11..ca7bc9e 100644 --- a/requirements/build.txt +++ b/requirements/build.txt @@ -1,3 +1,4 @@ +itune==0.1.1 pypdf==3.16.0 grobid-tei-xml==0.1.3 nltk==3.8.1 From 12493e0a8913bb1d537a1fb4629e90cc3b933637 Mon Sep 17 00:00:00 2001 From: Paul Lam Date: Thu, 12 Oct 2023 14:31:44 +0900 Subject: [PATCH 2/5] apply itune to similarity_top_k parameter --- mind_palace/app.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mind_palace/app.py b/mind_palace/app.py index 0d01673..7d36830 100644 --- a/mind_palace/app.py +++ b/mind_palace/app.py @@ -3,11 +3,15 @@ import openai import streamlit as st import welcome +from itune import MultiArmedBandit, Tune from llama_index.query_engine import CitationQueryEngine openai.api_key = st.secrets.openai_key xml_dir = "./resources/xmls/dennis-oct-10/" gpt_model = "gpt-3.5-turbo" +itune = Tune(strategy=MultiArmedBandit()) + +itune.load() st.set_page_config(page_title="Q&A with Dennis's PDFs") st.title("Q&A with Dennis's PDFs 💬") @@ -28,7 +32,11 @@ def load_nodes_and_index(xml_dir, model): nodes, vector_index = load_nodes_and_index(xml_dir, gpt_model) -query_engine = CitationQueryEngine.from_args(index=vector_index, verbose=True) +query_engine = CitationQueryEngine.from_args( + index=vector_index, + similarity_top_k=itune.choose(similarity_top_k=[3, 5]), + verbose=True, +) @st.cache_data( @@ -86,3 +94,5 @@ def get_welcome_message(abstracts): ) st.write("original text:") st.write(source_node.node.get_text().split(":", 1)[1]) + +itune.save() From 39838cf300e161ce09fe4c432703ab4c160a76d6 Mon Sep 17 00:00:00 2001 From: Paul Lam Date: Fri, 13 Oct 2023 07:24:14 +0900 Subject: [PATCH 3/5] add chat buttons to rate response --- mind_palace/app.py | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/mind_palace/app.py b/mind_palace/app.py index 7d36830..f1e8c51 100644 --- a/mind_palace/app.py +++ b/mind_palace/app.py @@ -54,15 +54,17 @@ def get_welcome_message(abstracts): if "messages" not in st.session_state.keys(): # Initialize the chat messages history st.session_state.messages = [ { - "role": "assistant", + "role": "ai", "content": get_welcome_message(welcome.parse_abstracts(nodes)), }, { - "role": "assistant", + "role": "ai", "content": "Ask me a question about these papers.", }, ] -else: +# If the last message is from the assistant, clear the chat history to reset the conversation +elif st.session_state.messages[-1]["role"] == "assistant": + print("clearing chat messages!") st.session_state.messages = [] @@ -71,12 +73,26 @@ def get_welcome_message(abstracts): ): # Prompt for user input and save to chat history st.session_state.messages.append({"role": "user", "content": prompt}) + for message in st.session_state.messages: # Display the prior chat messages with st.chat_message(message["role"]): st.write(message["content"]) -# If last message is not from assistant, generate a new response -if st.session_state.messages[-1]["role"] != "assistant": + +def user_clicked_thumbs_up(): + print("user thumbs up") + itune.register_outcome(True) + # itune.save() + + +def user_clicked_thumbs_down(): + print("user thumbs down") + itune.register_outcome(False) + # itune.save() + + +# If last message is from user, generate a new response +if st.session_state.messages[-1]["role"] == "user": with st.chat_message("assistant"): with st.spinner("Thinking..."): response = query_engine.query(prompt) @@ -85,6 +101,22 @@ def get_welcome_message(abstracts): message = {"role": "assistant", "content": response.response} st.session_state.messages.append(message) # Add response to message history + _, col1, col2 = st.columns([7, 1, 1], gap="small") + col1.button( + "👍", + on_click=user_clicked_thumbs_up, + help="Good response", + key="good_response", + use_container_width=True, + ) + col2.button( + "👎", + on_click=user_clicked_thumbs_down, + help="Bad response", + key="bad_response", + use_container_width=True, + ) + st.markdown("### Sources") for i, source_node in enumerate(response.source_nodes): with st.expander(f"[{i + 1}] {source_node.node.metadata['citation']}"): @@ -94,5 +126,3 @@ def get_welcome_message(abstracts): ) st.write("original text:") st.write(source_node.node.get_text().split(":", 1)[1]) - -itune.save() From b82b9140d871c67b3a3221f5a3f7f9d61bacdb96 Mon Sep 17 00:00:00 2001 From: Paul Lam Date: Fri, 13 Oct 2023 07:34:09 +0900 Subject: [PATCH 4/5] fixed messages clearing logic --- mind_palace/app.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mind_palace/app.py b/mind_palace/app.py index f1e8c51..13dae77 100644 --- a/mind_palace/app.py +++ b/mind_palace/app.py @@ -54,19 +54,21 @@ def get_welcome_message(abstracts): if "messages" not in st.session_state.keys(): # Initialize the chat messages history st.session_state.messages = [ { - "role": "ai", + "role": "assistant", "content": get_welcome_message(welcome.parse_abstracts(nodes)), }, { - "role": "ai", + "role": "assistant", "content": "Ask me a question about these papers.", }, ] -# If the last message is from the assistant, clear the chat history to reset the conversation -elif st.session_state.messages[-1]["role"] == "assistant": +# if this refresh is not triggered by user button press, reset the chat messages +elif not st.session_state.rating_button_press: print("clearing chat messages!") st.session_state.messages = [] +# this needs to be reset after checking if the messaages should be cleared +st.session_state.rating_button_press = False if prompt := st.chat_input( "Your question" @@ -82,12 +84,14 @@ def get_welcome_message(abstracts): def user_clicked_thumbs_up(): print("user thumbs up") itune.register_outcome(True) + st.session_state.rating_button_press = True # itune.save() def user_clicked_thumbs_down(): print("user thumbs down") itune.register_outcome(False) + st.session_state.rating_button_press = True # itune.save() From d0f720d66e668b348160a4e69db17d60bb21da17 Mon Sep 17 00:00:00 2001 From: Paul Lam Date: Fri, 13 Oct 2023 07:38:22 +0900 Subject: [PATCH 5/5] rename a var --- mind_palace/app.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/mind_palace/app.py b/mind_palace/app.py index 13dae77..5261cae 100644 --- a/mind_palace/app.py +++ b/mind_palace/app.py @@ -63,12 +63,12 @@ def get_welcome_message(abstracts): }, ] # if this refresh is not triggered by user button press, reset the chat messages -elif not st.session_state.rating_button_press: +elif not st.session_state.rating_button_pressed: print("clearing chat messages!") st.session_state.messages = [] # this needs to be reset after checking if the messaages should be cleared -st.session_state.rating_button_press = False +st.session_state.rating_button_pressed = False if prompt := st.chat_input( "Your question" @@ -81,18 +81,11 @@ def get_welcome_message(abstracts): st.write(message["content"]) -def user_clicked_thumbs_up(): - print("user thumbs up") - itune.register_outcome(True) - st.session_state.rating_button_press = True - # itune.save() - - -def user_clicked_thumbs_down(): - print("user thumbs down") - itune.register_outcome(False) - st.session_state.rating_button_press = True - # itune.save() +def user_clicked_rating(is_good_response): + print(f"user thumbs {'up' if is_good_response else 'down'}") + itune.register_outcome(is_good_response) + itune.save() + st.session_state.rating_button_pressed = True # If last message is from user, generate a new response @@ -108,14 +101,16 @@ def user_clicked_thumbs_down(): _, col1, col2 = st.columns([7, 1, 1], gap="small") col1.button( "👍", - on_click=user_clicked_thumbs_up, + on_click=user_clicked_rating, + args=[True], help="Good response", key="good_response", use_container_width=True, ) col2.button( "👎", - on_click=user_clicked_thumbs_down, + on_click=user_clicked_rating, + args=[False], help="Bad response", key="bad_response", use_container_width=True,