Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kirgrim committed Jan 30, 2024
1 parent 7b08324 commit a94d596
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 11 deletions.
8 changes: 6 additions & 2 deletions chat_server/blueprints/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions utils/database_utils/mongo_utils/queries/constants.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
18 changes: 15 additions & 3 deletions utils/database_utils/mongo_utils/queries/dao/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion utils/database_utils/mongo_utils/queries/dao/shouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions utils/database_utils/mongo_utils/queries/dao/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
Expand Down Expand Up @@ -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
Expand All @@ -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:
"""
Expand Down
33 changes: 31 additions & 2 deletions utils/database_utils/mongo_utils/queries/mongo_queries.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit a94d596

Please sign in to comment.