From c5546fd3749914ab9c0d9a1f0cac27261c560949 Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Thu, 25 Jan 2024 17:10:38 -0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20related=20(#2090)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit now updates # Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate): --- backend/llm/api_brain_qa.py | 2 +- backend/modules/brain/dto/inputs.py | 2 +- .../chat/controller/chat/brainful_chat.py | 73 ++++++++++--------- backend/modules/chat/service/chat_service.py | 2 +- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/backend/llm/api_brain_qa.py b/backend/llm/api_brain_qa.py index 727482878055..9331f009f170 100644 --- a/backend/llm/api_brain_qa.py +++ b/backend/llm/api_brain_qa.py @@ -331,7 +331,7 @@ async def generate_stream( chat_service.update_message_by_id( message_id=str(streamed_chat_history.message_id), user_message=question.question, - assistant="".join(response_tokens), + assistant="".join(str(token) for token in response_tokens), metadata=self.metadata, ) diff --git a/backend/modules/brain/dto/inputs.py b/backend/modules/brain/dto/inputs.py index 312e2a2bcc94..2cce60f6890b 100644 --- a/backend/modules/brain/dto/inputs.py +++ b/backend/modules/brain/dto/inputs.py @@ -30,7 +30,7 @@ class CreateBrainProperties(BaseModel, extra=Extra.forbid): status: Optional[str] = "private" model: Optional[str] temperature: Optional[float] = 0.0 - max_tokens: Optional[int] = 256 + max_tokens: Optional[int] = 2000 prompt_id: Optional[UUID] = None brain_type: Optional[BrainType] = BrainType.DOC brain_definition: Optional[CreateApiBrainDefinition] diff --git a/backend/modules/chat/controller/chat/brainful_chat.py b/backend/modules/chat/controller/chat/brainful_chat.py index e7506cba0b7b..a0a15a35c5c5 100644 --- a/backend/modules/chat/controller/chat/brainful_chat.py +++ b/backend/modules/chat/controller/chat/brainful_chat.py @@ -53,42 +53,47 @@ def get_answer_generator( user_id, chat_question, ): - brain_id_to_use = brain_id metadata = {} - if not brain_id: - brain_settings = BrainSettings() - supabase_client = get_supabase_client() - embeddings = None - if brain_settings.ollama_api_base_url: - embeddings = OllamaEmbeddings( - base_url=brain_settings.ollama_api_base_url - ) # pyright: ignore reportPrivateUsage=none - else: - embeddings = OpenAIEmbeddings() - vector_store = CustomSupabaseVectorStore( - supabase_client, embeddings, table_name="vectors", user_id=user_id - ) - # Get the first question from the chat_question - - question = chat_question.question - history = chat_service.get_chat_history(chat_id) - if history: - question = history[0].user_message - brain_id_to_use = history[0].brain_id - - list_brains = [] - if history: - list_brains = vector_store.find_brain_closest_query(user_id, question) - metadata["close_brains"] = list_brains - else: - list_brains = vector_store.find_brain_closest_query(user_id, question) - if list_brains: - brain_id_to_use = list_brains[0]["id"] - else: - brain_id_to_use = None - # Add to metadata close_brains and close_brains_similarity - metadata["close_brains"] = list_brains + brain_settings = BrainSettings() + supabase_client = get_supabase_client() + embeddings = None + if brain_settings.ollama_api_base_url: + embeddings = OllamaEmbeddings( + base_url=brain_settings.ollama_api_base_url + ) # pyright: ignore reportPrivateUsage=none + else: + embeddings = OpenAIEmbeddings() + vector_store = CustomSupabaseVectorStore( + supabase_client, embeddings, table_name="vectors", user_id=user_id + ) + + # Init + + brain_id_to_use = brain_id + + # Get the first question from the chat_question + + question = chat_question.question + history = chat_service.get_chat_history(chat_id) + + list_brains = [] # To return + + if history and not brain_id_to_use: + # Replace the question with the first question from the history + question = history[0].user_message + + if history and not brain_id: + brain_id_to_use = history[0].brain_id + + # Calculate the closest brains to the question + list_brains = vector_store.find_brain_closest_query(user_id, question) + + metadata["close_brains"] = list_brains + + if list_brains and not brain_id_to_use: + brain_id_to_use = list_brains[0]["id"] + # GENERIC follow_up_questions = chat_service.get_follow_up_question(chat_id) metadata["follow_up_questions"] = follow_up_questions metadata["model"] = model diff --git a/backend/modules/chat/service/chat_service.py b/backend/modules/chat/service/chat_service.py index e7d598c5548d..2cd02e70402b 100644 --- a/backend/modules/chat/service/chat_service.py +++ b/backend/modules/chat/service/chat_service.py @@ -99,7 +99,7 @@ def get_chat_history(self, chat_id: str) -> List[GetChatHistoryOutput]: assistant=message.assistant, message_time=message.message_time, brain_name=brain.name if brain else None, - brain_id=brain.id if brain else None, + brain_id=str(brain.id) if brain else None, prompt_title=prompt.title if prompt else None, metadata=message.metadata, )