main.py 19 KB

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