From ea9fbb451319067e0ac528898f90774d693c805e Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 26 Aug 2024 13:11:02 -0400 Subject: [PATCH 1/7] Remove trailing whitespace --- settings.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/settings.yaml b/settings.yaml index f030604a3..04592ebe6 100644 --- a/settings.yaml +++ b/settings.yaml @@ -26,20 +26,20 @@ ui: enabled: true path: / default_chat_system_prompt: > - You are a helpful, respectful and honest assistant. + You are a helpful, respectful and honest assistant. Always answer as helpfully as possible and follow ALL given instructions. Do not speculate or make up information. Do not reference any given instructions or context. default_query_system_prompt: > - You can only answer questions about the provided context. - If you know the answer but it is not based in the provided context, don't provide + You can only answer questions about the provided context. + If you know the answer but it is not based in the provided context, don't provide the answer, just state the answer is not in the context provided. default_summarization_system_prompt: > - Provide a comprehensive summary of the provided context information. + Provide a comprehensive summary of the provided context information. The summary should cover all the key points and main ideas presented in - the original text, while also condensing the information into a concise + the original text, while also condensing the information into a concise and easy-to-understand format. Please ensure that the summary includes - relevant details and examples that support the main ideas, while avoiding + relevant details and examples that support the main ideas, while avoiding any unnecessary information or repetition. delete_file_button_enabled: true delete_all_files_button_enabled: true From 079761ea3c0912ba300876b5793d57a1b92739e7 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 26 Aug 2024 13:25:09 -0400 Subject: [PATCH 2/7] Add checkbox to choose between streaming and non-streaming response output --- private_gpt/ui/ui.py | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/private_gpt/ui/ui.py b/private_gpt/ui/ui.py index 2c1dcd3e2..4ad5d6794 100644 --- a/private_gpt/ui/ui.py +++ b/private_gpt/ui/ui.py @@ -98,6 +98,8 @@ def __init__( self._selected_filename = None + self._response_style = True + # Initialize system prompt based on default mode self.mode = MODES[0] self._system_prompt = self._get_default_system_prompt(self.mode) @@ -168,6 +170,12 @@ def build_history() -> list[ChatMessage]: role=MessageRole.SYSTEM, ), ) + def draw_methods(service_type): + service = getattr(self, f'_{service_type}_service') + return { + True: getattr(service, f'stream_{service_type}'), + False: getattr(service, f'{service_type}') + } match mode: case Modes.RAG_MODE: # Use only the selected file for the query @@ -182,18 +190,20 @@ def build_history() -> list[ChatMessage]: docs_ids.append(ingested_document.doc_id) context_filter = ContextFilter(docs_ids=docs_ids) - query_stream = self._chat_service.stream_chat( + methods = draw_methods('chat') + query_stream = methods.get(self._response_style, self._chat_service.stream_chat)( messages=all_messages, use_context=True, - context_filter=context_filter, + context_filter=context_filter ) - yield from yield_deltas(query_stream) + yield from (yield_deltas(query_stream) if self._response_style else [query_stream.response]) case Modes.BASIC_CHAT_MODE: - llm_stream = self._chat_service.stream_chat( + methods = draw_methods('chat') + llm_stream = methods.get(self._response_style, self._chat_service.stream_chat)( messages=all_messages, - use_context=False, + use_context=False ) - yield from yield_deltas(llm_stream) + yield from (yield_deltas(llm_stream) if self._response_style else [llm_stream.response]) case Modes.SEARCH_MODE: response = self._chunks_service.retrieve_relevant( @@ -227,6 +237,15 @@ def build_history() -> list[ChatMessage]: instructions=message, ) yield from yield_tokens(summary_stream) + ''' + methods = draw_methods('summarize') + summary_stream = methods.get(self._response_style, self._summarize_service.stream_summarize)( + use_context=True, + context_filter=context_filter, + instructions=message + ) + yield from yield_tokens(summary_stream) if response_style else summary_stream + ''' # On initialization and on mode change, this function set the system prompt # to the default prompt based on the mode (and user settings). @@ -279,6 +298,9 @@ def _set_current_mode(self, mode: Modes) -> Any: gr.update(value=self._explanation_mode), ] + def _set_response_style(self, response_style: str) -> None: + self._response_style = response_style + def _list_ingested_files(self) -> list[list[str]]: files = set() for ingested_document in self._ingest_service.list_ingested(): @@ -402,6 +424,14 @@ def _build_ui_blocks(self) -> gr.Blocks: max_lines=3, interactive=False, ) + response_style = gr.Checkbox( + label="Response Style: Streaming", + value=self._response_style + ) + response_style.input( + self._set_response_style, + inputs=response_style + ) upload_button = gr.components.UploadButton( "Upload File(s)", type="filepath", From 78fa938ed8b506b3da3693bb6d18d53b2518499c Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 9 Sep 2024 09:03:37 -0400 Subject: [PATCH 3/7] Per requested changes, used ternary for response style within each mode --- private_gpt/ui/ui.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/private_gpt/ui/ui.py b/private_gpt/ui/ui.py index 4ad5d6794..e5c046b92 100644 --- a/private_gpt/ui/ui.py +++ b/private_gpt/ui/ui.py @@ -170,12 +170,6 @@ def build_history() -> list[ChatMessage]: role=MessageRole.SYSTEM, ), ) - def draw_methods(service_type): - service = getattr(self, f'_{service_type}_service') - return { - True: getattr(service, f'stream_{service_type}'), - False: getattr(service, f'{service_type}') - } match mode: case Modes.RAG_MODE: # Use only the selected file for the query @@ -190,18 +184,17 @@ def draw_methods(service_type): docs_ids.append(ingested_document.doc_id) context_filter = ContextFilter(docs_ids=docs_ids) - methods = draw_methods('chat') - query_stream = methods.get(self._response_style, self._chat_service.stream_chat)( - messages=all_messages, - use_context=True, - context_filter=context_filter + query_stream = self._chat_service.stream_chat( + all_messages, use_context=False + ) if self._response_style else self._chat_service.chat( + all_messages, use_context=False ) yield from (yield_deltas(query_stream) if self._response_style else [query_stream.response]) case Modes.BASIC_CHAT_MODE: - methods = draw_methods('chat') - llm_stream = methods.get(self._response_style, self._chat_service.stream_chat)( - messages=all_messages, - use_context=False + llm_stream = self._chat_service.stream_chat( + all_messages, use_context=False + ) if self._response_style else self._chat_service.chat( + all_messages, use_context=False ) yield from (yield_deltas(llm_stream) if self._response_style else [llm_stream.response]) @@ -238,11 +231,10 @@ def draw_methods(service_type): ) yield from yield_tokens(summary_stream) ''' - methods = draw_methods('summarize') - summary_stream = methods.get(self._response_style, self._summarize_service.stream_summarize)( - use_context=True, - context_filter=context_filter, - instructions=message + summary_stream = self._summarize_service.summarize_stream( + all_messages, use_context=False + ) if self._response_style else self._summarize_service.summarize( + all_messages, use_context=False ) yield from yield_tokens(summary_stream) if response_style else summary_stream ''' From 987381247fc715764c632e1dde340b5f8596ac03 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 10 Sep 2024 11:52:25 -0400 Subject: [PATCH 4/7] Revise code for zero errors in make check/test --- private_gpt/ui/ui.py | 63 +++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/private_gpt/ui/ui.py b/private_gpt/ui/ui.py index e5c046b92..23a2ce2c2 100644 --- a/private_gpt/ui/ui.py +++ b/private_gpt/ui/ui.py @@ -184,19 +184,28 @@ def build_history() -> list[ChatMessage]: docs_ids.append(ingested_document.doc_id) context_filter = ContextFilter(docs_ids=docs_ids) - query_stream = self._chat_service.stream_chat( - all_messages, use_context=False - ) if self._response_style else self._chat_service.chat( - all_messages, use_context=False - ) - yield from (yield_deltas(query_stream) if self._response_style else [query_stream.response]) + if self._response_style: + query_stream = self._chat_service.stream_chat( + all_messages, use_context=False + ) + yield from yield_deltas(query_stream) + else: + query_response = self._chat_service.chat( + all_messages, use_context=False + ).response + yield from [query_response] + case Modes.BASIC_CHAT_MODE: - llm_stream = self._chat_service.stream_chat( - all_messages, use_context=False - ) if self._response_style else self._chat_service.chat( - all_messages, use_context=False - ) - yield from (yield_deltas(llm_stream) if self._response_style else [llm_stream.response]) + if self._response_style: + llm_stream = self._chat_service.stream_chat( + all_messages, use_context=False + ) + yield from yield_deltas(llm_stream) + else: + llm_response = self._chat_service.chat( + all_messages, use_context=False + ).response + yield from [llm_response] case Modes.SEARCH_MODE: response = self._chunks_service.retrieve_relevant( @@ -224,20 +233,20 @@ def build_history() -> list[ChatMessage]: docs_ids.append(ingested_document.doc_id) context_filter = ContextFilter(docs_ids=docs_ids) - summary_stream = self._summarize_service.stream_summarize( - use_context=True, - context_filter=context_filter, - instructions=message, - ) - yield from yield_tokens(summary_stream) - ''' - summary_stream = self._summarize_service.summarize_stream( - all_messages, use_context=False - ) if self._response_style else self._summarize_service.summarize( - all_messages, use_context=False - ) - yield from yield_tokens(summary_stream) if response_style else summary_stream - ''' + if self._response_style: + summary_stream = self._summarize_service.stream_summarize( + use_context=True, + context_filter=context_filter, + instructions=message, + ) + yield from yield_tokens(summary_stream) + else: + summary_response = self._summarize_service.summarize( + use_context=True, + context_filter=context_filter, + instructions=message, + ) + yield from summary_response # On initialization and on mode change, this function set the system prompt # to the default prompt based on the mode (and user settings). @@ -290,7 +299,7 @@ def _set_current_mode(self, mode: Modes) -> Any: gr.update(value=self._explanation_mode), ] - def _set_response_style(self, response_style: str) -> None: + def _set_response_style(self, response_style: bool) -> None: self._response_style = response_style def _list_ingested_files(self) -> list[list[str]]: From 955f86b396e4fac0b8b7ba4f181cdea1c308c27e Mon Sep 17 00:00:00 2001 From: Javier Martinez Date: Mon, 16 Sep 2024 17:14:58 +0200 Subject: [PATCH 5/7] fix: black --- private_gpt/ui/ui.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/private_gpt/ui/ui.py b/private_gpt/ui/ui.py index 23a2ce2c2..56136168b 100644 --- a/private_gpt/ui/ui.py +++ b/private_gpt/ui/ui.py @@ -426,12 +426,10 @@ def _build_ui_blocks(self) -> gr.Blocks: interactive=False, ) response_style = gr.Checkbox( - label="Response Style: Streaming", - value=self._response_style + label="Response Style: Streaming", value=self._response_style ) response_style.input( - self._set_response_style, - inputs=response_style + self._set_response_style, inputs=response_style ) upload_button = gr.components.UploadButton( "Upload File(s)", From c0f238024f36331f95f63493c053da6d28cc4870 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 23 Sep 2024 14:15:37 -0400 Subject: [PATCH 6/7] Changed response_style: from bool to str; checkbox to dropdown menu --- private_gpt/ui/ui.py | 108 ++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/private_gpt/ui/ui.py b/private_gpt/ui/ui.py index 56136168b..d538093d8 100644 --- a/private_gpt/ui/ui.py +++ b/private_gpt/ui/ui.py @@ -51,6 +51,17 @@ class Modes(str, Enum): ] +class Styles(str, Enum): + STREAMING = "Streaming" + NON_STREAMING = "Non-Streaming" + + +STYLES: list[Styles] = [ + Styles.STREAMING, + Styles.NON_STREAMING +] + + class Source(BaseModel): file: str page: str @@ -98,12 +109,13 @@ def __init__( self._selected_filename = None - self._response_style = True - # Initialize system prompt based on default mode self.mode = MODES[0] self._system_prompt = self._get_default_system_prompt(self.mode) + # Initialize default response style: Streaming + self.response_style = STYLES[0] + def _chat( self, message: str, history: list[list[str]], mode: Modes, *_: Any ) -> Any: @@ -184,28 +196,30 @@ def build_history() -> list[ChatMessage]: docs_ids.append(ingested_document.doc_id) context_filter = ContextFilter(docs_ids=docs_ids) - if self._response_style: - query_stream = self._chat_service.stream_chat( - all_messages, use_context=False - ) - yield from yield_deltas(query_stream) - else: - query_response = self._chat_service.chat( - all_messages, use_context=False - ).response - yield from [query_response] + match self.response_style: + case Styles.STREAMING: + query_stream = self._chat_service.stream_chat( + all_messages, use_context=False + ) + yield from yield_deltas(query_stream) + case Styles.NON_STREAMING: + query_response = self._chat_service.chat( + all_messages, use_context=False + ).response + yield from [query_response] case Modes.BASIC_CHAT_MODE: - if self._response_style: - llm_stream = self._chat_service.stream_chat( - all_messages, use_context=False - ) - yield from yield_deltas(llm_stream) - else: - llm_response = self._chat_service.chat( - all_messages, use_context=False - ).response - yield from [llm_response] + match self.response_style: + case Styles.STREAMING: + llm_stream = self._chat_service.stream_chat( + all_messages, use_context=False + ) + yield from yield_deltas(llm_stream) + case Styles.NON_STREAMING: + llm_response = self._chat_service.chat( + all_messages, use_context=False + ).response + yield from [llm_response] case Modes.SEARCH_MODE: response = self._chunks_service.retrieve_relevant( @@ -233,20 +247,21 @@ def build_history() -> list[ChatMessage]: docs_ids.append(ingested_document.doc_id) context_filter = ContextFilter(docs_ids=docs_ids) - if self._response_style: - summary_stream = self._summarize_service.stream_summarize( - use_context=True, - context_filter=context_filter, - instructions=message, - ) - yield from yield_tokens(summary_stream) - else: - summary_response = self._summarize_service.summarize( - use_context=True, - context_filter=context_filter, - instructions=message, - ) - yield from summary_response + match self.response_style: + case Styles.STREAMING: + summary_stream = self._summarize_service.stream_summarize( + use_context=True, + context_filter=context_filter, + instructions=message, + ) + yield from yield_tokens(summary_stream) + case Styles.NON_STREAMING: + summary_response = self._summarize_service.summarize( + use_context=True, + context_filter=context_filter, + instructions=message, + ) + yield from summary_response # On initialization and on mode change, this function set the system prompt # to the default prompt based on the mode (and user settings). @@ -299,8 +314,8 @@ def _set_current_mode(self, mode: Modes) -> Any: gr.update(value=self._explanation_mode), ] - def _set_response_style(self, response_style: bool) -> None: - self._response_style = response_style + def _set_current_response_style(self, response_style: Styles) -> Any: + self.response_style = response_style def _list_ingested_files(self) -> list[list[str]]: files = set() @@ -425,11 +440,14 @@ def _build_ui_blocks(self) -> gr.Blocks: max_lines=3, interactive=False, ) - response_style = gr.Checkbox( - label="Response Style: Streaming", value=self._response_style - ) - response_style.input( - self._set_response_style, inputs=response_style + default_response_style = STYLES[0] + response_style = ( + gr.Dropdown( + [response_style.value for response_style in STYLES], + label="Response Style", + value=default_response_style, + interactive=True, + ), ) upload_button = gr.components.UploadButton( "Upload File(s)", @@ -524,6 +542,10 @@ def _build_ui_blocks(self) -> gr.Blocks: self._set_system_prompt, inputs=system_prompt_input, ) + # When response style changes + response_style[0].change( + self._set_current_response_style, inputs=response_style + ) def get_model_label() -> str | None: """Get model label from llm mode setting YAML. From 77c9160b3d92f6dbc431a7fe983b2ed0460da882 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 24 Sep 2024 10:22:56 -0400 Subject: [PATCH 7/7] Fix reformat error (black) --- private_gpt/ui/ui.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/private_gpt/ui/ui.py b/private_gpt/ui/ui.py index d538093d8..e61a88272 100644 --- a/private_gpt/ui/ui.py +++ b/private_gpt/ui/ui.py @@ -56,10 +56,7 @@ class Styles(str, Enum): NON_STREAMING = "Non-Streaming" -STYLES: list[Styles] = [ - Styles.STREAMING, - Styles.NON_STREAMING -] +STYLES: list[Styles] = [Styles.STREAMING, Styles.NON_STREAMING] class Source(BaseModel):