From 47b371333e7368d1c3ca9c2c9586c61c7ebd3516 Mon Sep 17 00:00:00 2001 From: Juan Date: Sat, 9 Dec 2023 12:12:32 -0300 Subject: [PATCH 1/3] feat: Context Template from settings for QueryDocs Allowing the text values "context_template" --- private_gpt/server/chat/chat_service.py | 9 +++++++++ private_gpt/settings/settings.py | 11 +++++++++++ settings.yaml | 5 ++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/private_gpt/server/chat/chat_service.py b/private_gpt/server/chat/chat_service.py index ffdb3f90f..77afe114d 100644 --- a/private_gpt/server/chat/chat_service.py +++ b/private_gpt/server/chat/chat_service.py @@ -19,6 +19,9 @@ ) from private_gpt.open_ai.extensions.context_filter import ContextFilter from private_gpt.server.chunks.chunks_service import Chunk +from private_gpt.settings.settings import settings + +DEFAULT_CONTEXT_TEMPLATE = settings().rag.default_context_template class Completion(BaseModel): @@ -97,6 +100,7 @@ def _chat_engine( system_prompt: str | None = None, use_context: bool = False, context_filter: ContextFilter | None = None, + context_template: str | None = DEFAULT_CONTEXT_TEMPLATE, ) -> BaseChatEngine: if use_context: vector_index_retriever = self.vector_store_component.get_retriever( @@ -109,6 +113,7 @@ def _chat_engine( node_postprocessors=[ MetadataReplacementPostProcessor(target_metadata_key="window"), ], + context_template=context_template, ) else: return SimpleChatEngine.from_defaults( @@ -121,6 +126,7 @@ def stream_chat( messages: list[ChatMessage], use_context: bool = False, context_filter: ContextFilter | None = None, + context_template: str | None = None, ) -> CompletionGen: chat_engine_input = ChatEngineInput.from_messages(messages) last_message = ( @@ -141,6 +147,7 @@ def stream_chat( system_prompt=system_prompt, use_context=use_context, context_filter=context_filter, + context_template=context_template, ) streaming_response = chat_engine.stream_chat( message=last_message if last_message is not None else "", @@ -157,6 +164,7 @@ def chat( messages: list[ChatMessage], use_context: bool = False, context_filter: ContextFilter | None = None, + context_template: str | None = None, ) -> Completion: chat_engine_input = ChatEngineInput.from_messages(messages) last_message = ( @@ -177,6 +185,7 @@ def chat( system_prompt=system_prompt, use_context=use_context, context_filter=context_filter, + context_template=context_template, ) wrapped_response = chat_engine.chat( message=last_message if last_message is not None else "", diff --git a/private_gpt/settings/settings.py b/private_gpt/settings/settings.py index 8b03f6111..ccf6b177f 100644 --- a/private_gpt/settings/settings.py +++ b/private_gpt/settings/settings.py @@ -217,6 +217,16 @@ class QdrantSettings(BaseModel): ) +class RagSettings(BaseModel): + default_context_template: str | None = Field( + None, + description=( + "The default context template to use for the chat engine when using RAG. " + "If none is given - use the default system prompt (from the llama_index). " + ), + ) + + class Settings(BaseModel): server: ServerSettings data: DataSettings @@ -228,6 +238,7 @@ class Settings(BaseModel): openai: OpenAISettings vectorstore: VectorstoreSettings qdrant: QdrantSettings | None = None + rag: RagSettings """ diff --git a/settings.yaml b/settings.yaml index af51a7f7e..066c00649 100644 --- a/settings.yaml +++ b/settings.yaml @@ -56,4 +56,7 @@ sagemaker: openai: api_key: ${OPENAI_API_KEY:} - model: gpt-3.5-turbo \ No newline at end of file + model: gpt-3.5-turbo + +rag: + default_context_template: "Context information is below.\n--------------------\n{context_str}\n--------------------\n" From cfbba710795c230a61d1726efd38204b7ce0b170 Mon Sep 17 00:00:00 2001 From: juan-m12i Date: Tue, 19 Dec 2023 15:22:54 +0000 Subject: [PATCH 2/3] feat: Context Template from settings for QueryDocs Allowing the text values context_template - PR comments --- private_gpt/server/chat/chat_service.py | 6 +++--- settings.yaml | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/private_gpt/server/chat/chat_service.py b/private_gpt/server/chat/chat_service.py index 77afe114d..da6f494ed 100644 --- a/private_gpt/server/chat/chat_service.py +++ b/private_gpt/server/chat/chat_service.py @@ -21,8 +21,6 @@ from private_gpt.server.chunks.chunks_service import Chunk from private_gpt.settings.settings import settings -DEFAULT_CONTEXT_TEMPLATE = settings().rag.default_context_template - class Completion(BaseModel): response: str @@ -100,9 +98,11 @@ def _chat_engine( system_prompt: str | None = None, use_context: bool = False, context_filter: ContextFilter | None = None, - context_template: str | None = DEFAULT_CONTEXT_TEMPLATE, + context_template: str | None = None, ) -> BaseChatEngine: if use_context: + if context_template is None: + context_template = settings().rag.default_context_template vector_index_retriever = self.vector_store_component.get_retriever( index=self.index, context_filter=context_filter ) diff --git a/settings.yaml b/settings.yaml index 52ce505d9..07a4b302c 100644 --- a/settings.yaml +++ b/settings.yaml @@ -61,4 +61,8 @@ openai: model: gpt-3.5-turbo rag: - default_context_template: "Context information is below.\n--------------------\n{context_str}\n--------------------\n" + default_context_template: | + Context information is below. + -------------------- + {context_str} + -------------------- From f06a8b4ba1c8af0da8b230656a9d1978d96cec2d Mon Sep 17 00:00:00 2001 From: Juan Date: Wed, 20 Dec 2023 11:51:08 -0300 Subject: [PATCH 3/3] new round of PR reviews --- private_gpt/server/chat/chat_service.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/private_gpt/server/chat/chat_service.py b/private_gpt/server/chat/chat_service.py index da6f494ed..cd1b5041a 100644 --- a/private_gpt/server/chat/chat_service.py +++ b/private_gpt/server/chat/chat_service.py @@ -19,7 +19,7 @@ ) from private_gpt.open_ai.extensions.context_filter import ContextFilter from private_gpt.server.chunks.chunks_service import Chunk -from private_gpt.settings.settings import settings +from private_gpt.settings.settings import Settings class Completion(BaseModel): @@ -75,6 +75,7 @@ def __init__( vector_store_component: VectorStoreComponent, embedding_component: EmbeddingComponent, node_store_component: NodeStoreComponent, + settings: Settings, ) -> None: self.llm_service = llm_component self.vector_store_component = vector_store_component @@ -92,17 +93,19 @@ def __init__( service_context=self.service_context, show_progress=True, ) + self.default_context_template = settings.rag.default_context_template def _chat_engine( self, system_prompt: str | None = None, use_context: bool = False, context_filter: ContextFilter | None = None, - context_template: str | None = None, ) -> BaseChatEngine: if use_context: - if context_template is None: - context_template = settings().rag.default_context_template + if self.default_context_template is not None: + context_template = self.default_context_template + else: + context_template = None vector_index_retriever = self.vector_store_component.get_retriever( index=self.index, context_filter=context_filter ) @@ -126,7 +129,6 @@ def stream_chat( messages: list[ChatMessage], use_context: bool = False, context_filter: ContextFilter | None = None, - context_template: str | None = None, ) -> CompletionGen: chat_engine_input = ChatEngineInput.from_messages(messages) last_message = ( @@ -147,7 +149,6 @@ def stream_chat( system_prompt=system_prompt, use_context=use_context, context_filter=context_filter, - context_template=context_template, ) streaming_response = chat_engine.stream_chat( message=last_message if last_message is not None else "", @@ -164,7 +165,6 @@ def chat( messages: list[ChatMessage], use_context: bool = False, context_filter: ContextFilter | None = None, - context_template: str | None = None, ) -> Completion: chat_engine_input = ChatEngineInput.from_messages(messages) last_message = ( @@ -185,7 +185,6 @@ def chat( system_prompt=system_prompt, use_context=use_context, context_filter=context_filter, - context_template=context_template, ) wrapped_response = chat_engine.chat( message=last_message if last_message is not None else "",