From a94d596b9c878d8eef5c82d24d8546fcb4223211 Mon Sep 17 00:00:00 2001 From: NeonKirill Date: Wed, 31 Jan 2024 00:49:33 +0100 Subject: [PATCH] Addressed comments --- chat_server/blueprints/chat.py | 8 +++-- .../mongo_utils/queries/constants.py | 28 ++++++++++++++++ .../mongo_utils/queries/dao/abc.py | 18 ++++++++-- .../mongo_utils/queries/dao/shouts.py | 3 +- .../mongo_utils/queries/dao/users.py | 5 ++- .../mongo_utils/queries/mongo_queries.py | 33 +++++++++++++++++-- 6 files changed, 84 insertions(+), 11 deletions(-) diff --git a/chat_server/blueprints/chat.py b/chat_server/blueprints/chat.py index 2dffc683..ba854ee5 100644 --- a/chat_server/blueprints/chat.py +++ b/chat_server/blueprints/chat.py @@ -25,6 +25,7 @@ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import warnings from typing import Optional from time import time @@ -67,9 +68,12 @@ async def new_conversation( :returns JSON response with new conversation data if added, 401 error message otherwise """ - + if conversation_id: + warnings.warn( + "Param conversation id is no longer considered", DeprecationWarning + ) conversation_data = MongoDocumentsAPI.CHATS.get_conversation_data( - search_str=[conversation_id, conversation_name], + search_str=[conversation_name], ) if conversation_data: return respond(f'Conversation "{conversation_name}" already exists', 400) diff --git a/utils/database_utils/mongo_utils/queries/constants.py b/utils/database_utils/mongo_utils/queries/constants.py index 0a41108c..95ae07ba 100644 --- a/utils/database_utils/mongo_utils/queries/constants.py +++ b/utils/database_utils/mongo_utils/queries/constants.py @@ -1,3 +1,31 @@ +# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework +# All trademark and other rights reserved by their respective owners +# Copyright 2008-2022 Neongecko.com Inc. +# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds, +# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo +# BSD-3 License +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + from enum import Enum diff --git a/utils/database_utils/mongo_utils/queries/dao/abc.py b/utils/database_utils/mongo_utils/queries/dao/abc.py index c432ec6e..c5abba3c 100644 --- a/utils/database_utils/mongo_utils/queries/dao/abc.py +++ b/utils/database_utils/mongo_utils/queries/dao/abc.py @@ -60,7 +60,14 @@ def list_contains( aggregate_result: bool = True, *args, **kwargs - ) -> dict: + ) -> dict[str, list] | list[str]: + """ + Lists items that are members of :param source_set under the :param key + :param key: attribute to query + :param source_set: collection of values to lookup + :param aggregate_result: to apply aggregation by key on result (defaults to True) + :return matching items + """ items = {} contains_filter = self._build_contains_filter(key=key, lookup_set=source_set) if contains_filter: @@ -105,7 +112,11 @@ def list_items( ] return items - def aggregate_items_by_key(self, key, items: list) -> dict: + def aggregate_items_by_key(self, key: str, items: list[dict]) -> dict: + """ + Aggregates list of dictionaries according to the provided key + :return dictionary mapping id -> list of matching items + """ aggregated_data = {} # TODO: consider Mongo DB aggregation API for item in items: @@ -134,7 +145,8 @@ def _build_contains_filter(self, key, lookup_set) -> MongoFilter | None: ) return mongo_filter - def add_item(self, data: dict): + def add_item(self, data: dict) -> bool: + """Inserts provided data into the object's document""" return self._execute_query(command=MongoCommands.INSERT_ONE, data=data) def get_item( diff --git a/utils/database_utils/mongo_utils/queries/dao/shouts.py b/utils/database_utils/mongo_utils/queries/dao/shouts.py index 490c674a..66ac246c 100644 --- a/utils/database_utils/mongo_utils/queries/dao/shouts.py +++ b/utils/database_utils/mongo_utils/queries/dao/shouts.py @@ -69,10 +69,11 @@ def fetch_messages_from_prompt(self, prompt: dict): message_ids.extend(list(prompt_data.get(column, {}).values())) return self.list_contains(source_set=message_ids) - def fetch_audio_data(self, message_id: str): + def fetch_audio_data(self, message_id: str) -> str | None: """ Fetches audio data from message :param message_id: message id to fetch + :returns base64 encoded audio data if any """ shout_data = self.get_item(item_id=message_id) if not shout_data: diff --git a/utils/database_utils/mongo_utils/queries/dao/users.py b/utils/database_utils/mongo_utils/queries/dao/users.py index caf08790..ef4188ad 100644 --- a/utils/database_utils/mongo_utils/queries/dao/users.py +++ b/utils/database_utils/mongo_utils/queries/dao/users.py @@ -71,7 +71,7 @@ def get_user(self, user_id=None, nickname=None) -> Union[dict, None]: ) return user - def fetch_users_from_prompt(self, prompt: dict): + def fetch_users_from_prompt(self, prompt: dict) -> dict[str, list]: """Fetches user ids detected in provided prompt""" prompt_data = prompt["data"] user_ids = prompt_data.get("participating_subminds", []) @@ -174,7 +174,7 @@ def _create_bot(self, nickname: str, context: dict) -> dict: def set_preferences(self, user_id, preferences_mapping: dict): """Sets user preferences for specified user according to preferences mapping""" - if user_id: + if user_id and preferences_mapping: try: update_mapping = { f"preferences.{key}": val @@ -187,7 +187,6 @@ def set_preferences(self, user_id, preferences_mapping: dict): ) except Exception as ex: LOG.error(f"Failed to update preferences for user_id={user_id} - {ex}") - return preferences_mapping def create_guest(self, nano_token: str = None) -> dict: """ diff --git a/utils/database_utils/mongo_utils/queries/mongo_queries.py b/utils/database_utils/mongo_utils/queries/mongo_queries.py index 17504928..6bb4bc29 100644 --- a/utils/database_utils/mongo_utils/queries/mongo_queries.py +++ b/utils/database_utils/mongo_utils/queries/mongo_queries.py @@ -1,6 +1,34 @@ +# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework +# All trademark and other rights reserved by their respective owners +# Copyright 2008-2022 Neongecko.com Inc. +# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds, +# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo +# BSD-3 License +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + from typing import List, Tuple -from utils.common import buffer_to_base64 from .constants import UserPatterns, ConversationSkins from .wrapper import MongoDocumentsAPI from utils.logging_utils import LOG @@ -60,7 +88,7 @@ def fetch_message_data( limit: int = 100, fetch_senders: bool = True, start_message_id: str = None, -): +) -> list[dict]: """Fetches message data based on provided conversation skin""" message_data = fetch_shout_data( conversation_data=conversation_data, @@ -213,6 +241,7 @@ def fetch_prompt_data( def add_shout(data: dict): + """Records shout data and pushes its id to the relevant conversation flow""" MongoDocumentsAPI.SHOUTS.add_item(data=data) if cid := data.get("cid"): shout_id = data["_id"]