open-webui/backend/apps/webui/routers/configs.py

90 lines
2.1 KiB
Python
Raw Normal View History

2024-01-03 00:48:10 +00:00
from fastapi import Response, Request
from fastapi import Depends, FastAPI, HTTPException, status
from datetime import datetime, timedelta
2024-08-14 12:46:31 +00:00
from typing import Union
2024-01-03 00:48:10 +00:00
from fastapi import APIRouter
from pydantic import BaseModel
import time
import uuid
from config import BannerModel
2024-05-26 08:15:48 +00:00
from apps.webui.models.users import Users
2024-01-03 00:48:10 +00:00
2024-03-31 08:13:39 +00:00
from utils.utils import (
get_password_hash,
2024-06-27 18:29:59 +00:00
get_verified_user,
2024-03-31 08:13:39 +00:00
get_admin_user,
create_token,
)
2024-01-03 00:48:10 +00:00
from utils.misc import get_gravatar_url, validate_email_format
from constants import ERROR_MESSAGES
router = APIRouter()
class SetDefaultModelsForm(BaseModel):
models: str
2024-01-23 05:07:40 +00:00
class PromptSuggestion(BaseModel):
2024-08-14 12:46:31 +00:00
title: list[str]
2024-01-23 05:07:40 +00:00
content: str
class SetDefaultSuggestionsForm(BaseModel):
2024-08-14 12:46:31 +00:00
suggestions: list[PromptSuggestion]
2024-01-23 05:07:40 +00:00
2024-01-03 00:48:10 +00:00
############################
# SetDefaultModels
############################
@router.post("/default/models", response_model=str)
2024-01-23 05:07:40 +00:00
async def set_global_default_models(
request: Request, form_data: SetDefaultModelsForm, user=Depends(get_admin_user)
2024-01-23 05:07:40 +00:00
):
request.app.state.config.DEFAULT_MODELS = form_data.models
return request.app.state.config.DEFAULT_MODELS
2024-01-23 05:07:40 +00:00
2024-08-14 12:46:31 +00:00
@router.post("/default/suggestions", response_model=list[PromptSuggestion])
2024-01-23 05:07:40 +00:00
async def set_global_default_suggestions(
request: Request,
form_data: SetDefaultSuggestionsForm,
user=Depends(get_admin_user),
2024-01-23 05:07:40 +00:00
):
data = form_data.model_dump()
request.app.state.config.DEFAULT_PROMPT_SUGGESTIONS = data["suggestions"]
return request.app.state.config.DEFAULT_PROMPT_SUGGESTIONS
############################
# SetBanners
############################
class SetBannersForm(BaseModel):
2024-08-14 12:46:31 +00:00
banners: list[BannerModel]
2024-08-14 12:46:31 +00:00
@router.post("/banners", response_model=list[BannerModel])
async def set_banners(
request: Request,
form_data: SetBannersForm,
user=Depends(get_admin_user),
):
data = form_data.model_dump()
request.app.state.config.BANNERS = data["banners"]
return request.app.state.config.BANNERS
2024-08-14 12:46:31 +00:00
@router.get("/banners", response_model=list[BannerModel])
async def get_banners(
request: Request,
2024-06-27 18:29:59 +00:00
user=Depends(get_verified_user),
):
return request.app.state.config.BANNERS