2024-06-11 00:12:48 +00:00
|
|
|
import black
|
2024-08-27 22:10:27 +00:00
|
|
|
import markdown
|
2024-10-12 16:12:31 +00:00
|
|
|
|
2024-12-10 08:54:13 +00:00
|
|
|
from open_webui.models.chats import ChatTitleMessagesForm
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.config import DATA_DIR, ENABLE_ADMIN_EXPORT
|
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
2025-02-18 00:25:50 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, Response, status
|
2024-08-27 22:10:27 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
from starlette.responses import FileResponse
|
2025-02-18 00:25:50 +00:00
|
|
|
|
|
|
|
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.utils.misc import get_gravatar_url
|
2024-10-12 16:12:31 +00:00
|
|
|
from open_webui.utils.pdf_generator import PDFGenerator
|
2025-02-18 00:25:50 +00:00
|
|
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
|
|
|
from open_webui.utils.code_interpreter import execute_code_jupyter
|
|
|
|
|
2023-12-23 23:38:52 +00:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2024-01-27 05:38:33 +00:00
|
|
|
@router.get("/gravatar")
|
2025-02-18 00:25:50 +00:00
|
|
|
async def get_gravatar(email: str, user=Depends(get_verified_user)):
|
2024-01-27 05:38:33 +00:00
|
|
|
return get_gravatar_url(email)
|
2024-03-02 08:33:20 +00:00
|
|
|
|
|
|
|
|
2025-02-18 00:25:50 +00:00
|
|
|
class CodeForm(BaseModel):
|
2024-06-11 00:12:48 +00:00
|
|
|
code: str
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/code/format")
|
2025-02-18 00:25:50 +00:00
|
|
|
async def format_code(form_data: CodeForm, user=Depends(get_verified_user)):
|
2024-06-11 00:12:48 +00:00
|
|
|
try:
|
2025-02-18 00:25:50 +00:00
|
|
|
formatted_code = black.format_str(form_data.code, mode=black.Mode())
|
2024-06-11 00:12:48 +00:00
|
|
|
return {"code": formatted_code}
|
|
|
|
except black.NothingChanged:
|
2025-02-18 00:25:50 +00:00
|
|
|
return {"code": form_data.code}
|
2024-06-11 00:12:48 +00:00
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
|
2025-02-18 00:25:50 +00:00
|
|
|
@router.post("/code/execute")
|
|
|
|
async def execute_code(
|
|
|
|
request: Request, form_data: CodeForm, user=Depends(get_verified_user)
|
|
|
|
):
|
|
|
|
if request.app.state.config.CODE_EXECUTION_ENGINE == "jupyter":
|
|
|
|
output = await execute_code_jupyter(
|
|
|
|
request.app.state.config.CODE_EXECUTION_JUPYTER_URL,
|
|
|
|
form_data.code,
|
|
|
|
(
|
|
|
|
request.app.state.config.CODE_EXECUTION_JUPYTER_AUTH_TOKEN
|
|
|
|
if request.app.state.config.CODE_EXECUTION_JUPYTER_AUTH == "token"
|
|
|
|
else None
|
|
|
|
),
|
|
|
|
(
|
|
|
|
request.app.state.config.CODE_EXECUTION_JUPYTER_AUTH_PASSWORD
|
|
|
|
if request.app.state.config.CODE_EXECUTION_JUPYTER_AUTH == "password"
|
|
|
|
else None
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return output
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=400,
|
|
|
|
detail="Code execution engine not supported",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-04 03:35:32 +00:00
|
|
|
class MarkdownForm(BaseModel):
|
|
|
|
md: str
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/markdown")
|
|
|
|
async def get_html_from_markdown(
|
2025-02-18 00:25:50 +00:00
|
|
|
form_data: MarkdownForm, user=Depends(get_verified_user)
|
2024-04-04 03:35:32 +00:00
|
|
|
):
|
|
|
|
return {"html": markdown.markdown(form_data.md)}
|
|
|
|
|
|
|
|
|
2024-04-06 08:54:59 +00:00
|
|
|
class ChatForm(BaseModel):
|
|
|
|
title: str
|
2024-08-14 12:46:31 +00:00
|
|
|
messages: list[dict]
|
2024-04-06 08:54:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.post("/pdf")
|
|
|
|
async def download_chat_as_pdf(
|
2025-02-18 00:25:50 +00:00
|
|
|
form_data: ChatTitleMessagesForm, user=Depends(get_verified_user)
|
2024-04-06 08:54:59 +00:00
|
|
|
):
|
2024-10-13 07:05:28 +00:00
|
|
|
try:
|
|
|
|
pdf_bytes = PDFGenerator(form_data).generate_chat_pdf()
|
|
|
|
|
|
|
|
return Response(
|
|
|
|
content=pdf_bytes,
|
|
|
|
media_type="application/pdf",
|
|
|
|
headers={"Content-Disposition": "attachment;filename=chat.pdf"},
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
2024-04-06 08:54:59 +00:00
|
|
|
|
|
|
|
|
2024-03-02 08:33:20 +00:00
|
|
|
@router.get("/db/download")
|
|
|
|
async def download_db(user=Depends(get_admin_user)):
|
2024-04-22 18:55:46 +00:00
|
|
|
if not ENABLE_ADMIN_EXPORT:
|
2024-04-17 08:33:22 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
|
)
|
2024-12-10 08:54:13 +00:00
|
|
|
from open_webui.internal.db import engine
|
2024-06-18 13:03:31 +00:00
|
|
|
|
|
|
|
if engine.name != "sqlite":
|
2024-04-27 14:53:31 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail=ERROR_MESSAGES.DB_NOT_SQLITE,
|
|
|
|
)
|
2024-03-02 08:33:20 +00:00
|
|
|
return FileResponse(
|
2024-06-18 13:03:31 +00:00
|
|
|
engine.url.database,
|
2024-03-02 08:33:20 +00:00
|
|
|
media_type="application/octet-stream",
|
|
|
|
filename="webui.db",
|
|
|
|
)
|
2024-06-01 23:31:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/litellm/config")
|
|
|
|
async def download_litellm_config_yaml(user=Depends(get_admin_user)):
|
|
|
|
return FileResponse(
|
|
|
|
f"{DATA_DIR}/litellm/config.yaml",
|
|
|
|
media_type="application/octet-stream",
|
|
|
|
filename="config.yaml",
|
|
|
|
)
|