Skip to content

Commit

Permalink
✨ Feat(settings): settings service and consistent naming
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterKenth committed Nov 21, 2024
1 parent 86cf4a6 commit eff7831
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 187 deletions.
11 changes: 0 additions & 11 deletions fai-rag-app/fai-backend/fai_backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,3 @@ class Config:


settings = TestSettings()


def get_settings_dict(overrides: dict) -> dict:
merged = settings.model_dump() | overrides

for key in merged.keys():
if isinstance(merged[key], SecretStr):
secret: SecretStr = merged[key]
merged[key] = secret.get_secret_value()

return merged
5 changes: 0 additions & 5 deletions fai-rag-app/fai-backend/fai_backend/config_v2/models.py

This file was deleted.

166 changes: 0 additions & 166 deletions fai-rag-app/fai-backend/fai_backend/config_v2/routes.py

This file was deleted.

2 changes: 1 addition & 1 deletion fai-rag-app/fai-backend/fai_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from fai_backend.vector.routes import router as vector_router
from fai_backend.new_chat.routes import router as new_chat_router
from fai_backend.feedback.routes import router as feedback_router
from fai_backend.config_v2.routes import router as config_router
from fai_backend.settings.routes import router as config_router


@asynccontextmanager
Expand Down
4 changes: 2 additions & 2 deletions fai-rag-app/fai-backend/fai_backend/projects/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from pydantic import BaseModel, EmailStr, Field, SecretStr, field_serializer

from fai_backend.assistant.models import AssistantTemplate
from fai_backend.config_v2.models import Config
from fai_backend.schema import Timestamp
from fai_backend.settings.models import SettingsDict


class ProjectMember(BaseModel):
Expand All @@ -21,7 +21,7 @@ class Project(BaseModel):
creator: EmailStr
description: str = ''
timestamp: Timestamp = Timestamp()
config: Config = Config(config={})
settings: SettingsDict = {}
assistants: list[AssistantTemplate] = Field(default_factory=list)
members: list[ProjectMember] = Field(..., default_factory=list)
roles: dict[str, ProjectRole] = Field(..., default_factory=dict)
Expand Down
9 changes: 9 additions & 0 deletions fai-rag-app/fai-backend/fai_backend/settings/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Dict

from pydantic import BaseModel

SettingsDict = Dict[str, bool | float | int | str] # Note: order of types is important!


class Config(BaseModel):
config: SettingsDict
155 changes: 155 additions & 0 deletions fai-rag-app/fai-backend/fai_backend/settings/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
from fastapi import APIRouter, Security, Depends
from pydantic import BaseModel, SecretStr

from fai_backend.auth.security import check_permissions
from fai_backend.dependencies import get_authenticated_user, get_project_user_permissions, \
get_page_template_for_logged_in_users
from fai_backend.framework import components as c
from fai_backend.logger.route_class import APIRouter as LoggingAPIRouter
from fai_backend.phrase import phrase as _
from fai_backend.settings.models import SettingsDict
from fai_backend.settings.service import SettingsServiceFactory

router = APIRouter(
prefix='/api',
tags=['Settings'],
route_class=LoggingAPIRouter,
dependencies=[]
)


class GetSettingsResponseModel(BaseModel):
settings: SettingsDict


@router.get(
'/settings',
response_model=GetSettingsResponseModel,
dependencies=[Security(get_authenticated_user)]
)
async def get_settings(
permissions=Depends(get_project_user_permissions)
):
check_permissions(['can_edit_settings'], permissions)
service = SettingsServiceFactory().get_service()
settings = (await service.get_all()).model_dump()
for key in settings.keys():
if isinstance(settings[key], SecretStr):
secret: SecretStr = settings[key]
settings[key] = secret.get_secret_value()
return GetSettingsResponseModel(settings=settings)


class SetSettingsRequestModel(BaseModel):
settings: SettingsDict


@router.post(
'/settings',
dependencies=[Security(get_authenticated_user)]
)
async def set_settings(
body: SetSettingsRequestModel,
permissions=Depends(get_project_user_permissions)
):
check_permissions(['can_edit_settings'], permissions)
service = SettingsServiceFactory.get_service()
await service.set_all(body.settings)


@router.post(
'/settings/form',
response_model=list,
response_model_exclude_none=True
)
async def set_settings_from_form(
_=Depends(set_settings),
view=Depends(get_page_template_for_logged_in_users)
):
return view(
[c.Div(components=[
c.Div(components=[
c.Text(text='Settings updated!'),
], class_name='card-body'),
], class_name='card')],
'Edit settings',
)


@router.get(
'/view/settings',
dependencies=[Security(get_authenticated_user)],
response_model=list,
response_model_exclude_none=True
)
async def edit_settings(
permissions=Depends(get_project_user_permissions),
view=Depends(get_page_template_for_logged_in_users),
):
check_permissions(['can_edit_settings'], permissions)
settings = await SettingsServiceFactory.get_service().get_all()

return view(
[c.Div(components=[
c.Div(components=[
c.Form(
submit_url='/api/settings/form',
method='POST',
submit_text=_('edit_settings_submit_button', 'Submit'),
components=[
c.Heading(text='Basic', class_name='font-bold'),
c.InputField(
name='settings.FIXED_PIN',
label=_('Fixed pin (leave blank to disable)'),
placeholder='',
required=False,
html_type='text',
size='sm',
value=str(settings.FIXED_PIN),
),

c.Heading(text='AI Settings', class_name='font-bold'),
c.InputField(
name='settings.OPENAI_API_KEY',
label=_('OpenAI API Key'),
placeholder='',
required=False,
html_type='text',
size='sm',
value=settings.OPENAI_API_KEY.get_secret_value(),
),
c.Textarea(
name='settings.VLLM_CONFIG',
label=_('VLLM Config'),
placeholder='',
required=False,
size='sm',
value=settings.VLLM_CONFIG,
),

c.Heading(text='E-mail (Brevo)', class_name='font-bold'),
c.InputField(
name='settings.BREVO_API_URL',
label=_('Brevo API URL'),
placeholder='',
required=False,
html_type='text',
size='sm',
value=settings.BREVO_API_URL,
),
c.InputField(
name='settings.BREVO_API_KEY',
label=_('Brevo API Key'),
placeholder=_(''),
required=False,
html_type='text',
size='sm',
value=settings.BREVO_API_KEY.get_secret_value(),
)
]
)
], class_name='card-body')
], class_name='card')
],
_('Edit settings')
)
Loading

0 comments on commit eff7831

Please sign in to comment.