Repository: imartinez/privateGPT
File: private_gpt/ui/ui.py (Python)

Code before:
                    use_context=False,
                )
                yield from yield_deltas(llm_stream)

            case "Search in Docs":
                response = self._chunks_service.retrieve_relevant(
                    text=message, limit=4, prev_next_chunks=0
                )

                sources = Source.curate_sources(response)

                yield "\n\n\n".join(
                    f"{index}. **{source.file} "
                    f"(page {source.page})**\n "
                    f"{source.text}"
                    for index, source in enumerate(sources, start=1)
                )

    # On initialization and on mode change, this function set the system prompt
    # to the default prompt based on the mode (and user settings).
    @staticmethod
    def _get_default_system_prompt(mode: str) -> str:
        p = ""
        match mode:
            case "Query Docs":
                p = "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."
            case "LLM Chat":
                p = settings().local.default_system_prompt or llama_utils.DEFAULT_SYSTEM_PROMPT
            case "Search in Docs":
                # TODO - Verify no prompt needed for doc search (and no default prompt options)
                p = ""
        return p

    def _set_system_prompt(self, system_prompt_input: str) -> None:
        logger.info("Setting system prompt to: {}".format(system_prompt_input))
        self._system_prompt = system_prompt_input

    def _set_current_mode(self, mode: str) -> dict:
        self.mode = mode
        self._set_system_prompt(self._get_default_system_prompt(mode))
        return gr.update(placeholder=self._system_prompt)

    def _list_ingested_files(self) -> list[list[str]]:
        files = set()
        for ingested_document in self._ingest_service.list_ingested():
            if ingested_document.doc_metadata is None:
                # Skipping documents without metadata
                continue
            file_name = ingested_document.doc_metadata.get(

Review comment (refactor):
I'd suggest to use the prompt defined in `settings().local.default_system_prompt` for Query Docs too, instead of the hardcoded one. That way we let users configure the behaviour of Gradio client through settings too. 

We could move this hardcoded text to the documentation as a suggested option. 

Code after:
                    use_context=False,
                )
                yield from yield_deltas(llm_stream)

            case "Search in Docs":
                response = self._chunks_service.retrieve_relevant(
                    text=message, limit=4, prev_next_chunks=0
                )

                sources = Source.curate_sources(response)

                yield "\n\n\n".join(
                    f"{index}. **{source.file} "
                    f"(page {source.page})**\n "
                    f"{source.text}"
                    for index, source in enumerate(sources, start=1)
                )

    # On initialization and on mode change, this function set the system prompt
    # to the default prompt based on the mode (and user settings).
    @staticmethod
    def _get_default_system_prompt(mode: str) -> str:
        p = ""
        match mode:
            # For query chat mode, obtain default system prompt from settings
            case "Query Docs":
                p = settings().ui.default_query_system_prompt
            # For chat mode, obtain default system prompt from settings
            case "LLM Chat":
                p = settings().ui.default_chat_system_prompt
            # For any other mode, clear the system prompt
            case _:
                p = ""
        return p

    def _set_system_prompt(self, system_prompt_input: str) -> None:
        logger.info(f"Setting system prompt to: {system_prompt_input}")
        self._system_prompt = system_prompt_input

    def _set_current_mode(self, mode: str) -> Any:
        self.mode = mode
        self._set_system_prompt(self._get_default_system_prompt(mode))
        # Update placeholder and allow interaction if default system prompt is set
        if self._system_prompt:
            return gr.update(placeholder=self._system_prompt, interactive=True)
        # Update placeholder and disable interaction if no default system prompt is set
        else:
            return gr.update(placeholder=self._system_prompt, interactive=False)

    def _list_ingested_files(self) -> list[list[str]]:
        files = set()