main.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 import HTTPException
  15. from fastapi.middleware.wsgi import WSGIMiddleware
  16. from fastapi.middleware.cors import CORSMiddleware
  17. from starlette.exceptions import HTTPException as StarletteHTTPException
  18. from starlette.middleware.base import BaseHTTPMiddleware
  19. from starlette.responses import StreamingResponse, Response
  20. from apps.ollama.main import app as ollama_app, get_all_models as get_ollama_models
  21. from apps.openai.main import app as openai_app, get_all_models as get_openai_models
  22. from apps.audio.main import app as audio_app
  23. from apps.images.main import app as images_app
  24. from apps.rag.main import app as rag_app
  25. from apps.webui.main import app as webui_app
  26. import asyncio
  27. from pydantic import BaseModel
  28. from typing import List, Optional
  29. from apps.webui.models.models import Models, ModelModel
  30. from utils.utils import (
  31. get_admin_user,
  32. get_verified_user,
  33. get_current_user,
  34. get_http_authorization_cred,
  35. )
  36. from apps.rag.utils import rag_messages
  37. from config import (
  38. CONFIG_DATA,
  39. WEBUI_NAME,
  40. WEBUI_URL,
  41. WEBUI_AUTH,
  42. ENV,
  43. VERSION,
  44. CHANGELOG,
  45. FRONTEND_BUILD_DIR,
  46. CACHE_DIR,
  47. STATIC_DIR,
  48. ENABLE_OPENAI_API,
  49. ENABLE_OLLAMA_API,
  50. ENABLE_MODEL_FILTER,
  51. MODEL_FILTER_LIST,
  52. GLOBAL_LOG_LEVEL,
  53. SRC_LOG_LEVELS,
  54. WEBHOOK_URL,
  55. ENABLE_ADMIN_EXPORT,
  56. RAG_WEB_SEARCH_ENABLED,
  57. AppConfig,
  58. WEBUI_BUILD_HASH,
  59. )
  60. from constants import ERROR_MESSAGES
  61. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  62. log = logging.getLogger(__name__)
  63. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  64. class SPAStaticFiles(StaticFiles):
  65. async def get_response(self, path: str, scope):
  66. try:
  67. return await super().get_response(path, scope)
  68. except (HTTPException, StarletteHTTPException) as ex:
  69. if ex.status_code == 404:
  70. return await super().get_response("index.html", scope)
  71. else:
  72. raise ex
  73. print(
  74. rf"""
  75. ___ __ __ _ _ _ ___
  76. / _ \ _ __ ___ _ __ \ \ / /__| |__ | | | |_ _|
  77. | | | | '_ \ / _ \ '_ \ \ \ /\ / / _ \ '_ \| | | || |
  78. | |_| | |_) | __/ | | | \ V V / __/ |_) | |_| || |
  79. \___/| .__/ \___|_| |_| \_/\_/ \___|_.__/ \___/|___|
  80. |_|
  81. v{VERSION} - building the best open-source AI user interface.
  82. {f"Commit: {WEBUI_BUILD_HASH}" if WEBUI_BUILD_HASH != "dev-build" else ""}
  83. https://github.com/open-webui/open-webui
  84. """
  85. )
  86. @asynccontextmanager
  87. async def lifespan(app: FastAPI):
  88. yield
  89. app = FastAPI(
  90. docs_url="/docs" if ENV == "dev" else None, redoc_url=None, lifespan=lifespan
  91. )
  92. app.state.config = AppConfig()
  93. app.state.config.ENABLE_OPENAI_API = ENABLE_OPENAI_API
  94. app.state.config.ENABLE_OLLAMA_API = ENABLE_OLLAMA_API
  95. app.state.config.ENABLE_MODEL_FILTER = ENABLE_MODEL_FILTER
  96. app.state.config.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  97. app.state.config.WEBHOOK_URL = WEBHOOK_URL
  98. app.state.MODELS = {}
  99. origins = ["*"]
  100. app.add_middleware(
  101. CORSMiddleware,
  102. allow_origins=origins,
  103. allow_credentials=True,
  104. allow_methods=["*"],
  105. allow_headers=["*"],
  106. )
  107. # Custom middleware to add security headers
  108. # class SecurityHeadersMiddleware(BaseHTTPMiddleware):
  109. # async def dispatch(self, request: Request, call_next):
  110. # response: Response = await call_next(request)
  111. # response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
  112. # response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
  113. # return response
  114. # app.add_middleware(SecurityHeadersMiddleware)
  115. class RAGMiddleware(BaseHTTPMiddleware):
  116. async def dispatch(self, request: Request, call_next):
  117. return_citations = False
  118. if request.method == "POST" and (
  119. "/api/chat" in request.url.path or "/chat/completions" in request.url.path
  120. ):
  121. log.debug(f"request.url.path: {request.url.path}")
  122. # Read the original request body
  123. body = await request.body()
  124. # Decode body to string
  125. body_str = body.decode("utf-8")
  126. # Parse string to JSON
  127. data = json.loads(body_str) if body_str else {}
  128. return_citations = data.get("citations", False)
  129. if "citations" in data:
  130. del data["citations"]
  131. # Example: Add a new key-value pair or modify existing ones
  132. # data["modified"] = True # Example modification
  133. if "docs" in data:
  134. data = {**data}
  135. data["messages"], citations = rag_messages(
  136. docs=data["docs"],
  137. messages=data["messages"],
  138. template=rag_app.state.config.RAG_TEMPLATE,
  139. embedding_function=rag_app.state.EMBEDDING_FUNCTION,
  140. k=rag_app.state.config.TOP_K,
  141. reranking_function=rag_app.state.sentence_transformer_rf,
  142. r=rag_app.state.config.RELEVANCE_THRESHOLD,
  143. hybrid_search=rag_app.state.config.ENABLE_RAG_HYBRID_SEARCH,
  144. )
  145. del data["docs"]
  146. log.debug(
  147. f"data['messages']: {data['messages']}, citations: {citations}"
  148. )
  149. modified_body_bytes = json.dumps(data).encode("utf-8")
  150. # Replace the request body with the modified one
  151. request._body = modified_body_bytes
  152. # Set custom header to ensure content-length matches new body length
  153. request.headers.__dict__["_list"] = [
  154. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  155. *[
  156. (k, v)
  157. for k, v in request.headers.raw
  158. if k.lower() != b"content-length"
  159. ],
  160. ]
  161. response = await call_next(request)
  162. if return_citations:
  163. # Inject the citations into the response
  164. if isinstance(response, StreamingResponse):
  165. # If it's a streaming response, inject it as SSE event or NDJSON line
  166. content_type = response.headers.get("Content-Type")
  167. if "text/event-stream" in content_type:
  168. return StreamingResponse(
  169. self.openai_stream_wrapper(response.body_iterator, citations),
  170. )
  171. if "application/x-ndjson" in content_type:
  172. return StreamingResponse(
  173. self.ollama_stream_wrapper(response.body_iterator, citations),
  174. )
  175. return response
  176. async def _receive(self, body: bytes):
  177. return {"type": "http.request", "body": body, "more_body": False}
  178. async def openai_stream_wrapper(self, original_generator, citations):
  179. yield f"data: {json.dumps({'citations': citations})}\n\n"
  180. async for data in original_generator:
  181. yield data
  182. async def ollama_stream_wrapper(self, original_generator, citations):
  183. yield f"{json.dumps({'citations': citations})}\n"
  184. async for data in original_generator:
  185. yield data
  186. app.add_middleware(RAGMiddleware)
  187. class PipelineMiddleware(BaseHTTPMiddleware):
  188. async def dispatch(self, request: Request, call_next):
  189. if request.method == "POST" and (
  190. "/api/chat" in request.url.path or "/chat/completions" in request.url.path
  191. ):
  192. log.debug(f"request.url.path: {request.url.path}")
  193. # Read the original request body
  194. body = await request.body()
  195. # Decode body to string
  196. body_str = body.decode("utf-8")
  197. # Parse string to JSON
  198. data = json.loads(body_str) if body_str else {}
  199. model_id = data["model"]
  200. valves = [
  201. model
  202. for model in app.state.MODELS.values()
  203. if "pipeline" in model
  204. and model["pipeline"]["type"] == "valve"
  205. and model_id
  206. in [
  207. target_model["id"]
  208. for target_model in model["pipeline"]["pipelines"]
  209. ]
  210. ]
  211. sorted_valves = sorted(valves, key=lambda x: x["pipeline"]["priority"])
  212. user = None
  213. if len(sorted_valves) > 0:
  214. try:
  215. user = get_current_user(
  216. get_http_authorization_cred(
  217. request.headers.get("Authorization")
  218. )
  219. )
  220. user = {"id": user.id, "name": user.name, "role": user.role}
  221. except:
  222. pass
  223. for valve in sorted_valves:
  224. try:
  225. urlIdx = valve["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}/valve",
  232. headers=headers,
  233. json={
  234. "user": user,
  235. "model": valve["id"],
  236. "body": data,
  237. },
  238. )
  239. r.raise_for_status()
  240. data = r.json()
  241. except Exception as e:
  242. # Handle connection error here
  243. log.error(f"Connection error: {e}")
  244. pass
  245. modified_body_bytes = json.dumps(data).encode("utf-8")
  246. # Replace the request body with the modified one
  247. request._body = modified_body_bytes
  248. # Set custom header to ensure content-length matches new body length
  249. request.headers.__dict__["_list"] = [
  250. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  251. *[
  252. (k, v)
  253. for k, v in request.headers.raw
  254. if k.lower() != b"content-length"
  255. ],
  256. ]
  257. response = await call_next(request)
  258. return response
  259. async def _receive(self, body: bytes):
  260. return {"type": "http.request", "body": body, "more_body": False}
  261. app.add_middleware(PipelineMiddleware)
  262. @app.middleware("http")
  263. async def check_url(request: Request, call_next):
  264. if len(app.state.MODELS) == 0:
  265. await get_all_models()
  266. else:
  267. pass
  268. start_time = int(time.time())
  269. response = await call_next(request)
  270. process_time = int(time.time()) - start_time
  271. response.headers["X-Process-Time"] = str(process_time)
  272. return response
  273. @app.middleware("http")
  274. async def update_embedding_function(request: Request, call_next):
  275. response = await call_next(request)
  276. if "/embedding/update" in request.url.path:
  277. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  278. return response
  279. app.mount("/ollama", ollama_app)
  280. app.mount("/openai", openai_app)
  281. app.mount("/images/api/v1", images_app)
  282. app.mount("/audio/api/v1", audio_app)
  283. app.mount("/rag/api/v1", rag_app)
  284. app.mount("/api/v1", webui_app)
  285. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  286. async def get_all_models():
  287. openai_models = []
  288. ollama_models = []
  289. if app.state.config.ENABLE_OPENAI_API:
  290. openai_models = await get_openai_models()
  291. openai_models = openai_models["data"]
  292. if app.state.config.ENABLE_OLLAMA_API:
  293. ollama_models = await get_ollama_models()
  294. ollama_models = [
  295. {
  296. "id": model["model"],
  297. "name": model["name"],
  298. "object": "model",
  299. "created": int(time.time()),
  300. "owned_by": "ollama",
  301. "ollama": model,
  302. }
  303. for model in ollama_models["models"]
  304. ]
  305. models = openai_models + ollama_models
  306. custom_models = Models.get_all_models()
  307. for custom_model in custom_models:
  308. if custom_model.base_model_id == None:
  309. for model in models:
  310. if (
  311. custom_model.id == model["id"]
  312. or custom_model.id == model["id"].split(":")[0]
  313. ):
  314. model["name"] = custom_model.name
  315. model["info"] = custom_model.model_dump()
  316. else:
  317. owned_by = "openai"
  318. for model in models:
  319. if (
  320. custom_model.base_model_id == model["id"]
  321. or custom_model.base_model_id == model["id"].split(":")[0]
  322. ):
  323. owned_by = model["owned_by"]
  324. break
  325. models.append(
  326. {
  327. "id": custom_model.id,
  328. "name": custom_model.name,
  329. "object": "model",
  330. "created": custom_model.created_at,
  331. "owned_by": owned_by,
  332. "info": custom_model.model_dump(),
  333. "preset": True,
  334. }
  335. )
  336. app.state.MODELS = {model["id"]: model for model in models}
  337. webui_app.state.MODELS = app.state.MODELS
  338. return models
  339. @app.get("/api/models")
  340. async def get_models(user=Depends(get_verified_user)):
  341. models = await get_all_models()
  342. # Filter out valve models
  343. models = [
  344. model
  345. for model in models
  346. if "pipeline" not in model or model["pipeline"]["type"] != "valve"
  347. ]
  348. if app.state.config.ENABLE_MODEL_FILTER:
  349. if user.role == "user":
  350. models = list(
  351. filter(
  352. lambda model: model["id"] in app.state.config.MODEL_FILTER_LIST,
  353. models,
  354. )
  355. )
  356. return {"data": models}
  357. return {"data": models}
  358. @app.get("/api/config")
  359. async def get_app_config():
  360. # Checking and Handling the Absence of 'ui' in CONFIG_DATA
  361. default_locale = "en-US"
  362. if "ui" in CONFIG_DATA:
  363. default_locale = CONFIG_DATA["ui"].get("default_locale", "en-US")
  364. # The Rest of the Function Now Uses the Variables Defined Above
  365. return {
  366. "status": True,
  367. "name": WEBUI_NAME,
  368. "version": VERSION,
  369. "default_locale": default_locale,
  370. "default_models": webui_app.state.config.DEFAULT_MODELS,
  371. "default_prompt_suggestions": webui_app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
  372. "features": {
  373. "auth": WEBUI_AUTH,
  374. "auth_trusted_header": bool(webui_app.state.AUTH_TRUSTED_EMAIL_HEADER),
  375. "enable_signup": webui_app.state.config.ENABLE_SIGNUP,
  376. "enable_web_search": RAG_WEB_SEARCH_ENABLED,
  377. "enable_image_generation": images_app.state.config.ENABLED,
  378. "enable_community_sharing": webui_app.state.config.ENABLE_COMMUNITY_SHARING,
  379. "enable_admin_export": ENABLE_ADMIN_EXPORT,
  380. },
  381. }
  382. @app.get("/api/config/model/filter")
  383. async def get_model_filter_config(user=Depends(get_admin_user)):
  384. return {
  385. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  386. "models": app.state.config.MODEL_FILTER_LIST,
  387. }
  388. class ModelFilterConfigForm(BaseModel):
  389. enabled: bool
  390. models: List[str]
  391. @app.post("/api/config/model/filter")
  392. async def update_model_filter_config(
  393. form_data: ModelFilterConfigForm, user=Depends(get_admin_user)
  394. ):
  395. app.state.config.ENABLE_MODEL_FILTER = form_data.enabled
  396. app.state.config.MODEL_FILTER_LIST = form_data.models
  397. return {
  398. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  399. "models": app.state.config.MODEL_FILTER_LIST,
  400. }
  401. @app.get("/api/webhook")
  402. async def get_webhook_url(user=Depends(get_admin_user)):
  403. return {
  404. "url": app.state.config.WEBHOOK_URL,
  405. }
  406. class UrlForm(BaseModel):
  407. url: str
  408. @app.post("/api/webhook")
  409. async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)):
  410. app.state.config.WEBHOOK_URL = form_data.url
  411. webui_app.state.WEBHOOK_URL = app.state.config.WEBHOOK_URL
  412. return {
  413. "url": app.state.config.WEBHOOK_URL,
  414. }
  415. @app.get("/api/community_sharing", response_model=bool)
  416. async def get_community_sharing_status(request: Request, user=Depends(get_admin_user)):
  417. return webui_app.state.config.ENABLE_COMMUNITY_SHARING
  418. @app.get("/api/community_sharing/toggle", response_model=bool)
  419. async def toggle_community_sharing(request: Request, user=Depends(get_admin_user)):
  420. webui_app.state.config.ENABLE_COMMUNITY_SHARING = (
  421. not webui_app.state.config.ENABLE_COMMUNITY_SHARING
  422. )
  423. return webui_app.state.config.ENABLE_COMMUNITY_SHARING
  424. @app.get("/api/version")
  425. async def get_app_config():
  426. return {
  427. "version": VERSION,
  428. }
  429. @app.get("/api/changelog")
  430. async def get_app_changelog():
  431. return {key: CHANGELOG[key] for idx, key in enumerate(CHANGELOG) if idx < 5}
  432. @app.get("/api/version/updates")
  433. async def get_app_latest_release_version():
  434. try:
  435. async with aiohttp.ClientSession() as session:
  436. async with session.get(
  437. "https://api.github.com/repos/open-webui/open-webui/releases/latest"
  438. ) as response:
  439. response.raise_for_status()
  440. data = await response.json()
  441. latest_version = data["tag_name"]
  442. return {"current": VERSION, "latest": latest_version[1:]}
  443. except aiohttp.ClientError as e:
  444. raise HTTPException(
  445. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  446. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  447. )
  448. @app.get("/manifest.json")
  449. async def get_manifest_json():
  450. return {
  451. "name": WEBUI_NAME,
  452. "short_name": WEBUI_NAME,
  453. "start_url": "/",
  454. "display": "standalone",
  455. "background_color": "#343541",
  456. "theme_color": "#343541",
  457. "orientation": "portrait-primary",
  458. "icons": [{"src": "/static/logo.png", "type": "image/png", "sizes": "500x500"}],
  459. }
  460. @app.get("/opensearch.xml")
  461. async def get_opensearch_xml():
  462. xml_content = rf"""
  463. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  464. <ShortName>{WEBUI_NAME}</ShortName>
  465. <Description>Search {WEBUI_NAME}</Description>
  466. <InputEncoding>UTF-8</InputEncoding>
  467. <Image width="16" height="16" type="image/x-icon">{WEBUI_URL}/favicon.png</Image>
  468. <Url type="text/html" method="get" template="{WEBUI_URL}/?q={"{searchTerms}"}"/>
  469. <moz:SearchForm>{WEBUI_URL}</moz:SearchForm>
  470. </OpenSearchDescription>
  471. """
  472. return Response(content=xml_content, media_type="application/xml")
  473. @app.get("/health")
  474. async def healthcheck():
  475. return {"status": True}
  476. app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
  477. app.mount("/cache", StaticFiles(directory=CACHE_DIR), name="cache")
  478. if os.path.exists(FRONTEND_BUILD_DIR):
  479. mimetypes.add_type("text/javascript", ".js")
  480. app.mount(
  481. "/",
  482. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  483. name="spa-static-files",
  484. )
  485. else:
  486. log.warning(
  487. f"Frontend build directory not found at '{FRONTEND_BUILD_DIR}'. Serving API only."
  488. )