Skip to content

Commit

Permalink
Moved configs to the separate blueprint & added update config endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kirgrim committed Mar 11, 2024
1 parent 950b41d commit 33bed7e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 9 deletions.
1 change: 1 addition & 0 deletions chat_server/blueprints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
files_api as files_api_blueprint,
preferences as preferences_blueprint,
personas as personas_blueprint,
configs as configs_blueprint,
)
68 changes: 68 additions & 0 deletions chat_server/blueprints/configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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 fastapi import APIRouter, Depends
from starlette.responses import JSONResponse

from chat_server.server_utils.dependencies import CurrentUserDependency
from chat_server.server_utils.exceptions import (
ItemNotFoundException,
UserUnauthorizedException,
)
from chat_server.server_utils.http_utils import KlatAPIResponse
from chat_server.server_utils.models.configs import SetConfigModel, ConfigModel
from utils.database_utils.mongo_utils.queries.wrapper import MongoDocumentsAPI

router = APIRouter(
prefix="/configs",
responses={"404": {"description": "Unknown endpoint"}},
)


@router.get("/{config_property}")
async def get_config_data(model: ConfigModel = Depends()) -> JSONResponse:
"""Retrieves configured data by name"""
items = MongoDocumentsAPI.CONFIGS.get_by_name(
config_name=model.config_property, version=model.version
)
return JSONResponse(content=items)


@router.put("/{config_property}")
async def update_config(
current_user: CurrentUserDependency, model: SetConfigModel = Depends()
) -> JSONResponse:
"""Updates provided config by name"""
if "admin" not in current_user.roles:
raise UserUnauthorizedException
updated_data = MongoDocumentsAPI.CONFIGS.update_by_name(
config_name=model.config_property, version=model.version, data=model.data
)
if updated_data.matched_count == 0:
raise ItemNotFoundException
return KlatAPIResponse.OK
9 changes: 1 addition & 8 deletions chat_server/blueprints/personas.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def list_personas(
model.user_id == "*" and "admin" not in current_user.roles
) or not is_authorized_for_user_id(current_user, user_id=model.user_id):
raise UserUnauthorizedException
else:
elif model.user_id != "*":
filters.append(MongoFilter(key="user_id", value=model.user_id))
else:
user_filter = [{"user_id": None}, {"user_id": current_user.user_id}]
Expand Down Expand Up @@ -159,10 +159,3 @@ async def toggle_persona_state(
if updated_data.matched_count == 0:
raise ItemNotFoundException
return KlatAPIResponse.OK


@router.get("/supported_llms/list")
async def list_supported_llms():
"""Lists supported LLMs"""
items = MongoDocumentsAPI.CONFIGS.get_by_name(config_name="supported_llms") or []
return JSONResponse(content=items)
13 changes: 13 additions & 0 deletions chat_server/server_utils/models/configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fastapi import Path, Query
from pydantic import BaseModel, Field


class ConfigModel(BaseModel):
config_property: str = Field(
Path(title="Name of the config property"), examples=["supported_llms"]
)
version: str = Field(Query(default="latest"), examples=["latest"])


class SetConfigModel(ConfigModel):
data: dict = Field([{"records": [{"label": "Chat GPT", "value": "chatgpt"}]}])
11 changes: 10 additions & 1 deletion utils/database_utils/mongo_utils/queries/dao/configs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from chat_server.server_utils.exceptions import ItemNotFoundException
from utils.database_utils.mongo_utils import MongoDocuments, MongoFilter
from utils.database_utils.mongo_utils.queries.dao.abc import MongoDocumentDAO
from utils.logging_utils import LOG
Expand All @@ -8,7 +9,7 @@ class ConfigsDAO(MongoDocumentDAO):
def document(self):
return MongoDocuments.CONFIGS

def get_by_name(self, config_name: str, version: str = "default") -> dict:
def get_by_name(self, config_name: str, version: str = "latest"):
filters = [
MongoFilter(key="name", value=config_name),
MongoFilter(key="version", value=version),
Expand All @@ -18,3 +19,11 @@ def get_by_name(self, config_name: str, version: str = "default") -> dict:
return item.get("value")
else:
LOG.error(f"Failed to get config by {config_name = }, {version = }")
raise ItemNotFoundException

def update_by_name(self, config_name: str, data: dict, version: str = "latest"):
filters = [
MongoFilter(key="name", value=config_name),
MongoFilter(key="version", value=version),
]
return self.update_item(filters=filters, data={"value": data})

0 comments on commit 33bed7e

Please sign in to comment.