main.py 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  1. import base64
  2. import inspect
  3. import json
  4. import logging
  5. import mimetypes
  6. import os
  7. import shutil
  8. import sys
  9. import time
  10. import uuid
  11. from contextlib import asynccontextmanager
  12. from typing import Optional
  13. import aiohttp
  14. import requests
  15. from open_webui.apps.audio.main import app as audio_app
  16. from open_webui.apps.images.main import app as images_app
  17. from open_webui.apps.ollama.main import app as ollama_app
  18. from open_webui.apps.ollama.main import (
  19. generate_openai_chat_completion as generate_ollama_chat_completion,
  20. )
  21. from open_webui.apps.ollama.main import get_all_models as get_ollama_models
  22. from open_webui.apps.openai.main import app as openai_app
  23. from open_webui.apps.openai.main import (
  24. generate_chat_completion as generate_openai_chat_completion,
  25. )
  26. from open_webui.apps.openai.main import get_all_models as get_openai_models
  27. from open_webui.apps.rag.main import app as rag_app
  28. from open_webui.apps.rag.utils import get_rag_context, rag_template
  29. from open_webui.apps.socket.main import app as socket_app
  30. from open_webui.apps.socket.main import get_event_call, get_event_emitter
  31. from open_webui.apps.webui.internal.db import Session
  32. from open_webui.apps.webui.main import app as webui_app
  33. from open_webui.apps.webui.main import (
  34. generate_function_chat_completion,
  35. get_pipe_models,
  36. )
  37. from open_webui.apps.webui.models.auths import Auths
  38. from open_webui.apps.webui.models.functions import Functions
  39. from open_webui.apps.webui.models.models import Models
  40. from open_webui.apps.webui.models.users import UserModel, Users
  41. from open_webui.apps.webui.utils import load_function_module_by_id
  42. from authlib.integrations.starlette_client import OAuth
  43. from authlib.oidc.core import UserInfo
  44. from open_webui.config import (
  45. CACHE_DIR,
  46. CORS_ALLOW_ORIGIN,
  47. DEFAULT_LOCALE,
  48. ENABLE_ADMIN_CHAT_ACCESS,
  49. ENABLE_ADMIN_EXPORT,
  50. ENABLE_MODEL_FILTER,
  51. ENABLE_OAUTH_SIGNUP,
  52. ENABLE_OLLAMA_API,
  53. ENABLE_OPENAI_API,
  54. ENV,
  55. FRONTEND_BUILD_DIR,
  56. MODEL_FILTER_LIST,
  57. OAUTH_MERGE_ACCOUNTS_BY_EMAIL,
  58. OAUTH_PROVIDERS,
  59. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  60. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  61. STATIC_DIR,
  62. TASK_MODEL,
  63. TASK_MODEL_EXTERNAL,
  64. TITLE_GENERATION_PROMPT_TEMPLATE,
  65. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  66. WEBHOOK_URL,
  67. WEBUI_AUTH,
  68. WEBUI_NAME,
  69. AppConfig,
  70. run_migrations,
  71. )
  72. from open_webui.constants import ERROR_MESSAGES, TASKS, WEBHOOK_MESSAGES
  73. from open_webui.env import (
  74. CHANGELOG,
  75. GLOBAL_LOG_LEVEL,
  76. SAFE_MODE,
  77. SRC_LOG_LEVELS,
  78. VERSION,
  79. WEBUI_BUILD_HASH,
  80. WEBUI_SECRET_KEY,
  81. WEBUI_SESSION_COOKIE_SAME_SITE,
  82. WEBUI_SESSION_COOKIE_SECURE,
  83. WEBUI_URL,
  84. )
  85. from fastapi import (
  86. Depends,
  87. FastAPI,
  88. File,
  89. Form,
  90. HTTPException,
  91. Request,
  92. UploadFile,
  93. status,
  94. )
  95. from fastapi.middleware.cors import CORSMiddleware
  96. from fastapi.responses import JSONResponse
  97. from fastapi.staticfiles import StaticFiles
  98. from pydantic import BaseModel
  99. from sqlalchemy import text
  100. from starlette.exceptions import HTTPException as StarletteHTTPException
  101. from starlette.middleware.base import BaseHTTPMiddleware
  102. from starlette.middleware.sessions import SessionMiddleware
  103. from starlette.responses import RedirectResponse, Response, StreamingResponse
  104. from open_webui.utils.misc import (
  105. add_or_update_system_message,
  106. get_last_user_message,
  107. parse_duration,
  108. prepend_to_first_user_message_content,
  109. )
  110. from open_webui.utils.task import (
  111. moa_response_generation_template,
  112. search_query_generation_template,
  113. title_generation_template,
  114. tools_function_calling_generation_template,
  115. )
  116. from open_webui.utils.tools import get_tools
  117. from open_webui.utils.utils import (
  118. create_token,
  119. decode_token,
  120. get_admin_user,
  121. get_current_user,
  122. get_http_authorization_cred,
  123. get_password_hash,
  124. get_verified_user,
  125. )
  126. from open_webui.utils.webhook import post_webhook
  127. if SAFE_MODE:
  128. print("SAFE MODE ENABLED")
  129. Functions.deactivate_all_functions()
  130. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  131. log = logging.getLogger(__name__)
  132. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  133. class SPAStaticFiles(StaticFiles):
  134. async def get_response(self, path: str, scope):
  135. try:
  136. return await super().get_response(path, scope)
  137. except (HTTPException, StarletteHTTPException) as ex:
  138. if ex.status_code == 404:
  139. return await super().get_response("index.html", scope)
  140. else:
  141. raise ex
  142. print(
  143. rf"""
  144. ___ __ __ _ _ _ ___
  145. / _ \ _ __ ___ _ __ \ \ / /__| |__ | | | |_ _|
  146. | | | | '_ \ / _ \ '_ \ \ \ /\ / / _ \ '_ \| | | || |
  147. | |_| | |_) | __/ | | | \ V V / __/ |_) | |_| || |
  148. \___/| .__/ \___|_| |_| \_/\_/ \___|_.__/ \___/|___|
  149. |_|
  150. v{VERSION} - building the best open-source AI user interface.
  151. {f"Commit: {WEBUI_BUILD_HASH}" if WEBUI_BUILD_HASH != "dev-build" else ""}
  152. https://github.com/open-webui/open-webui
  153. """
  154. )
  155. @asynccontextmanager
  156. async def lifespan(app: FastAPI):
  157. run_migrations()
  158. yield
  159. app = FastAPI(
  160. docs_url="/docs" if ENV == "dev" else None, redoc_url=None, lifespan=lifespan
  161. )
  162. app.state.config = AppConfig()
  163. app.state.config.ENABLE_OPENAI_API = ENABLE_OPENAI_API
  164. app.state.config.ENABLE_OLLAMA_API = ENABLE_OLLAMA_API
  165. app.state.config.ENABLE_MODEL_FILTER = ENABLE_MODEL_FILTER
  166. app.state.config.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  167. app.state.config.WEBHOOK_URL = WEBHOOK_URL
  168. app.state.config.TASK_MODEL = TASK_MODEL
  169. app.state.config.TASK_MODEL_EXTERNAL = TASK_MODEL_EXTERNAL
  170. app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = TITLE_GENERATION_PROMPT_TEMPLATE
  171. app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE = (
  172. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  173. )
  174. app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD = (
  175. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD
  176. )
  177. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  178. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  179. )
  180. app.state.MODELS = {}
  181. ##################################
  182. #
  183. # ChatCompletion Middleware
  184. #
  185. ##################################
  186. def get_task_model_id(default_model_id):
  187. # Set the task model
  188. task_model_id = default_model_id
  189. # Check if the user has a custom task model and use that model
  190. if app.state.MODELS[task_model_id]["owned_by"] == "ollama":
  191. if (
  192. app.state.config.TASK_MODEL
  193. and app.state.config.TASK_MODEL in app.state.MODELS
  194. ):
  195. task_model_id = app.state.config.TASK_MODEL
  196. else:
  197. if (
  198. app.state.config.TASK_MODEL_EXTERNAL
  199. and app.state.config.TASK_MODEL_EXTERNAL in app.state.MODELS
  200. ):
  201. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  202. return task_model_id
  203. def get_filter_function_ids(model):
  204. def get_priority(function_id):
  205. function = Functions.get_function_by_id(function_id)
  206. if function is not None and hasattr(function, "valves"):
  207. # TODO: Fix FunctionModel
  208. return (function.valves if function.valves else {}).get("priority", 0)
  209. return 0
  210. filter_ids = [function.id for function in Functions.get_global_filter_functions()]
  211. if "info" in model and "meta" in model["info"]:
  212. filter_ids.extend(model["info"]["meta"].get("filterIds", []))
  213. filter_ids = list(set(filter_ids))
  214. enabled_filter_ids = [
  215. function.id
  216. for function in Functions.get_functions_by_type("filter", active_only=True)
  217. ]
  218. filter_ids = [
  219. filter_id for filter_id in filter_ids if filter_id in enabled_filter_ids
  220. ]
  221. filter_ids.sort(key=get_priority)
  222. return filter_ids
  223. async def chat_completion_filter_functions_handler(body, model, extra_params):
  224. skip_files = None
  225. filter_ids = get_filter_function_ids(model)
  226. for filter_id in filter_ids:
  227. filter = Functions.get_function_by_id(filter_id)
  228. if not filter:
  229. continue
  230. if filter_id in webui_app.state.FUNCTIONS:
  231. function_module = webui_app.state.FUNCTIONS[filter_id]
  232. else:
  233. function_module, _, _ = load_function_module_by_id(filter_id)
  234. webui_app.state.FUNCTIONS[filter_id] = function_module
  235. # Check if the function has a file_handler variable
  236. if hasattr(function_module, "file_handler"):
  237. skip_files = function_module.file_handler
  238. if hasattr(function_module, "valves") and hasattr(function_module, "Valves"):
  239. valves = Functions.get_function_valves_by_id(filter_id)
  240. function_module.valves = function_module.Valves(
  241. **(valves if valves else {})
  242. )
  243. if not hasattr(function_module, "inlet"):
  244. continue
  245. try:
  246. inlet = function_module.inlet
  247. # Get the signature of the function
  248. sig = inspect.signature(inlet)
  249. params = {"body": body} | {
  250. k: v
  251. for k, v in {
  252. **extra_params,
  253. "__model__": model,
  254. "__id__": filter_id,
  255. }.items()
  256. if k in sig.parameters
  257. }
  258. if "__user__" in params and hasattr(function_module, "UserValves"):
  259. try:
  260. params["__user__"]["valves"] = function_module.UserValves(
  261. **Functions.get_user_valves_by_id_and_user_id(
  262. filter_id, params["__user__"]["id"]
  263. )
  264. )
  265. except Exception as e:
  266. print(e)
  267. if inspect.iscoroutinefunction(inlet):
  268. body = await inlet(**params)
  269. else:
  270. body = inlet(**params)
  271. except Exception as e:
  272. print(f"Error: {e}")
  273. raise e
  274. if skip_files and "files" in body.get("metadata", {}):
  275. del body["metadata"]["files"]
  276. return body, {}
  277. def get_tools_function_calling_payload(messages, task_model_id, content):
  278. user_message = get_last_user_message(messages)
  279. history = "\n".join(
  280. f"{message['role'].upper()}: \"\"\"{message['content']}\"\"\""
  281. for message in messages[::-1][:4]
  282. )
  283. prompt = f"History:\n{history}\nQuery: {user_message}"
  284. return {
  285. "model": task_model_id,
  286. "messages": [
  287. {"role": "system", "content": content},
  288. {"role": "user", "content": f"Query: {prompt}"},
  289. ],
  290. "stream": False,
  291. "metadata": {"task": str(TASKS.FUNCTION_CALLING)},
  292. }
  293. async def get_content_from_response(response) -> Optional[str]:
  294. content = None
  295. if hasattr(response, "body_iterator"):
  296. async for chunk in response.body_iterator:
  297. data = json.loads(chunk.decode("utf-8"))
  298. content = data["choices"][0]["message"]["content"]
  299. # Cleanup any remaining background tasks if necessary
  300. if response.background is not None:
  301. await response.background()
  302. else:
  303. content = response["choices"][0]["message"]["content"]
  304. return content
  305. async def chat_completion_tools_handler(
  306. body: dict, user: UserModel, extra_params: dict
  307. ) -> tuple[dict, dict]:
  308. # If tool_ids field is present, call the functions
  309. metadata = body.get("metadata", {})
  310. tool_ids = metadata.get("tool_ids", None)
  311. log.debug(f"{tool_ids=}")
  312. if not tool_ids:
  313. return body, {}
  314. skip_files = False
  315. contexts = []
  316. citations = []
  317. task_model_id = get_task_model_id(body["model"])
  318. tools = get_tools(
  319. webui_app,
  320. tool_ids,
  321. user,
  322. {
  323. **extra_params,
  324. "__model__": app.state.MODELS[task_model_id],
  325. "__messages__": body["messages"],
  326. "__files__": metadata.get("files", []),
  327. },
  328. )
  329. log.info(f"{tools=}")
  330. specs = [tool["spec"] for tool in tools.values()]
  331. tools_specs = json.dumps(specs)
  332. tools_function_calling_prompt = tools_function_calling_generation_template(
  333. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE, tools_specs
  334. )
  335. log.info(f"{tools_function_calling_prompt=}")
  336. payload = get_tools_function_calling_payload(
  337. body["messages"], task_model_id, tools_function_calling_prompt
  338. )
  339. try:
  340. payload = filter_pipeline(payload, user)
  341. except Exception as e:
  342. raise e
  343. try:
  344. response = await generate_chat_completions(form_data=payload, user=user)
  345. log.debug(f"{response=}")
  346. content = await get_content_from_response(response)
  347. log.debug(f"{content=}")
  348. if not content:
  349. return body, {}
  350. result = json.loads(content)
  351. tool_function_name = result.get("name", None)
  352. if tool_function_name not in tools:
  353. return body, {}
  354. tool_function_params = result.get("parameters", {})
  355. try:
  356. tool_output = await tools[tool_function_name]["callable"](
  357. **tool_function_params
  358. )
  359. except Exception as e:
  360. tool_output = str(e)
  361. if tools[tool_function_name]["citation"]:
  362. citations.append(
  363. {
  364. "source": {
  365. "name": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}"
  366. },
  367. "document": [tool_output],
  368. "metadata": [{"source": tool_function_name}],
  369. }
  370. )
  371. if tools[tool_function_name]["file_handler"]:
  372. skip_files = True
  373. if isinstance(tool_output, str):
  374. contexts.append(tool_output)
  375. except Exception as e:
  376. log.exception(f"Error: {e}")
  377. content = None
  378. log.debug(f"tool_contexts: {contexts}")
  379. if skip_files and "files" in body.get("metadata", {}):
  380. del body["metadata"]["files"]
  381. return body, {"contexts": contexts, "citations": citations}
  382. async def chat_completion_files_handler(body) -> tuple[dict, dict[str, list]]:
  383. contexts = []
  384. citations = []
  385. if files := body.get("metadata", {}).get("files", None):
  386. contexts, citations = get_rag_context(
  387. files=files,
  388. messages=body["messages"],
  389. embedding_function=rag_app.state.EMBEDDING_FUNCTION,
  390. k=rag_app.state.config.TOP_K,
  391. reranking_function=rag_app.state.sentence_transformer_rf,
  392. r=rag_app.state.config.RELEVANCE_THRESHOLD,
  393. hybrid_search=rag_app.state.config.ENABLE_RAG_HYBRID_SEARCH,
  394. )
  395. log.debug(f"rag_contexts: {contexts}, citations: {citations}")
  396. return body, {"contexts": contexts, "citations": citations}
  397. def is_chat_completion_request(request):
  398. return request.method == "POST" and any(
  399. endpoint in request.url.path
  400. for endpoint in ["/ollama/api/chat", "/chat/completions"]
  401. )
  402. async def get_body_and_model_and_user(request):
  403. # Read the original request body
  404. body = await request.body()
  405. body_str = body.decode("utf-8")
  406. body = json.loads(body_str) if body_str else {}
  407. model_id = body["model"]
  408. if model_id not in app.state.MODELS:
  409. raise Exception("Model not found")
  410. model = app.state.MODELS[model_id]
  411. user = get_current_user(
  412. request,
  413. get_http_authorization_cred(request.headers.get("Authorization")),
  414. )
  415. return body, model, user
  416. class ChatCompletionMiddleware(BaseHTTPMiddleware):
  417. async def dispatch(self, request: Request, call_next):
  418. if not is_chat_completion_request(request):
  419. return await call_next(request)
  420. log.debug(f"request.url.path: {request.url.path}")
  421. try:
  422. body, model, user = await get_body_and_model_and_user(request)
  423. except Exception as e:
  424. return JSONResponse(
  425. status_code=status.HTTP_400_BAD_REQUEST,
  426. content={"detail": str(e)},
  427. )
  428. metadata = {
  429. "chat_id": body.pop("chat_id", None),
  430. "message_id": body.pop("id", None),
  431. "session_id": body.pop("session_id", None),
  432. "tool_ids": body.get("tool_ids", None),
  433. "files": body.get("files", None),
  434. }
  435. body["metadata"] = metadata
  436. extra_params = {
  437. "__event_emitter__": get_event_emitter(metadata),
  438. "__event_call__": get_event_call(metadata),
  439. "__user__": {
  440. "id": user.id,
  441. "email": user.email,
  442. "name": user.name,
  443. "role": user.role,
  444. },
  445. }
  446. # Initialize data_items to store additional data to be sent to the client
  447. # Initalize contexts and citation
  448. data_items = []
  449. contexts = []
  450. citations = []
  451. try:
  452. body, flags = await chat_completion_filter_functions_handler(
  453. body, model, extra_params
  454. )
  455. except Exception as e:
  456. return JSONResponse(
  457. status_code=status.HTTP_400_BAD_REQUEST,
  458. content={"detail": str(e)},
  459. )
  460. metadata = {
  461. **metadata,
  462. "tool_ids": body.pop("tool_ids", None),
  463. "files": body.pop("files", None),
  464. }
  465. body["metadata"] = metadata
  466. try:
  467. body, flags = await chat_completion_tools_handler(body, user, extra_params)
  468. contexts.extend(flags.get("contexts", []))
  469. citations.extend(flags.get("citations", []))
  470. except Exception as e:
  471. log.exception(e)
  472. try:
  473. body, flags = await chat_completion_files_handler(body)
  474. contexts.extend(flags.get("contexts", []))
  475. citations.extend(flags.get("citations", []))
  476. except Exception as e:
  477. log.exception(e)
  478. # If context is not empty, insert it into the messages
  479. if len(contexts) > 0:
  480. context_string = "/n".join(contexts).strip()
  481. prompt = get_last_user_message(body["messages"])
  482. if prompt is None:
  483. raise Exception("No user message found")
  484. # Workaround for Ollama 2.0+ system prompt issue
  485. # TODO: replace with add_or_update_system_message
  486. if model["owned_by"] == "ollama":
  487. body["messages"] = prepend_to_first_user_message_content(
  488. rag_template(
  489. rag_app.state.config.RAG_TEMPLATE, context_string, prompt
  490. ),
  491. body["messages"],
  492. )
  493. else:
  494. body["messages"] = add_or_update_system_message(
  495. rag_template(
  496. rag_app.state.config.RAG_TEMPLATE, context_string, prompt
  497. ),
  498. body["messages"],
  499. )
  500. # If there are citations, add them to the data_items
  501. if len(citations) > 0:
  502. data_items.append({"citations": citations})
  503. modified_body_bytes = json.dumps(body).encode("utf-8")
  504. # Replace the request body with the modified one
  505. request._body = modified_body_bytes
  506. # Set custom header to ensure content-length matches new body length
  507. request.headers.__dict__["_list"] = [
  508. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  509. *[(k, v) for k, v in request.headers.raw if k.lower() != b"content-length"],
  510. ]
  511. response = await call_next(request)
  512. if not isinstance(response, StreamingResponse):
  513. return response
  514. content_type = response.headers["Content-Type"]
  515. is_openai = "text/event-stream" in content_type
  516. is_ollama = "application/x-ndjson" in content_type
  517. if not is_openai and not is_ollama:
  518. return response
  519. def wrap_item(item):
  520. return f"data: {item}\n\n" if is_openai else f"{item}\n"
  521. async def stream_wrapper(original_generator, data_items):
  522. for item in data_items:
  523. yield wrap_item(json.dumps(item))
  524. async for data in original_generator:
  525. yield data
  526. return StreamingResponse(
  527. stream_wrapper(response.body_iterator, data_items),
  528. headers=dict(response.headers),
  529. )
  530. async def _receive(self, body: bytes):
  531. return {"type": "http.request", "body": body, "more_body": False}
  532. app.add_middleware(ChatCompletionMiddleware)
  533. ##################################
  534. #
  535. # Pipeline Middleware
  536. #
  537. ##################################
  538. def get_sorted_filters(model_id):
  539. filters = [
  540. model
  541. for model in app.state.MODELS.values()
  542. if "pipeline" in model
  543. and "type" in model["pipeline"]
  544. and model["pipeline"]["type"] == "filter"
  545. and (
  546. model["pipeline"]["pipelines"] == ["*"]
  547. or any(
  548. model_id == target_model_id
  549. for target_model_id in model["pipeline"]["pipelines"]
  550. )
  551. )
  552. ]
  553. sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"])
  554. return sorted_filters
  555. def filter_pipeline(payload, user):
  556. user = {"id": user.id, "email": user.email, "name": user.name, "role": user.role}
  557. model_id = payload["model"]
  558. sorted_filters = get_sorted_filters(model_id)
  559. model = app.state.MODELS[model_id]
  560. if "pipeline" in model:
  561. sorted_filters.append(model)
  562. for filter in sorted_filters:
  563. r = None
  564. try:
  565. urlIdx = filter["urlIdx"]
  566. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  567. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  568. if key == "":
  569. continue
  570. headers = {"Authorization": f"Bearer {key}"}
  571. r = requests.post(
  572. f"{url}/{filter['id']}/filter/inlet",
  573. headers=headers,
  574. json={
  575. "user": user,
  576. "body": payload,
  577. },
  578. )
  579. r.raise_for_status()
  580. payload = r.json()
  581. except Exception as e:
  582. # Handle connection error here
  583. print(f"Connection error: {e}")
  584. if r is not None:
  585. res = r.json()
  586. if "detail" in res:
  587. raise Exception(r.status_code, res["detail"])
  588. return payload
  589. class PipelineMiddleware(BaseHTTPMiddleware):
  590. async def dispatch(self, request: Request, call_next):
  591. if not is_chat_completion_request(request):
  592. return await call_next(request)
  593. log.debug(f"request.url.path: {request.url.path}")
  594. # Read the original request body
  595. body = await request.body()
  596. # Decode body to string
  597. body_str = body.decode("utf-8")
  598. # Parse string to JSON
  599. data = json.loads(body_str) if body_str else {}
  600. user = get_current_user(
  601. request,
  602. get_http_authorization_cred(request.headers["Authorization"]),
  603. )
  604. try:
  605. data = filter_pipeline(data, user)
  606. except Exception as e:
  607. if len(e.args) > 1:
  608. return JSONResponse(
  609. status_code=e.args[0],
  610. content={"detail": e.args[1]},
  611. )
  612. else:
  613. return JSONResponse(
  614. status_code=status.HTTP_400_BAD_REQUEST,
  615. content={"detail": str(e)},
  616. )
  617. modified_body_bytes = json.dumps(data).encode("utf-8")
  618. # Replace the request body with the modified one
  619. request._body = modified_body_bytes
  620. # Set custom header to ensure content-length matches new body length
  621. request.headers.__dict__["_list"] = [
  622. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  623. *[(k, v) for k, v in request.headers.raw if k.lower() != b"content-length"],
  624. ]
  625. response = await call_next(request)
  626. return response
  627. async def _receive(self, body: bytes):
  628. return {"type": "http.request", "body": body, "more_body": False}
  629. app.add_middleware(PipelineMiddleware)
  630. app.add_middleware(
  631. CORSMiddleware,
  632. allow_origins=CORS_ALLOW_ORIGIN,
  633. allow_credentials=True,
  634. allow_methods=["*"],
  635. allow_headers=["*"],
  636. )
  637. @app.middleware("http")
  638. async def commit_session_after_request(request: Request, call_next):
  639. response = await call_next(request)
  640. log.debug("Commit session after request")
  641. Session.commit()
  642. return response
  643. @app.middleware("http")
  644. async def check_url(request: Request, call_next):
  645. if len(app.state.MODELS) == 0:
  646. await get_all_models()
  647. else:
  648. pass
  649. start_time = int(time.time())
  650. response = await call_next(request)
  651. process_time = int(time.time()) - start_time
  652. response.headers["X-Process-Time"] = str(process_time)
  653. return response
  654. @app.middleware("http")
  655. async def update_embedding_function(request: Request, call_next):
  656. response = await call_next(request)
  657. if "/embedding/update" in request.url.path:
  658. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  659. return response
  660. app.mount("/ws", socket_app)
  661. app.mount("/ollama", ollama_app)
  662. app.mount("/openai", openai_app)
  663. app.mount("/images/api/v1", images_app)
  664. app.mount("/audio/api/v1", audio_app)
  665. app.mount("/rag/api/v1", rag_app)
  666. app.mount("/api/v1", webui_app)
  667. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  668. async def get_all_models():
  669. # TODO: Optimize this function
  670. pipe_models = []
  671. openai_models = []
  672. ollama_models = []
  673. pipe_models = await get_pipe_models()
  674. if app.state.config.ENABLE_OPENAI_API:
  675. openai_models = await get_openai_models()
  676. openai_models = openai_models["data"]
  677. if app.state.config.ENABLE_OLLAMA_API:
  678. ollama_models = await get_ollama_models()
  679. ollama_models = [
  680. {
  681. "id": model["model"],
  682. "name": model["name"],
  683. "object": "model",
  684. "created": int(time.time()),
  685. "owned_by": "ollama",
  686. "ollama": model,
  687. }
  688. for model in ollama_models["models"]
  689. ]
  690. models = pipe_models + openai_models + ollama_models
  691. global_action_ids = [
  692. function.id for function in Functions.get_global_action_functions()
  693. ]
  694. enabled_action_ids = [
  695. function.id
  696. for function in Functions.get_functions_by_type("action", active_only=True)
  697. ]
  698. custom_models = Models.get_all_models()
  699. for custom_model in custom_models:
  700. if custom_model.base_model_id is None:
  701. for model in models:
  702. if (
  703. custom_model.id == model["id"]
  704. or custom_model.id == model["id"].split(":")[0]
  705. ):
  706. model["name"] = custom_model.name
  707. model["info"] = custom_model.model_dump()
  708. action_ids = []
  709. if "info" in model and "meta" in model["info"]:
  710. action_ids.extend(model["info"]["meta"].get("actionIds", []))
  711. model["action_ids"] = action_ids
  712. else:
  713. owned_by = "openai"
  714. pipe = None
  715. action_ids = []
  716. for model in models:
  717. if (
  718. custom_model.base_model_id == model["id"]
  719. or custom_model.base_model_id == model["id"].split(":")[0]
  720. ):
  721. owned_by = model["owned_by"]
  722. if "pipe" in model:
  723. pipe = model["pipe"]
  724. if "info" in model and "meta" in model["info"]:
  725. action_ids.extend(model["info"]["meta"].get("actionIds", []))
  726. break
  727. models.append(
  728. {
  729. "id": custom_model.id,
  730. "name": custom_model.name,
  731. "object": "model",
  732. "created": custom_model.created_at,
  733. "owned_by": owned_by,
  734. "info": custom_model.model_dump(),
  735. "preset": True,
  736. **({"pipe": pipe} if pipe is not None else {}),
  737. "action_ids": action_ids,
  738. }
  739. )
  740. for model in models:
  741. action_ids = []
  742. if "action_ids" in model:
  743. action_ids = model["action_ids"]
  744. del model["action_ids"]
  745. action_ids = action_ids + global_action_ids
  746. action_ids = list(set(action_ids))
  747. action_ids = [
  748. action_id for action_id in action_ids if action_id in enabled_action_ids
  749. ]
  750. model["actions"] = []
  751. for action_id in action_ids:
  752. action = Functions.get_function_by_id(action_id)
  753. if action is None:
  754. raise Exception(f"Action not found: {action_id}")
  755. if action_id in webui_app.state.FUNCTIONS:
  756. function_module = webui_app.state.FUNCTIONS[action_id]
  757. else:
  758. function_module, _, _ = load_function_module_by_id(action_id)
  759. webui_app.state.FUNCTIONS[action_id] = function_module
  760. __webui__ = False
  761. if hasattr(function_module, "__webui__"):
  762. __webui__ = function_module.__webui__
  763. if hasattr(function_module, "actions"):
  764. actions = function_module.actions
  765. model["actions"].extend(
  766. [
  767. {
  768. "id": f"{action_id}.{_action['id']}",
  769. "name": _action.get(
  770. "name", f"{action.name} ({_action['id']})"
  771. ),
  772. "description": action.meta.description,
  773. "icon_url": _action.get(
  774. "icon_url", action.meta.manifest.get("icon_url", None)
  775. ),
  776. **({"__webui__": __webui__} if __webui__ else {}),
  777. }
  778. for _action in actions
  779. ]
  780. )
  781. else:
  782. model["actions"].append(
  783. {
  784. "id": action_id,
  785. "name": action.name,
  786. "description": action.meta.description,
  787. "icon_url": action.meta.manifest.get("icon_url", None),
  788. **({"__webui__": __webui__} if __webui__ else {}),
  789. }
  790. )
  791. app.state.MODELS = {model["id"]: model for model in models}
  792. webui_app.state.MODELS = app.state.MODELS
  793. return models
  794. @app.get("/api/models")
  795. async def get_models(user=Depends(get_verified_user)):
  796. models = await get_all_models()
  797. # Filter out filter pipelines
  798. models = [
  799. model
  800. for model in models
  801. if "pipeline" not in model or model["pipeline"].get("type", None) != "filter"
  802. ]
  803. if app.state.config.ENABLE_MODEL_FILTER:
  804. if user.role == "user":
  805. models = list(
  806. filter(
  807. lambda model: model["id"] in app.state.config.MODEL_FILTER_LIST,
  808. models,
  809. )
  810. )
  811. return {"data": models}
  812. return {"data": models}
  813. @app.post("/api/chat/completions")
  814. async def generate_chat_completions(form_data: dict, user=Depends(get_verified_user)):
  815. model_id = form_data["model"]
  816. if model_id not in app.state.MODELS:
  817. raise HTTPException(
  818. status_code=status.HTTP_404_NOT_FOUND,
  819. detail="Model not found",
  820. )
  821. if app.state.config.ENABLE_MODEL_FILTER:
  822. if user.role == "user" and model_id not in app.state.config.MODEL_FILTER_LIST:
  823. raise HTTPException(
  824. status_code=status.HTTP_403_FORBIDDEN,
  825. detail="Model not found",
  826. )
  827. model = app.state.MODELS[model_id]
  828. if model.get("pipe"):
  829. return await generate_function_chat_completion(form_data, user=user)
  830. if model["owned_by"] == "ollama":
  831. return await generate_ollama_chat_completion(form_data, user=user)
  832. else:
  833. return await generate_openai_chat_completion(form_data, user=user)
  834. @app.post("/api/chat/completed")
  835. async def chat_completed(form_data: dict, user=Depends(get_verified_user)):
  836. data = form_data
  837. model_id = data["model"]
  838. if model_id not in app.state.MODELS:
  839. raise HTTPException(
  840. status_code=status.HTTP_404_NOT_FOUND,
  841. detail="Model not found",
  842. )
  843. model = app.state.MODELS[model_id]
  844. sorted_filters = get_sorted_filters(model_id)
  845. if "pipeline" in model:
  846. sorted_filters = [model] + sorted_filters
  847. for filter in sorted_filters:
  848. r = None
  849. try:
  850. urlIdx = filter["urlIdx"]
  851. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  852. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  853. if key != "":
  854. headers = {"Authorization": f"Bearer {key}"}
  855. r = requests.post(
  856. f"{url}/{filter['id']}/filter/outlet",
  857. headers=headers,
  858. json={
  859. "user": {
  860. "id": user.id,
  861. "name": user.name,
  862. "email": user.email,
  863. "role": user.role,
  864. },
  865. "body": data,
  866. },
  867. )
  868. r.raise_for_status()
  869. data = r.json()
  870. except Exception as e:
  871. # Handle connection error here
  872. print(f"Connection error: {e}")
  873. if r is not None:
  874. try:
  875. res = r.json()
  876. if "detail" in res:
  877. return JSONResponse(
  878. status_code=r.status_code,
  879. content=res,
  880. )
  881. except Exception:
  882. pass
  883. else:
  884. pass
  885. __event_emitter__ = get_event_emitter(
  886. {
  887. "chat_id": data["chat_id"],
  888. "message_id": data["id"],
  889. "session_id": data["session_id"],
  890. }
  891. )
  892. __event_call__ = get_event_call(
  893. {
  894. "chat_id": data["chat_id"],
  895. "message_id": data["id"],
  896. "session_id": data["session_id"],
  897. }
  898. )
  899. def get_priority(function_id):
  900. function = Functions.get_function_by_id(function_id)
  901. if function is not None and hasattr(function, "valves"):
  902. # TODO: Fix FunctionModel to include vavles
  903. return (function.valves if function.valves else {}).get("priority", 0)
  904. return 0
  905. filter_ids = [function.id for function in Functions.get_global_filter_functions()]
  906. if "info" in model and "meta" in model["info"]:
  907. filter_ids.extend(model["info"]["meta"].get("filterIds", []))
  908. filter_ids = list(set(filter_ids))
  909. enabled_filter_ids = [
  910. function.id
  911. for function in Functions.get_functions_by_type("filter", active_only=True)
  912. ]
  913. filter_ids = [
  914. filter_id for filter_id in filter_ids if filter_id in enabled_filter_ids
  915. ]
  916. # Sort filter_ids by priority, using the get_priority function
  917. filter_ids.sort(key=get_priority)
  918. for filter_id in filter_ids:
  919. filter = Functions.get_function_by_id(filter_id)
  920. if not filter:
  921. continue
  922. if filter_id in webui_app.state.FUNCTIONS:
  923. function_module = webui_app.state.FUNCTIONS[filter_id]
  924. else:
  925. function_module, _, _ = load_function_module_by_id(filter_id)
  926. webui_app.state.FUNCTIONS[filter_id] = function_module
  927. if hasattr(function_module, "valves") and hasattr(function_module, "Valves"):
  928. valves = Functions.get_function_valves_by_id(filter_id)
  929. function_module.valves = function_module.Valves(
  930. **(valves if valves else {})
  931. )
  932. if not hasattr(function_module, "outlet"):
  933. continue
  934. try:
  935. outlet = function_module.outlet
  936. # Get the signature of the function
  937. sig = inspect.signature(outlet)
  938. params = {"body": data}
  939. # Extra parameters to be passed to the function
  940. extra_params = {
  941. "__model__": model,
  942. "__id__": filter_id,
  943. "__event_emitter__": __event_emitter__,
  944. "__event_call__": __event_call__,
  945. }
  946. # Add extra params in contained in function signature
  947. for key, value in extra_params.items():
  948. if key in sig.parameters:
  949. params[key] = value
  950. if "__user__" in sig.parameters:
  951. __user__ = {
  952. "id": user.id,
  953. "email": user.email,
  954. "name": user.name,
  955. "role": user.role,
  956. }
  957. try:
  958. if hasattr(function_module, "UserValves"):
  959. __user__["valves"] = function_module.UserValves(
  960. **Functions.get_user_valves_by_id_and_user_id(
  961. filter_id, user.id
  962. )
  963. )
  964. except Exception as e:
  965. print(e)
  966. params = {**params, "__user__": __user__}
  967. if inspect.iscoroutinefunction(outlet):
  968. data = await outlet(**params)
  969. else:
  970. data = outlet(**params)
  971. except Exception as e:
  972. print(f"Error: {e}")
  973. return JSONResponse(
  974. status_code=status.HTTP_400_BAD_REQUEST,
  975. content={"detail": str(e)},
  976. )
  977. return data
  978. @app.post("/api/chat/actions/{action_id}")
  979. async def chat_action(action_id: str, form_data: dict, user=Depends(get_verified_user)):
  980. if "." in action_id:
  981. action_id, sub_action_id = action_id.split(".")
  982. else:
  983. sub_action_id = None
  984. action = Functions.get_function_by_id(action_id)
  985. if not action:
  986. raise HTTPException(
  987. status_code=status.HTTP_404_NOT_FOUND,
  988. detail="Action not found",
  989. )
  990. data = form_data
  991. model_id = data["model"]
  992. if model_id not in app.state.MODELS:
  993. raise HTTPException(
  994. status_code=status.HTTP_404_NOT_FOUND,
  995. detail="Model not found",
  996. )
  997. model = app.state.MODELS[model_id]
  998. __event_emitter__ = get_event_emitter(
  999. {
  1000. "chat_id": data["chat_id"],
  1001. "message_id": data["id"],
  1002. "session_id": data["session_id"],
  1003. }
  1004. )
  1005. __event_call__ = get_event_call(
  1006. {
  1007. "chat_id": data["chat_id"],
  1008. "message_id": data["id"],
  1009. "session_id": data["session_id"],
  1010. }
  1011. )
  1012. if action_id in webui_app.state.FUNCTIONS:
  1013. function_module = webui_app.state.FUNCTIONS[action_id]
  1014. else:
  1015. function_module, _, _ = load_function_module_by_id(action_id)
  1016. webui_app.state.FUNCTIONS[action_id] = function_module
  1017. if hasattr(function_module, "valves") and hasattr(function_module, "Valves"):
  1018. valves = Functions.get_function_valves_by_id(action_id)
  1019. function_module.valves = function_module.Valves(**(valves if valves else {}))
  1020. if hasattr(function_module, "action"):
  1021. try:
  1022. action = function_module.action
  1023. # Get the signature of the function
  1024. sig = inspect.signature(action)
  1025. params = {"body": data}
  1026. # Extra parameters to be passed to the function
  1027. extra_params = {
  1028. "__model__": model,
  1029. "__id__": sub_action_id if sub_action_id is not None else action_id,
  1030. "__event_emitter__": __event_emitter__,
  1031. "__event_call__": __event_call__,
  1032. }
  1033. # Add extra params in contained in function signature
  1034. for key, value in extra_params.items():
  1035. if key in sig.parameters:
  1036. params[key] = value
  1037. if "__user__" in sig.parameters:
  1038. __user__ = {
  1039. "id": user.id,
  1040. "email": user.email,
  1041. "name": user.name,
  1042. "role": user.role,
  1043. }
  1044. try:
  1045. if hasattr(function_module, "UserValves"):
  1046. __user__["valves"] = function_module.UserValves(
  1047. **Functions.get_user_valves_by_id_and_user_id(
  1048. action_id, user.id
  1049. )
  1050. )
  1051. except Exception as e:
  1052. print(e)
  1053. params = {**params, "__user__": __user__}
  1054. if inspect.iscoroutinefunction(action):
  1055. data = await action(**params)
  1056. else:
  1057. data = action(**params)
  1058. except Exception as e:
  1059. print(f"Error: {e}")
  1060. return JSONResponse(
  1061. status_code=status.HTTP_400_BAD_REQUEST,
  1062. content={"detail": str(e)},
  1063. )
  1064. return data
  1065. ##################################
  1066. #
  1067. # Task Endpoints
  1068. #
  1069. ##################################
  1070. # TODO: Refactor task API endpoints below into a separate file
  1071. @app.get("/api/task/config")
  1072. async def get_task_config(user=Depends(get_verified_user)):
  1073. return {
  1074. "TASK_MODEL": app.state.config.TASK_MODEL,
  1075. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  1076. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  1077. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  1078. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  1079. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  1080. }
  1081. class TaskConfigForm(BaseModel):
  1082. TASK_MODEL: Optional[str]
  1083. TASK_MODEL_EXTERNAL: Optional[str]
  1084. TITLE_GENERATION_PROMPT_TEMPLATE: str
  1085. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE: str
  1086. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD: int
  1087. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: str
  1088. @app.post("/api/task/config/update")
  1089. async def update_task_config(form_data: TaskConfigForm, user=Depends(get_admin_user)):
  1090. app.state.config.TASK_MODEL = form_data.TASK_MODEL
  1091. app.state.config.TASK_MODEL_EXTERNAL = form_data.TASK_MODEL_EXTERNAL
  1092. app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = (
  1093. form_data.TITLE_GENERATION_PROMPT_TEMPLATE
  1094. )
  1095. app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE = (
  1096. form_data.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  1097. )
  1098. app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD = (
  1099. form_data.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD
  1100. )
  1101. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  1102. form_data.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  1103. )
  1104. return {
  1105. "TASK_MODEL": app.state.config.TASK_MODEL,
  1106. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  1107. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  1108. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  1109. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  1110. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  1111. }
  1112. @app.post("/api/task/title/completions")
  1113. async def generate_title(form_data: dict, user=Depends(get_verified_user)):
  1114. print("generate_title")
  1115. model_id = form_data["model"]
  1116. if model_id not in app.state.MODELS:
  1117. raise HTTPException(
  1118. status_code=status.HTTP_404_NOT_FOUND,
  1119. detail="Model not found",
  1120. )
  1121. # Check if the user has a custom task model
  1122. # If the user has a custom task model, use that model
  1123. model_id = get_task_model_id(model_id)
  1124. print(model_id)
  1125. template = app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE
  1126. content = title_generation_template(
  1127. template,
  1128. form_data["prompt"],
  1129. {
  1130. "name": user.name,
  1131. "location": user.info.get("location") if user.info else None,
  1132. },
  1133. )
  1134. payload = {
  1135. "model": model_id,
  1136. "messages": [{"role": "user", "content": content}],
  1137. "stream": False,
  1138. "max_tokens": 50,
  1139. "chat_id": form_data.get("chat_id", None),
  1140. "metadata": {"task": str(TASKS.TITLE_GENERATION)},
  1141. }
  1142. log.debug(payload)
  1143. try:
  1144. payload = filter_pipeline(payload, user)
  1145. except Exception as e:
  1146. if len(e.args) > 1:
  1147. return JSONResponse(
  1148. status_code=e.args[0],
  1149. content={"detail": e.args[1]},
  1150. )
  1151. else:
  1152. return JSONResponse(
  1153. status_code=status.HTTP_400_BAD_REQUEST,
  1154. content={"detail": str(e)},
  1155. )
  1156. if "chat_id" in payload:
  1157. del payload["chat_id"]
  1158. return await generate_chat_completions(form_data=payload, user=user)
  1159. @app.post("/api/task/query/completions")
  1160. async def generate_search_query(form_data: dict, user=Depends(get_verified_user)):
  1161. print("generate_search_query")
  1162. if len(form_data["prompt"]) < app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD:
  1163. raise HTTPException(
  1164. status_code=status.HTTP_400_BAD_REQUEST,
  1165. detail=f"Skip search query generation for short prompts (< {app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD} characters)",
  1166. )
  1167. model_id = form_data["model"]
  1168. if model_id not in app.state.MODELS:
  1169. raise HTTPException(
  1170. status_code=status.HTTP_404_NOT_FOUND,
  1171. detail="Model not found",
  1172. )
  1173. # Check if the user has a custom task model
  1174. # If the user has a custom task model, use that model
  1175. model_id = get_task_model_id(model_id)
  1176. print(model_id)
  1177. template = app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  1178. content = search_query_generation_template(
  1179. template, form_data["prompt"], {"name": user.name}
  1180. )
  1181. payload = {
  1182. "model": model_id,
  1183. "messages": [{"role": "user", "content": content}],
  1184. "stream": False,
  1185. "max_tokens": 30,
  1186. "metadata": {"task": str(TASKS.QUERY_GENERATION)},
  1187. }
  1188. print(payload)
  1189. try:
  1190. payload = filter_pipeline(payload, user)
  1191. except Exception as e:
  1192. if len(e.args) > 1:
  1193. return JSONResponse(
  1194. status_code=e.args[0],
  1195. content={"detail": e.args[1]},
  1196. )
  1197. else:
  1198. return JSONResponse(
  1199. status_code=status.HTTP_400_BAD_REQUEST,
  1200. content={"detail": str(e)},
  1201. )
  1202. if "chat_id" in payload:
  1203. del payload["chat_id"]
  1204. return await generate_chat_completions(form_data=payload, user=user)
  1205. @app.post("/api/task/emoji/completions")
  1206. async def generate_emoji(form_data: dict, user=Depends(get_verified_user)):
  1207. print("generate_emoji")
  1208. model_id = form_data["model"]
  1209. if model_id not in app.state.MODELS:
  1210. raise HTTPException(
  1211. status_code=status.HTTP_404_NOT_FOUND,
  1212. detail="Model not found",
  1213. )
  1214. # Check if the user has a custom task model
  1215. # If the user has a custom task model, use that model
  1216. model_id = get_task_model_id(model_id)
  1217. print(model_id)
  1218. template = '''
  1219. Your task is to reflect the speaker's likely facial expression through a fitting emoji. Interpret emotions from the message and reflect their facial expression using fitting, diverse emojis (e.g., 😊, 😢, 😡, 😱).
  1220. Message: """{{prompt}}"""
  1221. '''
  1222. content = title_generation_template(
  1223. template,
  1224. form_data["prompt"],
  1225. {
  1226. "name": user.name,
  1227. "location": user.info.get("location") if user.info else None,
  1228. },
  1229. )
  1230. payload = {
  1231. "model": model_id,
  1232. "messages": [{"role": "user", "content": content}],
  1233. "stream": False,
  1234. "max_tokens": 4,
  1235. "chat_id": form_data.get("chat_id", None),
  1236. "metadata": {"task": str(TASKS.EMOJI_GENERATION)},
  1237. }
  1238. log.debug(payload)
  1239. try:
  1240. payload = filter_pipeline(payload, user)
  1241. except Exception as e:
  1242. if len(e.args) > 1:
  1243. return JSONResponse(
  1244. status_code=e.args[0],
  1245. content={"detail": e.args[1]},
  1246. )
  1247. else:
  1248. return JSONResponse(
  1249. status_code=status.HTTP_400_BAD_REQUEST,
  1250. content={"detail": str(e)},
  1251. )
  1252. if "chat_id" in payload:
  1253. del payload["chat_id"]
  1254. return await generate_chat_completions(form_data=payload, user=user)
  1255. @app.post("/api/task/moa/completions")
  1256. async def generate_moa_response(form_data: dict, user=Depends(get_verified_user)):
  1257. print("generate_moa_response")
  1258. model_id = form_data["model"]
  1259. if model_id not in app.state.MODELS:
  1260. raise HTTPException(
  1261. status_code=status.HTTP_404_NOT_FOUND,
  1262. detail="Model not found",
  1263. )
  1264. # Check if the user has a custom task model
  1265. # If the user has a custom task model, use that model
  1266. model_id = get_task_model_id(model_id)
  1267. print(model_id)
  1268. template = """You have been provided with a set of responses from various models to the latest user query: "{{prompt}}"
  1269. Your task is to synthesize these responses into a single, high-quality response. It is crucial to critically evaluate the information provided in these responses, recognizing that some of it may be biased or incorrect. Your response should not simply replicate the given answers but should offer a refined, accurate, and comprehensive reply to the instruction. Ensure your response is well-structured, coherent, and adheres to the highest standards of accuracy and reliability.
  1270. Responses from models: {{responses}}"""
  1271. content = moa_response_generation_template(
  1272. template,
  1273. form_data["prompt"],
  1274. form_data["responses"],
  1275. )
  1276. payload = {
  1277. "model": model_id,
  1278. "messages": [{"role": "user", "content": content}],
  1279. "stream": form_data.get("stream", False),
  1280. "chat_id": form_data.get("chat_id", None),
  1281. "metadata": {"task": str(TASKS.MOA_RESPONSE_GENERATION)},
  1282. }
  1283. log.debug(payload)
  1284. try:
  1285. payload = filter_pipeline(payload, user)
  1286. except Exception as e:
  1287. if len(e.args) > 1:
  1288. return JSONResponse(
  1289. status_code=e.args[0],
  1290. content={"detail": e.args[1]},
  1291. )
  1292. else:
  1293. return JSONResponse(
  1294. status_code=status.HTTP_400_BAD_REQUEST,
  1295. content={"detail": str(e)},
  1296. )
  1297. if "chat_id" in payload:
  1298. del payload["chat_id"]
  1299. return await generate_chat_completions(form_data=payload, user=user)
  1300. ##################################
  1301. #
  1302. # Pipelines Endpoints
  1303. #
  1304. ##################################
  1305. # TODO: Refactor pipelines API endpoints below into a separate file
  1306. @app.get("/api/pipelines/list")
  1307. async def get_pipelines_list(user=Depends(get_admin_user)):
  1308. responses = await get_openai_models(raw=True)
  1309. print(responses)
  1310. urlIdxs = [
  1311. idx
  1312. for idx, response in enumerate(responses)
  1313. if response is not None and "pipelines" in response
  1314. ]
  1315. return {
  1316. "data": [
  1317. {
  1318. "url": openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx],
  1319. "idx": urlIdx,
  1320. }
  1321. for urlIdx in urlIdxs
  1322. ]
  1323. }
  1324. @app.post("/api/pipelines/upload")
  1325. async def upload_pipeline(
  1326. urlIdx: int = Form(...), file: UploadFile = File(...), user=Depends(get_admin_user)
  1327. ):
  1328. print("upload_pipeline", urlIdx, file.filename)
  1329. # Check if the uploaded file is a python file
  1330. if not (file.filename and file.filename.endswith(".py")):
  1331. raise HTTPException(
  1332. status_code=status.HTTP_400_BAD_REQUEST,
  1333. detail="Only Python (.py) files are allowed.",
  1334. )
  1335. upload_folder = f"{CACHE_DIR}/pipelines"
  1336. os.makedirs(upload_folder, exist_ok=True)
  1337. file_path = os.path.join(upload_folder, file.filename)
  1338. r = None
  1339. try:
  1340. # Save the uploaded file
  1341. with open(file_path, "wb") as buffer:
  1342. shutil.copyfileobj(file.file, buffer)
  1343. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1344. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1345. headers = {"Authorization": f"Bearer {key}"}
  1346. with open(file_path, "rb") as f:
  1347. files = {"file": f}
  1348. r = requests.post(f"{url}/pipelines/upload", headers=headers, files=files)
  1349. r.raise_for_status()
  1350. data = r.json()
  1351. return {**data}
  1352. except Exception as e:
  1353. # Handle connection error here
  1354. print(f"Connection error: {e}")
  1355. detail = "Pipeline not found"
  1356. status_code = status.HTTP_404_NOT_FOUND
  1357. if r is not None:
  1358. status_code = r.status_code
  1359. try:
  1360. res = r.json()
  1361. if "detail" in res:
  1362. detail = res["detail"]
  1363. except Exception:
  1364. pass
  1365. raise HTTPException(
  1366. status_code=status_code,
  1367. detail=detail,
  1368. )
  1369. finally:
  1370. # Ensure the file is deleted after the upload is completed or on failure
  1371. if os.path.exists(file_path):
  1372. os.remove(file_path)
  1373. class AddPipelineForm(BaseModel):
  1374. url: str
  1375. urlIdx: int
  1376. @app.post("/api/pipelines/add")
  1377. async def add_pipeline(form_data: AddPipelineForm, user=Depends(get_admin_user)):
  1378. r = None
  1379. try:
  1380. urlIdx = form_data.urlIdx
  1381. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1382. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1383. headers = {"Authorization": f"Bearer {key}"}
  1384. r = requests.post(
  1385. f"{url}/pipelines/add", headers=headers, json={"url": form_data.url}
  1386. )
  1387. r.raise_for_status()
  1388. data = r.json()
  1389. return {**data}
  1390. except Exception as e:
  1391. # Handle connection error here
  1392. print(f"Connection error: {e}")
  1393. detail = "Pipeline not found"
  1394. if r is not None:
  1395. try:
  1396. res = r.json()
  1397. if "detail" in res:
  1398. detail = res["detail"]
  1399. except Exception:
  1400. pass
  1401. raise HTTPException(
  1402. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1403. detail=detail,
  1404. )
  1405. class DeletePipelineForm(BaseModel):
  1406. id: str
  1407. urlIdx: int
  1408. @app.delete("/api/pipelines/delete")
  1409. async def delete_pipeline(form_data: DeletePipelineForm, user=Depends(get_admin_user)):
  1410. r = None
  1411. try:
  1412. urlIdx = form_data.urlIdx
  1413. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1414. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1415. headers = {"Authorization": f"Bearer {key}"}
  1416. r = requests.delete(
  1417. f"{url}/pipelines/delete", headers=headers, json={"id": form_data.id}
  1418. )
  1419. r.raise_for_status()
  1420. data = r.json()
  1421. return {**data}
  1422. except Exception as e:
  1423. # Handle connection error here
  1424. print(f"Connection error: {e}")
  1425. detail = "Pipeline not found"
  1426. if r is not None:
  1427. try:
  1428. res = r.json()
  1429. if "detail" in res:
  1430. detail = res["detail"]
  1431. except Exception:
  1432. pass
  1433. raise HTTPException(
  1434. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1435. detail=detail,
  1436. )
  1437. @app.get("/api/pipelines")
  1438. async def get_pipelines(urlIdx: Optional[int] = None, user=Depends(get_admin_user)):
  1439. r = None
  1440. try:
  1441. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1442. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1443. headers = {"Authorization": f"Bearer {key}"}
  1444. r = requests.get(f"{url}/pipelines", headers=headers)
  1445. r.raise_for_status()
  1446. data = r.json()
  1447. return {**data}
  1448. except Exception as e:
  1449. # Handle connection error here
  1450. print(f"Connection error: {e}")
  1451. detail = "Pipeline not found"
  1452. if r is not None:
  1453. try:
  1454. res = r.json()
  1455. if "detail" in res:
  1456. detail = res["detail"]
  1457. except Exception:
  1458. pass
  1459. raise HTTPException(
  1460. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1461. detail=detail,
  1462. )
  1463. @app.get("/api/pipelines/{pipeline_id}/valves")
  1464. async def get_pipeline_valves(
  1465. urlIdx: Optional[int],
  1466. pipeline_id: str,
  1467. user=Depends(get_admin_user),
  1468. ):
  1469. r = None
  1470. try:
  1471. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1472. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1473. headers = {"Authorization": f"Bearer {key}"}
  1474. r = requests.get(f"{url}/{pipeline_id}/valves", headers=headers)
  1475. r.raise_for_status()
  1476. data = r.json()
  1477. return {**data}
  1478. except Exception as e:
  1479. # Handle connection error here
  1480. print(f"Connection error: {e}")
  1481. detail = "Pipeline not found"
  1482. if r is not None:
  1483. try:
  1484. res = r.json()
  1485. if "detail" in res:
  1486. detail = res["detail"]
  1487. except Exception:
  1488. pass
  1489. raise HTTPException(
  1490. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1491. detail=detail,
  1492. )
  1493. @app.get("/api/pipelines/{pipeline_id}/valves/spec")
  1494. async def get_pipeline_valves_spec(
  1495. urlIdx: Optional[int],
  1496. pipeline_id: str,
  1497. user=Depends(get_admin_user),
  1498. ):
  1499. r = None
  1500. try:
  1501. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1502. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1503. headers = {"Authorization": f"Bearer {key}"}
  1504. r = requests.get(f"{url}/{pipeline_id}/valves/spec", headers=headers)
  1505. r.raise_for_status()
  1506. data = r.json()
  1507. return {**data}
  1508. except Exception as e:
  1509. # Handle connection error here
  1510. print(f"Connection error: {e}")
  1511. detail = "Pipeline not found"
  1512. if r is not None:
  1513. try:
  1514. res = r.json()
  1515. if "detail" in res:
  1516. detail = res["detail"]
  1517. except Exception:
  1518. pass
  1519. raise HTTPException(
  1520. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1521. detail=detail,
  1522. )
  1523. @app.post("/api/pipelines/{pipeline_id}/valves/update")
  1524. async def update_pipeline_valves(
  1525. urlIdx: Optional[int],
  1526. pipeline_id: str,
  1527. form_data: dict,
  1528. user=Depends(get_admin_user),
  1529. ):
  1530. r = None
  1531. try:
  1532. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1533. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1534. headers = {"Authorization": f"Bearer {key}"}
  1535. r = requests.post(
  1536. f"{url}/{pipeline_id}/valves/update",
  1537. headers=headers,
  1538. json={**form_data},
  1539. )
  1540. r.raise_for_status()
  1541. data = r.json()
  1542. return {**data}
  1543. except Exception as e:
  1544. # Handle connection error here
  1545. print(f"Connection error: {e}")
  1546. detail = "Pipeline not found"
  1547. if r is not None:
  1548. try:
  1549. res = r.json()
  1550. if "detail" in res:
  1551. detail = res["detail"]
  1552. except Exception:
  1553. pass
  1554. raise HTTPException(
  1555. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1556. detail=detail,
  1557. )
  1558. ##################################
  1559. #
  1560. # Config Endpoints
  1561. #
  1562. ##################################
  1563. @app.get("/api/config")
  1564. async def get_app_config(request: Request):
  1565. user = None
  1566. if "token" in request.cookies:
  1567. token = request.cookies.get("token")
  1568. data = decode_token(token)
  1569. if data is not None and "id" in data:
  1570. user = Users.get_user_by_id(data["id"])
  1571. return {
  1572. "status": True,
  1573. "name": WEBUI_NAME,
  1574. "version": VERSION,
  1575. "default_locale": str(DEFAULT_LOCALE),
  1576. "oauth": {
  1577. "providers": {
  1578. name: config.get("name", name)
  1579. for name, config in OAUTH_PROVIDERS.items()
  1580. }
  1581. },
  1582. "features": {
  1583. "auth": WEBUI_AUTH,
  1584. "auth_trusted_header": bool(webui_app.state.AUTH_TRUSTED_EMAIL_HEADER),
  1585. "enable_signup": webui_app.state.config.ENABLE_SIGNUP,
  1586. "enable_login_form": webui_app.state.config.ENABLE_LOGIN_FORM,
  1587. **(
  1588. {
  1589. "enable_web_search": rag_app.state.config.ENABLE_RAG_WEB_SEARCH,
  1590. "enable_image_generation": images_app.state.config.ENABLED,
  1591. "enable_community_sharing": webui_app.state.config.ENABLE_COMMUNITY_SHARING,
  1592. "enable_message_rating": webui_app.state.config.ENABLE_MESSAGE_RATING,
  1593. "enable_admin_export": ENABLE_ADMIN_EXPORT,
  1594. "enable_admin_chat_access": ENABLE_ADMIN_CHAT_ACCESS,
  1595. }
  1596. if user is not None
  1597. else {}
  1598. ),
  1599. },
  1600. **(
  1601. {
  1602. "default_models": webui_app.state.config.DEFAULT_MODELS,
  1603. "default_prompt_suggestions": webui_app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
  1604. "audio": {
  1605. "tts": {
  1606. "engine": audio_app.state.config.TTS_ENGINE,
  1607. "voice": audio_app.state.config.TTS_VOICE,
  1608. "split_on": audio_app.state.config.TTS_SPLIT_ON,
  1609. },
  1610. "stt": {
  1611. "engine": audio_app.state.config.STT_ENGINE,
  1612. },
  1613. },
  1614. "file": {
  1615. "max_size": rag_app.state.config.FILE_MAX_SIZE,
  1616. "max_count": rag_app.state.config.FILE_MAX_COUNT,
  1617. },
  1618. "permissions": {**webui_app.state.config.USER_PERMISSIONS},
  1619. }
  1620. if user is not None
  1621. else {}
  1622. ),
  1623. }
  1624. @app.get("/api/config/model/filter")
  1625. async def get_model_filter_config(user=Depends(get_admin_user)):
  1626. return {
  1627. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1628. "models": app.state.config.MODEL_FILTER_LIST,
  1629. }
  1630. class ModelFilterConfigForm(BaseModel):
  1631. enabled: bool
  1632. models: list[str]
  1633. @app.post("/api/config/model/filter")
  1634. async def update_model_filter_config(
  1635. form_data: ModelFilterConfigForm, user=Depends(get_admin_user)
  1636. ):
  1637. app.state.config.ENABLE_MODEL_FILTER = form_data.enabled
  1638. app.state.config.MODEL_FILTER_LIST = form_data.models
  1639. return {
  1640. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1641. "models": app.state.config.MODEL_FILTER_LIST,
  1642. }
  1643. # TODO: webhook endpoint should be under config endpoints
  1644. @app.get("/api/webhook")
  1645. async def get_webhook_url(user=Depends(get_admin_user)):
  1646. return {
  1647. "url": app.state.config.WEBHOOK_URL,
  1648. }
  1649. class UrlForm(BaseModel):
  1650. url: str
  1651. @app.post("/api/webhook")
  1652. async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)):
  1653. app.state.config.WEBHOOK_URL = form_data.url
  1654. webui_app.state.WEBHOOK_URL = app.state.config.WEBHOOK_URL
  1655. return {"url": app.state.config.WEBHOOK_URL}
  1656. @app.get("/api/version")
  1657. async def get_app_version():
  1658. return {
  1659. "version": VERSION,
  1660. }
  1661. @app.get("/api/changelog")
  1662. async def get_app_changelog():
  1663. return {key: CHANGELOG[key] for idx, key in enumerate(CHANGELOG) if idx < 5}
  1664. @app.get("/api/version/updates")
  1665. async def get_app_latest_release_version():
  1666. try:
  1667. async with aiohttp.ClientSession(trust_env=True) as session:
  1668. async with session.get(
  1669. "https://api.github.com/repos/open-webui/open-webui/releases/latest"
  1670. ) as response:
  1671. response.raise_for_status()
  1672. data = await response.json()
  1673. latest_version = data["tag_name"]
  1674. return {"current": VERSION, "latest": latest_version[1:]}
  1675. except aiohttp.ClientError:
  1676. raise HTTPException(
  1677. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  1678. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  1679. )
  1680. ############################
  1681. # OAuth Login & Callback
  1682. ############################
  1683. oauth = OAuth()
  1684. for provider_name, provider_config in OAUTH_PROVIDERS.items():
  1685. oauth.register(
  1686. name=provider_name,
  1687. client_id=provider_config["client_id"],
  1688. client_secret=provider_config["client_secret"],
  1689. server_metadata_url=provider_config["server_metadata_url"],
  1690. client_kwargs={
  1691. "scope": provider_config["scope"],
  1692. },
  1693. redirect_uri=provider_config["redirect_uri"],
  1694. )
  1695. # SessionMiddleware is used by authlib for oauth
  1696. if len(OAUTH_PROVIDERS) > 0:
  1697. app.add_middleware(
  1698. SessionMiddleware,
  1699. secret_key=WEBUI_SECRET_KEY,
  1700. session_cookie="oui-session",
  1701. same_site=WEBUI_SESSION_COOKIE_SAME_SITE,
  1702. https_only=WEBUI_SESSION_COOKIE_SECURE,
  1703. )
  1704. @app.get("/oauth/{provider}/login")
  1705. async def oauth_login(provider: str, request: Request):
  1706. if provider not in OAUTH_PROVIDERS:
  1707. raise HTTPException(404)
  1708. # If the provider has a custom redirect URL, use that, otherwise automatically generate one
  1709. redirect_uri = OAUTH_PROVIDERS[provider].get("redirect_uri") or request.url_for(
  1710. "oauth_callback", provider=provider
  1711. )
  1712. client = oauth.create_client(provider)
  1713. if client is None:
  1714. raise HTTPException(404)
  1715. return await client.authorize_redirect(request, redirect_uri)
  1716. # OAuth login logic is as follows:
  1717. # 1. Attempt to find a user with matching subject ID, tied to the provider
  1718. # 2. If OAUTH_MERGE_ACCOUNTS_BY_EMAIL is true, find a user with the email address provided via OAuth
  1719. # - This is considered insecure in general, as OAuth providers do not always verify email addresses
  1720. # 3. If there is no user, and ENABLE_OAUTH_SIGNUP is true, create a user
  1721. # - Email addresses are considered unique, so we fail registration if the email address is alreayd taken
  1722. @app.get("/oauth/{provider}/callback")
  1723. async def oauth_callback(provider: str, request: Request, response: Response):
  1724. if provider not in OAUTH_PROVIDERS:
  1725. raise HTTPException(404)
  1726. client = oauth.create_client(provider)
  1727. try:
  1728. token = await client.authorize_access_token(request)
  1729. except Exception as e:
  1730. log.warning(f"OAuth callback error: {e}")
  1731. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  1732. user_data: UserInfo = token["userinfo"]
  1733. sub = user_data.get("sub")
  1734. if not sub:
  1735. log.warning(f"OAuth callback failed, sub is missing: {user_data}")
  1736. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  1737. provider_sub = f"{provider}@{sub}"
  1738. email_claim = webui_app.state.config.OAUTH_EMAIL_CLAIM
  1739. email = user_data.get(email_claim, "").lower()
  1740. # We currently mandate that email addresses are provided
  1741. if not email:
  1742. log.warning(f"OAuth callback failed, email is missing: {user_data}")
  1743. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  1744. # Check if the user exists
  1745. user = Users.get_user_by_oauth_sub(provider_sub)
  1746. if not user:
  1747. # If the user does not exist, check if merging is enabled
  1748. if OAUTH_MERGE_ACCOUNTS_BY_EMAIL.value:
  1749. # Check if the user exists by email
  1750. user = Users.get_user_by_email(email)
  1751. if user:
  1752. # Update the user with the new oauth sub
  1753. Users.update_user_oauth_sub_by_id(user.id, provider_sub)
  1754. if not user:
  1755. # If the user does not exist, check if signups are enabled
  1756. if ENABLE_OAUTH_SIGNUP.value:
  1757. # Check if an existing user with the same email already exists
  1758. existing_user = Users.get_user_by_email(user_data.get("email", "").lower())
  1759. if existing_user:
  1760. raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN)
  1761. picture_claim = webui_app.state.config.OAUTH_PICTURE_CLAIM
  1762. picture_url = user_data.get(picture_claim, "")
  1763. if picture_url:
  1764. # Download the profile image into a base64 string
  1765. try:
  1766. async with aiohttp.ClientSession() as session:
  1767. async with session.get(picture_url) as resp:
  1768. picture = await resp.read()
  1769. base64_encoded_picture = base64.b64encode(picture).decode(
  1770. "utf-8"
  1771. )
  1772. guessed_mime_type = mimetypes.guess_type(picture_url)[0]
  1773. if guessed_mime_type is None:
  1774. # assume JPG, browsers are tolerant enough of image formats
  1775. guessed_mime_type = "image/jpeg"
  1776. picture_url = f"data:{guessed_mime_type};base64,{base64_encoded_picture}"
  1777. except Exception as e:
  1778. log.error(f"Error downloading profile image '{picture_url}': {e}")
  1779. picture_url = ""
  1780. if not picture_url:
  1781. picture_url = "/user.png"
  1782. username_claim = webui_app.state.config.OAUTH_USERNAME_CLAIM
  1783. role = (
  1784. "admin"
  1785. if Users.get_num_users() == 0
  1786. else webui_app.state.config.DEFAULT_USER_ROLE
  1787. )
  1788. user = Auths.insert_new_auth(
  1789. email=email,
  1790. password=get_password_hash(
  1791. str(uuid.uuid4())
  1792. ), # Random password, not used
  1793. name=user_data.get(username_claim, "User"),
  1794. profile_image_url=picture_url,
  1795. role=role,
  1796. oauth_sub=provider_sub,
  1797. )
  1798. if webui_app.state.config.WEBHOOK_URL:
  1799. post_webhook(
  1800. webui_app.state.config.WEBHOOK_URL,
  1801. WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
  1802. {
  1803. "action": "signup",
  1804. "message": WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
  1805. "user": user.model_dump_json(exclude_none=True),
  1806. },
  1807. )
  1808. else:
  1809. raise HTTPException(
  1810. status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.ACCESS_PROHIBITED
  1811. )
  1812. jwt_token = create_token(
  1813. data={"id": user.id},
  1814. expires_delta=parse_duration(webui_app.state.config.JWT_EXPIRES_IN),
  1815. )
  1816. # Set the cookie token
  1817. response.set_cookie(
  1818. key="token",
  1819. value=jwt_token,
  1820. httponly=True, # Ensures the cookie is not accessible via JavaScript
  1821. )
  1822. # Redirect back to the frontend with the JWT token
  1823. redirect_url = f"{request.base_url}auth#token={jwt_token}"
  1824. return RedirectResponse(url=redirect_url)
  1825. @app.get("/manifest.json")
  1826. async def get_manifest_json():
  1827. return {
  1828. "name": WEBUI_NAME,
  1829. "short_name": WEBUI_NAME,
  1830. "start_url": "/",
  1831. "display": "standalone",
  1832. "background_color": "#343541",
  1833. "orientation": "portrait-primary",
  1834. "icons": [
  1835. {
  1836. "src": "/static/logo.png",
  1837. "type": "image/png",
  1838. "sizes": "500x500",
  1839. "purpose": "any",
  1840. },
  1841. {
  1842. "src": "/static/logo.png",
  1843. "type": "image/png",
  1844. "sizes": "500x500",
  1845. "purpose": "maskable",
  1846. },
  1847. ],
  1848. }
  1849. @app.get("/opensearch.xml")
  1850. async def get_opensearch_xml():
  1851. xml_content = rf"""
  1852. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  1853. <ShortName>{WEBUI_NAME}</ShortName>
  1854. <Description>Search {WEBUI_NAME}</Description>
  1855. <InputEncoding>UTF-8</InputEncoding>
  1856. <Image width="16" height="16" type="image/x-icon">{WEBUI_URL}/static/favicon.png</Image>
  1857. <Url type="text/html" method="get" template="{WEBUI_URL}/?q={"{searchTerms}"}"/>
  1858. <moz:SearchForm>{WEBUI_URL}</moz:SearchForm>
  1859. </OpenSearchDescription>
  1860. """
  1861. return Response(content=xml_content, media_type="application/xml")
  1862. @app.get("/health")
  1863. async def healthcheck():
  1864. return {"status": True}
  1865. @app.get("/health/db")
  1866. async def healthcheck_with_db():
  1867. Session.execute(text("SELECT 1;")).all()
  1868. return {"status": True}
  1869. app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
  1870. app.mount("/cache", StaticFiles(directory=CACHE_DIR), name="cache")
  1871. if os.path.exists(FRONTEND_BUILD_DIR):
  1872. mimetypes.add_type("text/javascript", ".js")
  1873. app.mount(
  1874. "/",
  1875. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  1876. name="spa-static-files",
  1877. )
  1878. else:
  1879. log.warning(
  1880. f"Frontend build directory not found at '{FRONTEND_BUILD_DIR}'. Serving API only."
  1881. )