main.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. from contextlib import asynccontextmanager
  2. from bs4 import BeautifulSoup
  3. import json
  4. import markdown
  5. import time
  6. import os
  7. import sys
  8. import logging
  9. import aiohttp
  10. import requests
  11. import mimetypes
  12. from fastapi import FastAPI, Request, Depends, status
  13. from fastapi.staticfiles import StaticFiles
  14. from fastapi.responses import JSONResponse
  15. from fastapi import HTTPException
  16. from fastapi.middleware.wsgi import WSGIMiddleware
  17. from fastapi.middleware.cors import CORSMiddleware
  18. from starlette.exceptions import HTTPException as StarletteHTTPException
  19. from starlette.middleware.base import BaseHTTPMiddleware
  20. from starlette.responses import StreamingResponse, Response
  21. from apps.ollama.main import app as ollama_app, get_all_models as get_ollama_models
  22. from apps.openai.main import app as openai_app, get_all_models as get_openai_models
  23. from apps.audio.main import app as audio_app
  24. from apps.images.main import app as images_app
  25. from apps.rag.main import app as rag_app
  26. from apps.webui.main import app as webui_app
  27. import asyncio
  28. from pydantic import BaseModel
  29. from typing import List, Optional
  30. from apps.webui.models.models import Models, ModelModel
  31. from utils.utils import (
  32. get_admin_user,
  33. get_verified_user,
  34. get_current_user,
  35. get_http_authorization_cred,
  36. )
  37. from apps.rag.utils import rag_messages
  38. from config import (
  39. CONFIG_DATA,
  40. WEBUI_NAME,
  41. WEBUI_URL,
  42. WEBUI_AUTH,
  43. ENV,
  44. VERSION,
  45. CHANGELOG,
  46. FRONTEND_BUILD_DIR,
  47. CACHE_DIR,
  48. STATIC_DIR,
  49. ENABLE_OPENAI_API,
  50. ENABLE_OLLAMA_API,
  51. ENABLE_MODEL_FILTER,
  52. MODEL_FILTER_LIST,
  53. GLOBAL_LOG_LEVEL,
  54. SRC_LOG_LEVELS,
  55. WEBHOOK_URL,
  56. ENABLE_ADMIN_EXPORT,
  57. RAG_WEB_SEARCH_ENABLED,
  58. AppConfig,
  59. WEBUI_BUILD_HASH,
  60. )
  61. from constants import ERROR_MESSAGES
  62. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  63. log = logging.getLogger(__name__)
  64. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  65. class SPAStaticFiles(StaticFiles):
  66. async def get_response(self, path: str, scope):
  67. try:
  68. return await super().get_response(path, scope)
  69. except (HTTPException, StarletteHTTPException) as ex:
  70. if ex.status_code == 404:
  71. return await super().get_response("index.html", scope)
  72. else:
  73. raise ex
  74. print(
  75. rf"""
  76. ___ __ __ _ _ _ ___
  77. / _ \ _ __ ___ _ __ \ \ / /__| |__ | | | |_ _|
  78. | | | | '_ \ / _ \ '_ \ \ \ /\ / / _ \ '_ \| | | || |
  79. | |_| | |_) | __/ | | | \ V V / __/ |_) | |_| || |
  80. \___/| .__/ \___|_| |_| \_/\_/ \___|_.__/ \___/|___|
  81. |_|
  82. v{VERSION} - building the best open-source AI user interface.
  83. {f"Commit: {WEBUI_BUILD_HASH}" if WEBUI_BUILD_HASH != "dev-build" else ""}
  84. https://github.com/open-webui/open-webui
  85. """
  86. )
  87. @asynccontextmanager
  88. async def lifespan(app: FastAPI):
  89. yield
  90. app = FastAPI(
  91. docs_url="/docs" if ENV == "dev" else None, redoc_url=None, lifespan=lifespan
  92. )
  93. app.state.config = AppConfig()
  94. app.state.config.ENABLE_OPENAI_API = ENABLE_OPENAI_API
  95. app.state.config.ENABLE_OLLAMA_API = ENABLE_OLLAMA_API
  96. app.state.config.ENABLE_MODEL_FILTER = ENABLE_MODEL_FILTER
  97. app.state.config.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  98. app.state.config.WEBHOOK_URL = WEBHOOK_URL
  99. app.state.MODELS = {}
  100. origins = ["*"]
  101. # Custom middleware to add security headers
  102. # class SecurityHeadersMiddleware(BaseHTTPMiddleware):
  103. # async def dispatch(self, request: Request, call_next):
  104. # response: Response = await call_next(request)
  105. # response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
  106. # response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
  107. # return response
  108. # app.add_middleware(SecurityHeadersMiddleware)
  109. class RAGMiddleware(BaseHTTPMiddleware):
  110. async def dispatch(self, request: Request, call_next):
  111. return_citations = False
  112. if request.method == "POST" and (
  113. "/api/chat" in request.url.path or "/chat/completions" in request.url.path
  114. ):
  115. log.debug(f"request.url.path: {request.url.path}")
  116. # Read the original request body
  117. body = await request.body()
  118. # Decode body to string
  119. body_str = body.decode("utf-8")
  120. # Parse string to JSON
  121. data = json.loads(body_str) if body_str else {}
  122. return_citations = data.get("citations", False)
  123. if "citations" in data:
  124. del data["citations"]
  125. # Example: Add a new key-value pair or modify existing ones
  126. # data["modified"] = True # Example modification
  127. if "docs" in data:
  128. data = {**data}
  129. data["messages"], citations = rag_messages(
  130. docs=data["docs"],
  131. messages=data["messages"],
  132. template=rag_app.state.config.RAG_TEMPLATE,
  133. embedding_function=rag_app.state.EMBEDDING_FUNCTION,
  134. k=rag_app.state.config.TOP_K,
  135. reranking_function=rag_app.state.sentence_transformer_rf,
  136. r=rag_app.state.config.RELEVANCE_THRESHOLD,
  137. hybrid_search=rag_app.state.config.ENABLE_RAG_HYBRID_SEARCH,
  138. )
  139. del data["docs"]
  140. log.debug(
  141. f"data['messages']: {data['messages']}, citations: {citations}"
  142. )
  143. modified_body_bytes = json.dumps(data).encode("utf-8")
  144. # Replace the request body with the modified one
  145. request._body = modified_body_bytes
  146. # Set custom header to ensure content-length matches new body length
  147. request.headers.__dict__["_list"] = [
  148. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  149. *[
  150. (k, v)
  151. for k, v in request.headers.raw
  152. if k.lower() != b"content-length"
  153. ],
  154. ]
  155. response = await call_next(request)
  156. if return_citations:
  157. # Inject the citations into the response
  158. if isinstance(response, StreamingResponse):
  159. # If it's a streaming response, inject it as SSE event or NDJSON line
  160. content_type = response.headers.get("Content-Type")
  161. if "text/event-stream" in content_type:
  162. return StreamingResponse(
  163. self.openai_stream_wrapper(response.body_iterator, citations),
  164. )
  165. if "application/x-ndjson" in content_type:
  166. return StreamingResponse(
  167. self.ollama_stream_wrapper(response.body_iterator, citations),
  168. )
  169. return response
  170. async def _receive(self, body: bytes):
  171. return {"type": "http.request", "body": body, "more_body": False}
  172. async def openai_stream_wrapper(self, original_generator, citations):
  173. yield f"data: {json.dumps({'citations': citations})}\n\n"
  174. async for data in original_generator:
  175. yield data
  176. async def ollama_stream_wrapper(self, original_generator, citations):
  177. yield f"{json.dumps({'citations': citations})}\n"
  178. async for data in original_generator:
  179. yield data
  180. app.add_middleware(RAGMiddleware)
  181. class PipelineMiddleware(BaseHTTPMiddleware):
  182. async def dispatch(self, request: Request, call_next):
  183. if request.method == "POST" and (
  184. "/api/chat" in request.url.path or "/chat/completions" in request.url.path
  185. ):
  186. log.debug(f"request.url.path: {request.url.path}")
  187. # Read the original request body
  188. body = await request.body()
  189. # Decode body to string
  190. body_str = body.decode("utf-8")
  191. # Parse string to JSON
  192. data = json.loads(body_str) if body_str else {}
  193. model_id = data["model"]
  194. filters = [
  195. model
  196. for model in app.state.MODELS.values()
  197. if "pipeline" in model
  198. and "type" in model["pipeline"]
  199. and model["pipeline"]["type"] == "filter"
  200. and (
  201. model["pipeline"]["pipelines"] == ["*"]
  202. or any(
  203. model_id == target_model_id
  204. for target_model_id in model["pipeline"]["pipelines"]
  205. )
  206. )
  207. ]
  208. sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"])
  209. user = None
  210. if len(sorted_filters) > 0:
  211. try:
  212. user = get_current_user(
  213. get_http_authorization_cred(
  214. request.headers.get("Authorization")
  215. )
  216. )
  217. user = {"id": user.id, "name": user.name, "role": user.role}
  218. except:
  219. pass
  220. for filter in sorted_filters:
  221. r = None
  222. try:
  223. urlIdx = filter["urlIdx"]
  224. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  225. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  226. if key != "":
  227. headers = {"Authorization": f"Bearer {key}"}
  228. r = requests.post(
  229. f"{url}/{filter['id']}/filter/inlet",
  230. headers=headers,
  231. json={
  232. "user": user,
  233. "body": data,
  234. },
  235. )
  236. r.raise_for_status()
  237. data = r.json()
  238. except Exception as e:
  239. # Handle connection error here
  240. print(f"Connection error: {e}")
  241. if r is not None:
  242. try:
  243. res = r.json()
  244. if "detail" in res:
  245. return JSONResponse(
  246. status_code=r.status_code,
  247. content=res,
  248. )
  249. except:
  250. pass
  251. else:
  252. pass
  253. modified_body_bytes = json.dumps(data).encode("utf-8")
  254. # Replace the request body with the modified one
  255. request._body = modified_body_bytes
  256. # Set custom header to ensure content-length matches new body length
  257. request.headers.__dict__["_list"] = [
  258. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  259. *[
  260. (k, v)
  261. for k, v in request.headers.raw
  262. if k.lower() != b"content-length"
  263. ],
  264. ]
  265. response = await call_next(request)
  266. return response
  267. async def _receive(self, body: bytes):
  268. return {"type": "http.request", "body": body, "more_body": False}
  269. app.add_middleware(PipelineMiddleware)
  270. app.add_middleware(
  271. CORSMiddleware,
  272. allow_origins=origins,
  273. allow_credentials=True,
  274. allow_methods=["*"],
  275. allow_headers=["*"],
  276. )
  277. @app.middleware("http")
  278. async def check_url(request: Request, call_next):
  279. if len(app.state.MODELS) == 0:
  280. await get_all_models()
  281. else:
  282. pass
  283. start_time = int(time.time())
  284. response = await call_next(request)
  285. process_time = int(time.time()) - start_time
  286. response.headers["X-Process-Time"] = str(process_time)
  287. return response
  288. @app.middleware("http")
  289. async def update_embedding_function(request: Request, call_next):
  290. response = await call_next(request)
  291. if "/embedding/update" in request.url.path:
  292. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  293. return response
  294. app.mount("/ollama", ollama_app)
  295. app.mount("/openai", openai_app)
  296. app.mount("/images/api/v1", images_app)
  297. app.mount("/audio/api/v1", audio_app)
  298. app.mount("/rag/api/v1", rag_app)
  299. app.mount("/api/v1", webui_app)
  300. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  301. async def get_all_models():
  302. openai_models = []
  303. ollama_models = []
  304. if app.state.config.ENABLE_OPENAI_API:
  305. openai_models = await get_openai_models()
  306. openai_models = openai_models["data"]
  307. if app.state.config.ENABLE_OLLAMA_API:
  308. ollama_models = await get_ollama_models()
  309. ollama_models = [
  310. {
  311. "id": model["model"],
  312. "name": model["name"],
  313. "object": "model",
  314. "created": int(time.time()),
  315. "owned_by": "ollama",
  316. "ollama": model,
  317. }
  318. for model in ollama_models["models"]
  319. ]
  320. models = openai_models + ollama_models
  321. custom_models = Models.get_all_models()
  322. for custom_model in custom_models:
  323. if custom_model.base_model_id == None:
  324. for model in models:
  325. if (
  326. custom_model.id == model["id"]
  327. or custom_model.id == model["id"].split(":")[0]
  328. ):
  329. model["name"] = custom_model.name
  330. model["info"] = custom_model.model_dump()
  331. else:
  332. owned_by = "openai"
  333. for model in models:
  334. if (
  335. custom_model.base_model_id == model["id"]
  336. or custom_model.base_model_id == model["id"].split(":")[0]
  337. ):
  338. owned_by = model["owned_by"]
  339. break
  340. models.append(
  341. {
  342. "id": custom_model.id,
  343. "name": custom_model.name,
  344. "object": "model",
  345. "created": custom_model.created_at,
  346. "owned_by": owned_by,
  347. "info": custom_model.model_dump(),
  348. "preset": True,
  349. }
  350. )
  351. app.state.MODELS = {model["id"]: model for model in models}
  352. webui_app.state.MODELS = app.state.MODELS
  353. return models
  354. @app.get("/api/models")
  355. async def get_models(user=Depends(get_verified_user)):
  356. models = await get_all_models()
  357. # Filter out filter pipelines
  358. models = [
  359. model
  360. for model in models
  361. if "pipeline" not in model or model["pipeline"].get("type", None) != "filter"
  362. ]
  363. if app.state.config.ENABLE_MODEL_FILTER:
  364. if user.role == "user":
  365. models = list(
  366. filter(
  367. lambda model: model["id"] in app.state.config.MODEL_FILTER_LIST,
  368. models,
  369. )
  370. )
  371. return {"data": models}
  372. return {"data": models}
  373. @app.get("/api/pipelines/list")
  374. async def get_pipelines_list(user=Depends(get_admin_user)):
  375. responses = await get_openai_models(raw=True)
  376. print(responses)
  377. urlIdxs = [idx for idx, response in enumerate(responses) if "pipelines" in response]
  378. return {
  379. "data": [
  380. {
  381. "url": openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx],
  382. "idx": urlIdx,
  383. }
  384. for urlIdx in urlIdxs
  385. ]
  386. }
  387. class AddPipelineForm(BaseModel):
  388. url: str
  389. urlIdx: int
  390. @app.post("/api/pipelines/add")
  391. async def add_pipeline(form_data: AddPipelineForm, user=Depends(get_admin_user)):
  392. r = None
  393. try:
  394. urlIdx = form_data.urlIdx
  395. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  396. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  397. headers = {"Authorization": f"Bearer {key}"}
  398. r = requests.post(
  399. f"{url}/pipelines/add", headers=headers, json={"url": form_data.url}
  400. )
  401. r.raise_for_status()
  402. data = r.json()
  403. return {**data}
  404. except Exception as e:
  405. # Handle connection error here
  406. print(f"Connection error: {e}")
  407. detail = "Pipeline not found"
  408. if r is not None:
  409. try:
  410. res = r.json()
  411. if "detail" in res:
  412. detail = res["detail"]
  413. except:
  414. pass
  415. raise HTTPException(
  416. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  417. detail=detail,
  418. )
  419. class DeletePipelineForm(BaseModel):
  420. id: str
  421. urlIdx: int
  422. @app.delete("/api/pipelines/delete")
  423. async def delete_pipeline(form_data: DeletePipelineForm, user=Depends(get_admin_user)):
  424. r = None
  425. try:
  426. urlIdx = form_data.urlIdx
  427. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  428. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  429. headers = {"Authorization": f"Bearer {key}"}
  430. r = requests.delete(
  431. f"{url}/pipelines/delete", headers=headers, json={"id": form_data.id}
  432. )
  433. r.raise_for_status()
  434. data = r.json()
  435. return {**data}
  436. except Exception as e:
  437. # Handle connection error here
  438. print(f"Connection error: {e}")
  439. detail = "Pipeline not found"
  440. if r is not None:
  441. try:
  442. res = r.json()
  443. if "detail" in res:
  444. detail = res["detail"]
  445. except:
  446. pass
  447. raise HTTPException(
  448. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  449. detail=detail,
  450. )
  451. @app.get("/api/pipelines")
  452. async def get_pipelines(urlIdx: Optional[int] = None, user=Depends(get_admin_user)):
  453. r = None
  454. try:
  455. urlIdx
  456. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  457. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  458. headers = {"Authorization": f"Bearer {key}"}
  459. r = requests.get(f"{url}/pipelines", headers=headers)
  460. r.raise_for_status()
  461. data = r.json()
  462. return {**data}
  463. except Exception as e:
  464. # Handle connection error here
  465. print(f"Connection error: {e}")
  466. detail = "Pipeline not found"
  467. if r is not None:
  468. try:
  469. res = r.json()
  470. if "detail" in res:
  471. detail = res["detail"]
  472. except:
  473. pass
  474. raise HTTPException(
  475. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  476. detail=detail,
  477. )
  478. @app.get("/api/pipelines/{pipeline_id}/valves")
  479. async def get_pipeline_valves(
  480. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  481. ):
  482. models = await get_all_models()
  483. r = None
  484. try:
  485. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  486. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  487. headers = {"Authorization": f"Bearer {key}"}
  488. r = requests.get(f"{url}/{pipeline_id}/valves", headers=headers)
  489. r.raise_for_status()
  490. data = r.json()
  491. return {**data}
  492. except Exception as e:
  493. # Handle connection error here
  494. print(f"Connection error: {e}")
  495. detail = "Pipeline not found"
  496. if r is not None:
  497. try:
  498. res = r.json()
  499. if "detail" in res:
  500. detail = res["detail"]
  501. except:
  502. pass
  503. raise HTTPException(
  504. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  505. detail=detail,
  506. )
  507. @app.get("/api/pipelines/{pipeline_id}/valves/spec")
  508. async def get_pipeline_valves_spec(
  509. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  510. ):
  511. models = await get_all_models()
  512. r = None
  513. try:
  514. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  515. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  516. headers = {"Authorization": f"Bearer {key}"}
  517. r = requests.get(f"{url}/{pipeline_id}/valves/spec", headers=headers)
  518. r.raise_for_status()
  519. data = r.json()
  520. return {**data}
  521. except Exception as e:
  522. # Handle connection error here
  523. print(f"Connection error: {e}")
  524. detail = "Pipeline not found"
  525. if r is not None:
  526. try:
  527. res = r.json()
  528. if "detail" in res:
  529. detail = res["detail"]
  530. except:
  531. pass
  532. raise HTTPException(
  533. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  534. detail=detail,
  535. )
  536. @app.post("/api/pipelines/{pipeline_id}/valves/update")
  537. async def update_pipeline_valves(
  538. urlIdx: Optional[int],
  539. pipeline_id: str,
  540. form_data: dict,
  541. user=Depends(get_admin_user),
  542. ):
  543. models = await get_all_models()
  544. r = None
  545. try:
  546. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  547. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  548. headers = {"Authorization": f"Bearer {key}"}
  549. r = requests.post(
  550. f"{url}/{pipeline_id}/valves/update",
  551. headers=headers,
  552. json={**form_data},
  553. )
  554. r.raise_for_status()
  555. data = r.json()
  556. return {**data}
  557. except Exception as e:
  558. # Handle connection error here
  559. print(f"Connection error: {e}")
  560. detail = "Pipeline not found"
  561. if r is not None:
  562. try:
  563. res = r.json()
  564. if "detail" in res:
  565. detail = res["detail"]
  566. except:
  567. pass
  568. raise HTTPException(
  569. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  570. detail=detail,
  571. )
  572. @app.get("/api/config")
  573. async def get_app_config():
  574. # Checking and Handling the Absence of 'ui' in CONFIG_DATA
  575. default_locale = "en-US"
  576. if "ui" in CONFIG_DATA:
  577. default_locale = CONFIG_DATA["ui"].get("default_locale", "en-US")
  578. # The Rest of the Function Now Uses the Variables Defined Above
  579. return {
  580. "status": True,
  581. "name": WEBUI_NAME,
  582. "version": VERSION,
  583. "default_locale": default_locale,
  584. "default_models": webui_app.state.config.DEFAULT_MODELS,
  585. "default_prompt_suggestions": webui_app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
  586. "features": {
  587. "auth": WEBUI_AUTH,
  588. "auth_trusted_header": bool(webui_app.state.AUTH_TRUSTED_EMAIL_HEADER),
  589. "enable_signup": webui_app.state.config.ENABLE_SIGNUP,
  590. "enable_web_search": RAG_WEB_SEARCH_ENABLED,
  591. "enable_image_generation": images_app.state.config.ENABLED,
  592. "enable_community_sharing": webui_app.state.config.ENABLE_COMMUNITY_SHARING,
  593. "enable_admin_export": ENABLE_ADMIN_EXPORT,
  594. },
  595. }
  596. @app.get("/api/config/model/filter")
  597. async def get_model_filter_config(user=Depends(get_admin_user)):
  598. return {
  599. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  600. "models": app.state.config.MODEL_FILTER_LIST,
  601. }
  602. class ModelFilterConfigForm(BaseModel):
  603. enabled: bool
  604. models: List[str]
  605. @app.post("/api/config/model/filter")
  606. async def update_model_filter_config(
  607. form_data: ModelFilterConfigForm, user=Depends(get_admin_user)
  608. ):
  609. app.state.config.ENABLE_MODEL_FILTER = form_data.enabled
  610. app.state.config.MODEL_FILTER_LIST = form_data.models
  611. return {
  612. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  613. "models": app.state.config.MODEL_FILTER_LIST,
  614. }
  615. @app.get("/api/webhook")
  616. async def get_webhook_url(user=Depends(get_admin_user)):
  617. return {
  618. "url": app.state.config.WEBHOOK_URL,
  619. }
  620. class UrlForm(BaseModel):
  621. url: str
  622. @app.post("/api/webhook")
  623. async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)):
  624. app.state.config.WEBHOOK_URL = form_data.url
  625. webui_app.state.WEBHOOK_URL = app.state.config.WEBHOOK_URL
  626. return {
  627. "url": app.state.config.WEBHOOK_URL,
  628. }
  629. @app.get("/api/community_sharing", response_model=bool)
  630. async def get_community_sharing_status(request: Request, user=Depends(get_admin_user)):
  631. return webui_app.state.config.ENABLE_COMMUNITY_SHARING
  632. @app.get("/api/community_sharing/toggle", response_model=bool)
  633. async def toggle_community_sharing(request: Request, user=Depends(get_admin_user)):
  634. webui_app.state.config.ENABLE_COMMUNITY_SHARING = (
  635. not webui_app.state.config.ENABLE_COMMUNITY_SHARING
  636. )
  637. return webui_app.state.config.ENABLE_COMMUNITY_SHARING
  638. @app.get("/api/version")
  639. async def get_app_config():
  640. return {
  641. "version": VERSION,
  642. }
  643. @app.get("/api/changelog")
  644. async def get_app_changelog():
  645. return {key: CHANGELOG[key] for idx, key in enumerate(CHANGELOG) if idx < 5}
  646. @app.get("/api/version/updates")
  647. async def get_app_latest_release_version():
  648. try:
  649. async with aiohttp.ClientSession() as session:
  650. async with session.get(
  651. "https://api.github.com/repos/open-webui/open-webui/releases/latest"
  652. ) as response:
  653. response.raise_for_status()
  654. data = await response.json()
  655. latest_version = data["tag_name"]
  656. return {"current": VERSION, "latest": latest_version[1:]}
  657. except aiohttp.ClientError as e:
  658. raise HTTPException(
  659. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  660. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  661. )
  662. @app.get("/manifest.json")
  663. async def get_manifest_json():
  664. return {
  665. "name": WEBUI_NAME,
  666. "short_name": WEBUI_NAME,
  667. "start_url": "/",
  668. "display": "standalone",
  669. "background_color": "#343541",
  670. "theme_color": "#343541",
  671. "orientation": "portrait-primary",
  672. "icons": [{"src": "/static/logo.png", "type": "image/png", "sizes": "500x500"}],
  673. }
  674. @app.get("/opensearch.xml")
  675. async def get_opensearch_xml():
  676. xml_content = rf"""
  677. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  678. <ShortName>{WEBUI_NAME}</ShortName>
  679. <Description>Search {WEBUI_NAME}</Description>
  680. <InputEncoding>UTF-8</InputEncoding>
  681. <Image width="16" height="16" type="image/x-icon">{WEBUI_URL}/favicon.png</Image>
  682. <Url type="text/html" method="get" template="{WEBUI_URL}/?q={"{searchTerms}"}"/>
  683. <moz:SearchForm>{WEBUI_URL}</moz:SearchForm>
  684. </OpenSearchDescription>
  685. """
  686. return Response(content=xml_content, media_type="application/xml")
  687. @app.get("/health")
  688. async def healthcheck():
  689. return {"status": True}
  690. app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
  691. app.mount("/cache", StaticFiles(directory=CACHE_DIR), name="cache")
  692. if os.path.exists(FRONTEND_BUILD_DIR):
  693. mimetypes.add_type("text/javascript", ".js")
  694. app.mount(
  695. "/",
  696. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  697. name="spa-static-files",
  698. )
  699. else:
  700. log.warning(
  701. f"Frontend build directory not found at '{FRONTEND_BUILD_DIR}'. Serving API only."
  702. )