main.py 29 KB

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