2024-08-27 22:10:27 +00:00
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
|
|
from pydantic import BaseModel
|
2024-01-03 00:48:10 +00:00
|
|
|
|
2024-11-26 16:55:06 +00:00
|
|
|
from typing import Optional
|
2024-09-03 19:16:07 +00:00
|
|
|
|
2024-12-09 00:01:56 +00:00
|
|
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.config import get_config, save_config
|
2024-11-26 16:55:06 +00:00
|
|
|
from open_webui.config import BannerModel
|
|
|
|
|
2024-09-03 19:16:07 +00:00
|
|
|
|
2024-01-03 00:48:10 +00:00
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2024-09-03 19:16:07 +00:00
|
|
|
############################
|
|
|
|
# ImportConfig
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
class ImportConfigForm(BaseModel):
|
|
|
|
config: dict
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/import", response_model=dict)
|
|
|
|
async def import_config(form_data: ImportConfigForm, user=Depends(get_admin_user)):
|
|
|
|
save_config(form_data.config)
|
|
|
|
return get_config()
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# ExportConfig
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/export", response_model=dict)
|
|
|
|
async def export_config(user=Depends(get_admin_user)):
|
|
|
|
return get_config()
|
|
|
|
|
|
|
|
|
2024-11-26 08:55:58 +00:00
|
|
|
############################
|
|
|
|
# SetDefaultModels
|
|
|
|
############################
|
|
|
|
class ModelsConfigForm(BaseModel):
|
2024-11-26 16:55:06 +00:00
|
|
|
DEFAULT_MODELS: Optional[str]
|
|
|
|
MODEL_ORDER_LIST: Optional[list[str]]
|
2024-11-26 08:55:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/models", response_model=ModelsConfigForm)
|
|
|
|
async def get_models_config(request: Request, user=Depends(get_admin_user)):
|
|
|
|
return {
|
|
|
|
"DEFAULT_MODELS": request.app.state.config.DEFAULT_MODELS,
|
|
|
|
"MODEL_ORDER_LIST": request.app.state.config.MODEL_ORDER_LIST,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/models", response_model=ModelsConfigForm)
|
|
|
|
async def set_models_config(
|
|
|
|
request: Request, form_data: ModelsConfigForm, user=Depends(get_admin_user)
|
|
|
|
):
|
|
|
|
request.app.state.config.DEFAULT_MODELS = form_data.DEFAULT_MODELS
|
|
|
|
request.app.state.config.MODEL_ORDER_LIST = form_data.MODEL_ORDER_LIST
|
|
|
|
return {
|
|
|
|
"DEFAULT_MODELS": request.app.state.config.DEFAULT_MODELS,
|
|
|
|
"MODEL_ORDER_LIST": request.app.state.config.MODEL_ORDER_LIST,
|
|
|
|
}
|
2024-01-03 00:48:10 +00:00
|
|
|
|
|
|
|
|
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-11-26 08:55:58 +00:00
|
|
|
@router.post("/suggestions", response_model=list[PromptSuggestion])
|
|
|
|
async def set_default_suggestions(
|
2024-01-23 05:07:40 +00:00
|
|
|
request: Request,
|
|
|
|
form_data: SetDefaultSuggestionsForm,
|
2024-02-09 00:05:01 +00:00
|
|
|
user=Depends(get_admin_user),
|
2024-01-23 05:07:40 +00:00
|
|
|
):
|
2024-02-09 00:05:01 +00:00
|
|
|
data = form_data.model_dump()
|
2024-05-10 07:03:24 +00:00
|
|
|
request.app.state.config.DEFAULT_PROMPT_SUGGESTIONS = data["suggestions"]
|
|
|
|
return request.app.state.config.DEFAULT_PROMPT_SUGGESTIONS
|
2024-05-26 19:18:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# SetBanners
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
class SetBannersForm(BaseModel):
|
2024-08-14 12:46:31 +00:00
|
|
|
banners: list[BannerModel]
|
2024-05-26 19:18:43 +00:00
|
|
|
|
|
|
|
|
2024-08-14 12:46:31 +00:00
|
|
|
@router.post("/banners", response_model=list[BannerModel])
|
2024-05-26 19:18:43 +00:00
|
|
|
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])
|
2024-05-26 19:18:43 +00:00
|
|
|
async def get_banners(
|
|
|
|
request: Request,
|
2024-06-27 18:29:59 +00:00
|
|
|
user=Depends(get_verified_user),
|
2024-05-26 19:18:43 +00:00
|
|
|
):
|
|
|
|
return request.app.state.config.BANNERS
|