main.py 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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. import shutil
  13. import os
  14. import uuid
  15. import inspect
  16. import asyncio
  17. from fastapi.concurrency import run_in_threadpool
  18. from fastapi import FastAPI, Request, Depends, status, UploadFile, File, Form
  19. from fastapi.staticfiles import StaticFiles
  20. from fastapi.responses import JSONResponse
  21. from fastapi import HTTPException
  22. from fastapi.middleware.wsgi import WSGIMiddleware
  23. from fastapi.middleware.cors import CORSMiddleware
  24. from starlette.exceptions import HTTPException as StarletteHTTPException
  25. from starlette.middleware.base import BaseHTTPMiddleware
  26. from starlette.responses import StreamingResponse, Response
  27. from apps.socket.main import app as socket_app
  28. from apps.ollama.main import (
  29. app as ollama_app,
  30. OpenAIChatCompletionForm,
  31. get_all_models as get_ollama_models,
  32. generate_openai_chat_completion as generate_ollama_chat_completion,
  33. )
  34. from apps.openai.main import (
  35. app as openai_app,
  36. get_all_models as get_openai_models,
  37. generate_chat_completion as generate_openai_chat_completion,
  38. )
  39. from apps.audio.main import app as audio_app
  40. from apps.images.main import app as images_app
  41. from apps.rag.main import app as rag_app
  42. from apps.webui.main import app as webui_app, get_pipe_models
  43. from pydantic import BaseModel
  44. from typing import List, Optional, Iterator, Generator, Union
  45. from apps.webui.models.models import Models, ModelModel
  46. from apps.webui.models.tools import Tools
  47. from apps.webui.models.functions import Functions
  48. from apps.webui.utils import load_toolkit_module_by_id, load_function_module_by_id
  49. from utils.utils import (
  50. get_admin_user,
  51. get_verified_user,
  52. get_current_user,
  53. get_http_authorization_cred,
  54. )
  55. from utils.task import (
  56. title_generation_template,
  57. search_query_generation_template,
  58. tools_function_calling_generation_template,
  59. )
  60. from utils.misc import (
  61. get_last_user_message,
  62. add_or_update_system_message,
  63. stream_message_template,
  64. )
  65. from apps.rag.utils import get_rag_context, rag_template
  66. from config import (
  67. CONFIG_DATA,
  68. WEBUI_NAME,
  69. WEBUI_URL,
  70. WEBUI_AUTH,
  71. ENV,
  72. VERSION,
  73. CHANGELOG,
  74. FRONTEND_BUILD_DIR,
  75. UPLOAD_DIR,
  76. CACHE_DIR,
  77. STATIC_DIR,
  78. ENABLE_OPENAI_API,
  79. ENABLE_OLLAMA_API,
  80. ENABLE_MODEL_FILTER,
  81. MODEL_FILTER_LIST,
  82. GLOBAL_LOG_LEVEL,
  83. SRC_LOG_LEVELS,
  84. WEBHOOK_URL,
  85. ENABLE_ADMIN_EXPORT,
  86. WEBUI_BUILD_HASH,
  87. TASK_MODEL,
  88. TASK_MODEL_EXTERNAL,
  89. TITLE_GENERATION_PROMPT_TEMPLATE,
  90. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  91. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  92. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  93. SAFE_MODE,
  94. AppConfig,
  95. )
  96. from constants import ERROR_MESSAGES
  97. if SAFE_MODE:
  98. print("SAFE MODE ENABLED")
  99. Functions.deactivate_all_functions()
  100. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  101. log = logging.getLogger(__name__)
  102. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  103. class SPAStaticFiles(StaticFiles):
  104. async def get_response(self, path: str, scope):
  105. try:
  106. return await super().get_response(path, scope)
  107. except (HTTPException, StarletteHTTPException) as ex:
  108. if ex.status_code == 404:
  109. return await super().get_response("index.html", scope)
  110. else:
  111. raise ex
  112. print(
  113. rf"""
  114. ___ __ __ _ _ _ ___
  115. / _ \ _ __ ___ _ __ \ \ / /__| |__ | | | |_ _|
  116. | | | | '_ \ / _ \ '_ \ \ \ /\ / / _ \ '_ \| | | || |
  117. | |_| | |_) | __/ | | | \ V V / __/ |_) | |_| || |
  118. \___/| .__/ \___|_| |_| \_/\_/ \___|_.__/ \___/|___|
  119. |_|
  120. v{VERSION} - building the best open-source AI user interface.
  121. {f"Commit: {WEBUI_BUILD_HASH}" if WEBUI_BUILD_HASH != "dev-build" else ""}
  122. https://github.com/open-webui/open-webui
  123. """
  124. )
  125. @asynccontextmanager
  126. async def lifespan(app: FastAPI):
  127. yield
  128. app = FastAPI(
  129. docs_url="/docs" if ENV == "dev" else None, redoc_url=None, lifespan=lifespan
  130. )
  131. app.state.config = AppConfig()
  132. app.state.config.ENABLE_OPENAI_API = ENABLE_OPENAI_API
  133. app.state.config.ENABLE_OLLAMA_API = ENABLE_OLLAMA_API
  134. app.state.config.ENABLE_MODEL_FILTER = ENABLE_MODEL_FILTER
  135. app.state.config.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  136. app.state.config.WEBHOOK_URL = WEBHOOK_URL
  137. app.state.config.TASK_MODEL = TASK_MODEL
  138. app.state.config.TASK_MODEL_EXTERNAL = TASK_MODEL_EXTERNAL
  139. app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = TITLE_GENERATION_PROMPT_TEMPLATE
  140. app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE = (
  141. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  142. )
  143. app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD = (
  144. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD
  145. )
  146. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  147. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  148. )
  149. app.state.MODELS = {}
  150. origins = ["*"]
  151. ##################################
  152. #
  153. # ChatCompletion Middleware
  154. #
  155. ##################################
  156. async def get_function_call_response(
  157. messages, files, tool_id, template, task_model_id, user
  158. ):
  159. tool = Tools.get_tool_by_id(tool_id)
  160. tools_specs = json.dumps(tool.specs, indent=2)
  161. content = tools_function_calling_generation_template(template, tools_specs)
  162. user_message = get_last_user_message(messages)
  163. prompt = (
  164. "History:\n"
  165. + "\n".join(
  166. [
  167. f"{message['role'].upper()}: \"\"\"{message['content']}\"\"\""
  168. for message in messages[::-1][:4]
  169. ]
  170. )
  171. + f"\nQuery: {user_message}"
  172. )
  173. print(prompt)
  174. payload = {
  175. "model": task_model_id,
  176. "messages": [
  177. {"role": "system", "content": content},
  178. {"role": "user", "content": f"Query: {prompt}"},
  179. ],
  180. "stream": False,
  181. }
  182. try:
  183. payload = filter_pipeline(payload, user)
  184. except Exception as e:
  185. raise e
  186. model = app.state.MODELS[task_model_id]
  187. response = None
  188. try:
  189. if model["owned_by"] == "ollama":
  190. response = await generate_ollama_chat_completion(payload, user=user)
  191. else:
  192. response = await generate_openai_chat_completion(payload, user=user)
  193. content = None
  194. if hasattr(response, "body_iterator"):
  195. async for chunk in response.body_iterator:
  196. data = json.loads(chunk.decode("utf-8"))
  197. content = data["choices"][0]["message"]["content"]
  198. # Cleanup any remaining background tasks if necessary
  199. if response.background is not None:
  200. await response.background()
  201. else:
  202. content = response["choices"][0]["message"]["content"]
  203. # Parse the function response
  204. if content is not None:
  205. print(f"content: {content}")
  206. result = json.loads(content)
  207. print(result)
  208. citation = None
  209. # Call the function
  210. if "name" in result:
  211. if tool_id in webui_app.state.TOOLS:
  212. toolkit_module = webui_app.state.TOOLS[tool_id]
  213. else:
  214. toolkit_module = load_toolkit_module_by_id(tool_id)
  215. webui_app.state.TOOLS[tool_id] = toolkit_module
  216. file_handler = False
  217. # check if toolkit_module has file_handler self variable
  218. if hasattr(toolkit_module, "file_handler"):
  219. file_handler = True
  220. print("file_handler: ", file_handler)
  221. if hasattr(toolkit_module, "valves") and hasattr(
  222. toolkit_module, "Valves"
  223. ):
  224. valves = Tools.get_tool_valves_by_id(tool_id)
  225. toolkit_module.valves = toolkit_module.Valves(
  226. **(valves if valves else {})
  227. )
  228. function = getattr(toolkit_module, result["name"])
  229. function_result = None
  230. try:
  231. # Get the signature of the function
  232. sig = inspect.signature(function)
  233. params = result["parameters"]
  234. if "__user__" in sig.parameters:
  235. # Call the function with the '__user__' parameter included
  236. __user__ = {
  237. "id": user.id,
  238. "email": user.email,
  239. "name": user.name,
  240. "role": user.role,
  241. }
  242. try:
  243. if hasattr(toolkit_module, "UserValves"):
  244. __user__["valves"] = toolkit_module.UserValves(
  245. **Tools.get_user_valves_by_id_and_user_id(
  246. tool_id, user.id
  247. )
  248. )
  249. except Exception as e:
  250. print(e)
  251. params = {**params, "__user__": __user__}
  252. if "__messages__" in sig.parameters:
  253. # Call the function with the '__messages__' parameter included
  254. params = {
  255. **params,
  256. "__messages__": messages,
  257. }
  258. if "__files__" in sig.parameters:
  259. # Call the function with the '__files__' parameter included
  260. params = {
  261. **params,
  262. "__files__": files,
  263. }
  264. if "__model__" in sig.parameters:
  265. # Call the function with the '__model__' parameter included
  266. params = {
  267. **params,
  268. "__model__": model,
  269. }
  270. if "__id__" in sig.parameters:
  271. # Call the function with the '__id__' parameter included
  272. params = {
  273. **params,
  274. "__id__": tool_id,
  275. }
  276. if inspect.iscoroutinefunction(function):
  277. function_result = await function(**params)
  278. else:
  279. function_result = function(**params)
  280. if hasattr(toolkit_module, "citation") and toolkit_module.citation:
  281. citation = {
  282. "source": {"name": f"TOOL:{tool.name}/{result['name']}"},
  283. "document": [function_result],
  284. "metadata": [{"source": result["name"]}],
  285. }
  286. except Exception as e:
  287. print(e)
  288. # Add the function result to the system prompt
  289. if function_result is not None:
  290. return function_result, citation, file_handler
  291. except Exception as e:
  292. print(f"Error: {e}")
  293. return None, None, False
  294. class ChatCompletionMiddleware(BaseHTTPMiddleware):
  295. async def dispatch(self, request: Request, call_next):
  296. data_items = []
  297. show_citations = False
  298. citations = []
  299. if request.method == "POST" and any(
  300. endpoint in request.url.path
  301. for endpoint in ["/ollama/api/chat", "/chat/completions"]
  302. ):
  303. log.debug(f"request.url.path: {request.url.path}")
  304. # Read the original request body
  305. body = await request.body()
  306. body_str = body.decode("utf-8")
  307. data = json.loads(body_str) if body_str else {}
  308. user = get_current_user(
  309. request,
  310. get_http_authorization_cred(request.headers.get("Authorization")),
  311. )
  312. # Flag to skip RAG completions if file_handler is present in tools/functions
  313. skip_files = False
  314. if data.get("citations"):
  315. show_citations = True
  316. del data["citations"]
  317. model_id = data["model"]
  318. if model_id not in app.state.MODELS:
  319. raise HTTPException(
  320. status_code=status.HTTP_404_NOT_FOUND,
  321. detail="Model not found",
  322. )
  323. model = app.state.MODELS[model_id]
  324. def get_priority(function_id):
  325. function = Functions.get_function_by_id(function_id)
  326. if function is not None and hasattr(function, "valves"):
  327. return (function.valves if function.valves else {}).get(
  328. "priority", 0
  329. )
  330. return 0
  331. filter_ids = [
  332. function.id
  333. for function in Functions.get_functions_by_type(
  334. "filter", active_only=True
  335. )
  336. ]
  337. # Check if the model has any filters
  338. if "info" in model and "meta" in model["info"]:
  339. filter_ids.extend(model["info"]["meta"].get("filterIds", []))
  340. filter_ids = list(set(filter_ids))
  341. filter_ids.sort(key=get_priority)
  342. for filter_id in filter_ids:
  343. filter = Functions.get_function_by_id(filter_id)
  344. if filter:
  345. if filter_id in webui_app.state.FUNCTIONS:
  346. function_module = webui_app.state.FUNCTIONS[filter_id]
  347. else:
  348. function_module, function_type = load_function_module_by_id(
  349. filter_id
  350. )
  351. webui_app.state.FUNCTIONS[filter_id] = function_module
  352. # Check if the function has a file_handler variable
  353. if hasattr(function_module, "file_handler"):
  354. skip_files = function_module.file_handler
  355. if hasattr(function_module, "valves") and hasattr(
  356. function_module, "Valves"
  357. ):
  358. valves = Functions.get_function_valves_by_id(filter_id)
  359. function_module.valves = function_module.Valves(
  360. **(valves if valves else {})
  361. )
  362. try:
  363. if hasattr(function_module, "inlet"):
  364. inlet = function_module.inlet
  365. # Get the signature of the function
  366. sig = inspect.signature(inlet)
  367. params = {"body": data}
  368. if "__user__" in sig.parameters:
  369. __user__ = {
  370. "id": user.id,
  371. "email": user.email,
  372. "name": user.name,
  373. "role": user.role,
  374. }
  375. try:
  376. if hasattr(function_module, "UserValves"):
  377. __user__["valves"] = function_module.UserValves(
  378. **Functions.get_user_valves_by_id_and_user_id(
  379. filter_id, user.id
  380. )
  381. )
  382. except Exception as e:
  383. print(e)
  384. params = {**params, "__user__": __user__}
  385. if "__id__" in sig.parameters:
  386. params = {
  387. **params,
  388. "__id__": filter_id,
  389. }
  390. if inspect.iscoroutinefunction(inlet):
  391. data = await inlet(**params)
  392. else:
  393. data = inlet(**params)
  394. except Exception as e:
  395. print(f"Error: {e}")
  396. return JSONResponse(
  397. status_code=status.HTTP_400_BAD_REQUEST,
  398. content={"detail": str(e)},
  399. )
  400. # Set the task model
  401. task_model_id = data["model"]
  402. # Check if the user has a custom task model and use that model
  403. if app.state.MODELS[task_model_id]["owned_by"] == "ollama":
  404. if (
  405. app.state.config.TASK_MODEL
  406. and app.state.config.TASK_MODEL in app.state.MODELS
  407. ):
  408. task_model_id = app.state.config.TASK_MODEL
  409. else:
  410. if (
  411. app.state.config.TASK_MODEL_EXTERNAL
  412. and app.state.config.TASK_MODEL_EXTERNAL in app.state.MODELS
  413. ):
  414. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  415. prompt = get_last_user_message(data["messages"])
  416. context = ""
  417. # If tool_ids field is present, call the functions
  418. if "tool_ids" in data:
  419. print(data["tool_ids"])
  420. for tool_id in data["tool_ids"]:
  421. print(tool_id)
  422. try:
  423. response, citation, file_handler = (
  424. await get_function_call_response(
  425. messages=data["messages"],
  426. files=data.get("files", []),
  427. tool_id=tool_id,
  428. template=app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  429. task_model_id=task_model_id,
  430. user=user,
  431. )
  432. )
  433. print(file_handler)
  434. if isinstance(response, str):
  435. context += ("\n" if context != "" else "") + response
  436. if citation:
  437. citations.append(citation)
  438. show_citations = True
  439. if file_handler:
  440. skip_files = True
  441. except Exception as e:
  442. print(f"Error: {e}")
  443. del data["tool_ids"]
  444. print(f"tool_context: {context}")
  445. # If files field is present, generate RAG completions
  446. # If skip_files is True, skip the RAG completions
  447. if "files" in data:
  448. if not skip_files:
  449. data = {**data}
  450. rag_context, rag_citations = get_rag_context(
  451. files=data["files"],
  452. messages=data["messages"],
  453. embedding_function=rag_app.state.EMBEDDING_FUNCTION,
  454. k=rag_app.state.config.TOP_K,
  455. reranking_function=rag_app.state.sentence_transformer_rf,
  456. r=rag_app.state.config.RELEVANCE_THRESHOLD,
  457. hybrid_search=rag_app.state.config.ENABLE_RAG_HYBRID_SEARCH,
  458. )
  459. if rag_context:
  460. context += ("\n" if context != "" else "") + rag_context
  461. log.debug(f"rag_context: {rag_context}, citations: {citations}")
  462. if rag_citations:
  463. citations.extend(rag_citations)
  464. del data["files"]
  465. if show_citations and len(citations) > 0:
  466. data_items.append({"citations": citations})
  467. if context != "":
  468. system_prompt = rag_template(
  469. rag_app.state.config.RAG_TEMPLATE, context, prompt
  470. )
  471. print(system_prompt)
  472. data["messages"] = add_or_update_system_message(
  473. system_prompt, data["messages"]
  474. )
  475. modified_body_bytes = json.dumps(data).encode("utf-8")
  476. # Replace the request body with the modified one
  477. request._body = modified_body_bytes
  478. # Set custom header to ensure content-length matches new body length
  479. request.headers.__dict__["_list"] = [
  480. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  481. *[
  482. (k, v)
  483. for k, v in request.headers.raw
  484. if k.lower() != b"content-length"
  485. ],
  486. ]
  487. response = await call_next(request)
  488. if isinstance(response, StreamingResponse):
  489. # If it's a streaming response, inject it as SSE event or NDJSON line
  490. content_type = response.headers.get("Content-Type")
  491. if "text/event-stream" in content_type:
  492. return StreamingResponse(
  493. self.openai_stream_wrapper(response.body_iterator, data_items),
  494. )
  495. if "application/x-ndjson" in content_type:
  496. return StreamingResponse(
  497. self.ollama_stream_wrapper(response.body_iterator, data_items),
  498. )
  499. else:
  500. return response
  501. # If it's not a chat completion request, just pass it through
  502. response = await call_next(request)
  503. return response
  504. async def _receive(self, body: bytes):
  505. return {"type": "http.request", "body": body, "more_body": False}
  506. async def openai_stream_wrapper(self, original_generator, data_items):
  507. for item in data_items:
  508. yield f"data: {json.dumps(item)}\n\n"
  509. async for data in original_generator:
  510. yield data
  511. async def ollama_stream_wrapper(self, original_generator, data_items):
  512. for item in data_items:
  513. yield f"{json.dumps(item)}\n"
  514. async for data in original_generator:
  515. yield data
  516. app.add_middleware(ChatCompletionMiddleware)
  517. ##################################
  518. #
  519. # Pipeline Middleware
  520. #
  521. ##################################
  522. def filter_pipeline(payload, user):
  523. user = {"id": user.id, "email": user.email, "name": user.name, "role": user.role}
  524. model_id = payload["model"]
  525. filters = [
  526. model
  527. for model in app.state.MODELS.values()
  528. if "pipeline" in model
  529. and "type" in model["pipeline"]
  530. and model["pipeline"]["type"] == "filter"
  531. and (
  532. model["pipeline"]["pipelines"] == ["*"]
  533. or any(
  534. model_id == target_model_id
  535. for target_model_id in model["pipeline"]["pipelines"]
  536. )
  537. )
  538. ]
  539. sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"])
  540. model = app.state.MODELS[model_id]
  541. if "pipeline" in model:
  542. sorted_filters.append(model)
  543. for filter in sorted_filters:
  544. r = None
  545. try:
  546. urlIdx = filter["urlIdx"]
  547. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  548. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  549. if key != "":
  550. headers = {"Authorization": f"Bearer {key}"}
  551. r = requests.post(
  552. f"{url}/{filter['id']}/filter/inlet",
  553. headers=headers,
  554. json={
  555. "user": user,
  556. "body": payload,
  557. },
  558. )
  559. r.raise_for_status()
  560. payload = r.json()
  561. except Exception as e:
  562. # Handle connection error here
  563. print(f"Connection error: {e}")
  564. if r is not None:
  565. try:
  566. res = r.json()
  567. except:
  568. pass
  569. if "detail" in res:
  570. raise Exception(r.status_code, res["detail"])
  571. else:
  572. pass
  573. if "pipeline" not in app.state.MODELS[model_id]:
  574. if "chat_id" in payload:
  575. del payload["chat_id"]
  576. if "title" in payload:
  577. del payload["title"]
  578. if "task" in payload:
  579. del payload["task"]
  580. return payload
  581. class PipelineMiddleware(BaseHTTPMiddleware):
  582. async def dispatch(self, request: Request, call_next):
  583. if request.method == "POST" and (
  584. "/ollama/api/chat" in request.url.path
  585. or "/chat/completions" in request.url.path
  586. ):
  587. log.debug(f"request.url.path: {request.url.path}")
  588. # Read the original request body
  589. body = await request.body()
  590. # Decode body to string
  591. body_str = body.decode("utf-8")
  592. # Parse string to JSON
  593. data = json.loads(body_str) if body_str else {}
  594. user = get_current_user(
  595. request,
  596. get_http_authorization_cred(request.headers.get("Authorization")),
  597. )
  598. try:
  599. data = filter_pipeline(data, user)
  600. except Exception as e:
  601. return JSONResponse(
  602. status_code=e.args[0],
  603. content={"detail": e.args[1]},
  604. )
  605. modified_body_bytes = json.dumps(data).encode("utf-8")
  606. # Replace the request body with the modified one
  607. request._body = modified_body_bytes
  608. # Set custom header to ensure content-length matches new body length
  609. request.headers.__dict__["_list"] = [
  610. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  611. *[
  612. (k, v)
  613. for k, v in request.headers.raw
  614. if k.lower() != b"content-length"
  615. ],
  616. ]
  617. response = await call_next(request)
  618. return response
  619. async def _receive(self, body: bytes):
  620. return {"type": "http.request", "body": body, "more_body": False}
  621. app.add_middleware(PipelineMiddleware)
  622. app.add_middleware(
  623. CORSMiddleware,
  624. allow_origins=origins,
  625. allow_credentials=True,
  626. allow_methods=["*"],
  627. allow_headers=["*"],
  628. )
  629. @app.middleware("http")
  630. async def check_url(request: Request, call_next):
  631. if len(app.state.MODELS) == 0:
  632. await get_all_models()
  633. else:
  634. pass
  635. start_time = int(time.time())
  636. response = await call_next(request)
  637. process_time = int(time.time()) - start_time
  638. response.headers["X-Process-Time"] = str(process_time)
  639. return response
  640. @app.middleware("http")
  641. async def update_embedding_function(request: Request, call_next):
  642. response = await call_next(request)
  643. if "/embedding/update" in request.url.path:
  644. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  645. return response
  646. app.mount("/ws", socket_app)
  647. app.mount("/ollama", ollama_app)
  648. app.mount("/openai", openai_app)
  649. app.mount("/images/api/v1", images_app)
  650. app.mount("/audio/api/v1", audio_app)
  651. app.mount("/rag/api/v1", rag_app)
  652. app.mount("/api/v1", webui_app)
  653. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  654. async def get_all_models():
  655. pipe_models = []
  656. openai_models = []
  657. ollama_models = []
  658. pipe_models = await get_pipe_models()
  659. if app.state.config.ENABLE_OPENAI_API:
  660. openai_models = await get_openai_models()
  661. openai_models = openai_models["data"]
  662. if app.state.config.ENABLE_OLLAMA_API:
  663. ollama_models = await get_ollama_models()
  664. ollama_models = [
  665. {
  666. "id": model["model"],
  667. "name": model["name"],
  668. "object": "model",
  669. "created": int(time.time()),
  670. "owned_by": "ollama",
  671. "ollama": model,
  672. }
  673. for model in ollama_models["models"]
  674. ]
  675. models = pipe_models + openai_models + ollama_models
  676. custom_models = Models.get_all_models()
  677. for custom_model in custom_models:
  678. if custom_model.base_model_id == None:
  679. for model in models:
  680. if (
  681. custom_model.id == model["id"]
  682. or custom_model.id == model["id"].split(":")[0]
  683. ):
  684. model["name"] = custom_model.name
  685. model["info"] = custom_model.model_dump()
  686. else:
  687. owned_by = "openai"
  688. for model in models:
  689. if (
  690. custom_model.base_model_id == model["id"]
  691. or custom_model.base_model_id == model["id"].split(":")[0]
  692. ):
  693. owned_by = model["owned_by"]
  694. break
  695. models.append(
  696. {
  697. "id": custom_model.id,
  698. "name": custom_model.name,
  699. "object": "model",
  700. "created": custom_model.created_at,
  701. "owned_by": owned_by,
  702. "info": custom_model.model_dump(),
  703. "preset": True,
  704. }
  705. )
  706. app.state.MODELS = {model["id"]: model for model in models}
  707. webui_app.state.MODELS = app.state.MODELS
  708. return models
  709. @app.get("/api/models")
  710. async def get_models(user=Depends(get_verified_user)):
  711. models = await get_all_models()
  712. # Filter out filter pipelines
  713. models = [
  714. model
  715. for model in models
  716. if "pipeline" not in model or model["pipeline"].get("type", None) != "filter"
  717. ]
  718. if app.state.config.ENABLE_MODEL_FILTER:
  719. if user.role == "user":
  720. models = list(
  721. filter(
  722. lambda model: model["id"] in app.state.config.MODEL_FILTER_LIST,
  723. models,
  724. )
  725. )
  726. return {"data": models}
  727. return {"data": models}
  728. @app.post("/api/chat/completions")
  729. async def generate_chat_completions(form_data: dict, user=Depends(get_verified_user)):
  730. model_id = form_data["model"]
  731. if model_id not in app.state.MODELS:
  732. raise HTTPException(
  733. status_code=status.HTTP_404_NOT_FOUND,
  734. detail="Model not found",
  735. )
  736. model = app.state.MODELS[model_id]
  737. print(model)
  738. pipe = model.get("pipe")
  739. if pipe:
  740. async def job():
  741. pipe_id = form_data["model"]
  742. if "." in pipe_id:
  743. pipe_id, sub_pipe_id = pipe_id.split(".", 1)
  744. print(pipe_id)
  745. # Check if function is already loaded
  746. if pipe_id not in webui_app.state.FUNCTIONS:
  747. function_module, function_type = load_function_module_by_id(pipe_id)
  748. webui_app.state.FUNCTIONS[pipe_id] = function_module
  749. else:
  750. function_module = webui_app.state.FUNCTIONS[pipe_id]
  751. if hasattr(function_module, "valves") and hasattr(
  752. function_module, "Valves"
  753. ):
  754. valves = Functions.get_function_valves_by_id(pipe_id)
  755. function_module.valves = function_module.Valves(
  756. **(valves if valves else {})
  757. )
  758. pipe = function_module.pipe
  759. # Get the signature of the function
  760. sig = inspect.signature(pipe)
  761. params = {"body": form_data}
  762. if "__user__" in sig.parameters:
  763. __user__ = {
  764. "id": user.id,
  765. "email": user.email,
  766. "name": user.name,
  767. "role": user.role,
  768. }
  769. try:
  770. if hasattr(function_module, "UserValves"):
  771. __user__["valves"] = function_module.UserValves(
  772. **Functions.get_user_valves_by_id_and_user_id(
  773. pipe_id, user.id
  774. )
  775. )
  776. except Exception as e:
  777. print(e)
  778. params = {**params, "__user__": __user__}
  779. if form_data["stream"]:
  780. async def stream_content():
  781. try:
  782. if inspect.iscoroutinefunction(pipe):
  783. res = await pipe(**params)
  784. else:
  785. res = pipe(**params)
  786. except Exception as e:
  787. print(f"Error: {e}")
  788. yield f"data: {json.dumps({'error': {'detail':str(e)}})}\n\n"
  789. return
  790. if isinstance(res, str):
  791. message = stream_message_template(form_data["model"], res)
  792. yield f"data: {json.dumps(message)}\n\n"
  793. if isinstance(res, Iterator):
  794. for line in res:
  795. if isinstance(line, BaseModel):
  796. line = line.model_dump_json()
  797. line = f"data: {line}"
  798. try:
  799. line = line.decode("utf-8")
  800. except:
  801. pass
  802. if line.startswith("data:"):
  803. yield f"{line}\n\n"
  804. else:
  805. line = stream_message_template(form_data["model"], line)
  806. yield f"data: {json.dumps(line)}\n\n"
  807. if isinstance(res, str) or isinstance(res, Generator):
  808. finish_message = {
  809. "id": f"{form_data['model']}-{str(uuid.uuid4())}",
  810. "object": "chat.completion.chunk",
  811. "created": int(time.time()),
  812. "model": form_data["model"],
  813. "choices": [
  814. {
  815. "index": 0,
  816. "delta": {},
  817. "logprobs": None,
  818. "finish_reason": "stop",
  819. }
  820. ],
  821. }
  822. yield f"data: {json.dumps(finish_message)}\n\n"
  823. yield f"data: [DONE]"
  824. return StreamingResponse(
  825. stream_content(), media_type="text/event-stream"
  826. )
  827. else:
  828. try:
  829. if inspect.iscoroutinefunction(pipe):
  830. res = await pipe(**params)
  831. else:
  832. res = pipe(**params)
  833. except Exception as e:
  834. print(f"Error: {e}")
  835. return {"error": {"detail": str(e)}}
  836. if inspect.iscoroutinefunction(pipe):
  837. res = await pipe(**params)
  838. else:
  839. res = pipe(**params)
  840. if isinstance(res, dict):
  841. return res
  842. elif isinstance(res, BaseModel):
  843. return res.model_dump()
  844. else:
  845. message = ""
  846. if isinstance(res, str):
  847. message = res
  848. if isinstance(res, Generator):
  849. for stream in res:
  850. message = f"{message}{stream}"
  851. return {
  852. "id": f"{form_data['model']}-{str(uuid.uuid4())}",
  853. "object": "chat.completion",
  854. "created": int(time.time()),
  855. "model": form_data["model"],
  856. "choices": [
  857. {
  858. "index": 0,
  859. "message": {
  860. "role": "assistant",
  861. "content": message,
  862. },
  863. "logprobs": None,
  864. "finish_reason": "stop",
  865. }
  866. ],
  867. }
  868. return await job()
  869. if model["owned_by"] == "ollama":
  870. return await generate_ollama_chat_completion(form_data, user=user)
  871. else:
  872. return await generate_openai_chat_completion(form_data, user=user)
  873. @app.post("/api/chat/completed")
  874. async def chat_completed(form_data: dict, user=Depends(get_verified_user)):
  875. data = form_data
  876. model_id = data["model"]
  877. if model_id not in app.state.MODELS:
  878. raise HTTPException(
  879. status_code=status.HTTP_404_NOT_FOUND,
  880. detail="Model not found",
  881. )
  882. model = app.state.MODELS[model_id]
  883. filters = [
  884. model
  885. for model in app.state.MODELS.values()
  886. if "pipeline" in model
  887. and "type" in model["pipeline"]
  888. and model["pipeline"]["type"] == "filter"
  889. and (
  890. model["pipeline"]["pipelines"] == ["*"]
  891. or any(
  892. model_id == target_model_id
  893. for target_model_id in model["pipeline"]["pipelines"]
  894. )
  895. )
  896. ]
  897. sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"])
  898. if "pipeline" in model:
  899. sorted_filters = [model] + sorted_filters
  900. for filter in sorted_filters:
  901. r = None
  902. try:
  903. urlIdx = filter["urlIdx"]
  904. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  905. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  906. if key != "":
  907. headers = {"Authorization": f"Bearer {key}"}
  908. r = requests.post(
  909. f"{url}/{filter['id']}/filter/outlet",
  910. headers=headers,
  911. json={
  912. "user": {
  913. "id": user.id,
  914. "name": user.name,
  915. "email": user.email,
  916. "role": user.role,
  917. },
  918. "body": data,
  919. },
  920. )
  921. r.raise_for_status()
  922. data = r.json()
  923. except Exception as e:
  924. # Handle connection error here
  925. print(f"Connection error: {e}")
  926. if r is not None:
  927. try:
  928. res = r.json()
  929. if "detail" in res:
  930. return JSONResponse(
  931. status_code=r.status_code,
  932. content=res,
  933. )
  934. except:
  935. pass
  936. else:
  937. pass
  938. def get_priority(function_id):
  939. function = Functions.get_function_by_id(function_id)
  940. if function is not None and hasattr(function, "valves"):
  941. return (function.valves if function.valves else {}).get("priority", 0)
  942. return 0
  943. filter_ids = [
  944. function.id
  945. for function in Functions.get_functions_by_type("filter", active_only=True)
  946. ]
  947. # Check if the model has any filters
  948. if "info" in model and "meta" in model["info"]:
  949. filter_ids.extend(model["info"]["meta"].get("filterIds", []))
  950. filter_ids = list(set(filter_ids))
  951. # Sort filter_ids by priority, using the get_priority function
  952. filter_ids.sort(key=get_priority)
  953. for filter_id in filter_ids:
  954. filter = Functions.get_function_by_id(filter_id)
  955. if filter:
  956. if filter_id in webui_app.state.FUNCTIONS:
  957. function_module = webui_app.state.FUNCTIONS[filter_id]
  958. else:
  959. function_module, function_type = load_function_module_by_id(filter_id)
  960. webui_app.state.FUNCTIONS[filter_id] = function_module
  961. if hasattr(function_module, "valves") and hasattr(
  962. function_module, "Valves"
  963. ):
  964. valves = Functions.get_function_valves_by_id(filter_id)
  965. function_module.valves = function_module.Valves(
  966. **(valves if valves else {})
  967. )
  968. try:
  969. if hasattr(function_module, "outlet"):
  970. outlet = function_module.outlet
  971. # Get the signature of the function
  972. sig = inspect.signature(outlet)
  973. params = {"body": data}
  974. if "__user__" in sig.parameters:
  975. __user__ = {
  976. "id": user.id,
  977. "email": user.email,
  978. "name": user.name,
  979. "role": user.role,
  980. }
  981. try:
  982. if hasattr(function_module, "UserValves"):
  983. __user__["valves"] = function_module.UserValves(
  984. **Functions.get_user_valves_by_id_and_user_id(
  985. filter_id, user.id
  986. )
  987. )
  988. except Exception as e:
  989. print(e)
  990. params = {**params, "__user__": __user__}
  991. if "__id__" in sig.parameters:
  992. params = {
  993. **params,
  994. "__id__": filter_id,
  995. }
  996. if inspect.iscoroutinefunction(outlet):
  997. data = await outlet(**params)
  998. else:
  999. data = outlet(**params)
  1000. except Exception as e:
  1001. print(f"Error: {e}")
  1002. return JSONResponse(
  1003. status_code=status.HTTP_400_BAD_REQUEST,
  1004. content={"detail": str(e)},
  1005. )
  1006. return data
  1007. ##################################
  1008. #
  1009. # Task Endpoints
  1010. #
  1011. ##################################
  1012. # TODO: Refactor task API endpoints below into a separate file
  1013. @app.get("/api/task/config")
  1014. async def get_task_config(user=Depends(get_verified_user)):
  1015. return {
  1016. "TASK_MODEL": app.state.config.TASK_MODEL,
  1017. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  1018. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  1019. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  1020. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  1021. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  1022. }
  1023. class TaskConfigForm(BaseModel):
  1024. TASK_MODEL: Optional[str]
  1025. TASK_MODEL_EXTERNAL: Optional[str]
  1026. TITLE_GENERATION_PROMPT_TEMPLATE: str
  1027. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE: str
  1028. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD: int
  1029. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: str
  1030. @app.post("/api/task/config/update")
  1031. async def update_task_config(form_data: TaskConfigForm, user=Depends(get_admin_user)):
  1032. app.state.config.TASK_MODEL = form_data.TASK_MODEL
  1033. app.state.config.TASK_MODEL_EXTERNAL = form_data.TASK_MODEL_EXTERNAL
  1034. app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = (
  1035. form_data.TITLE_GENERATION_PROMPT_TEMPLATE
  1036. )
  1037. app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE = (
  1038. form_data.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  1039. )
  1040. app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD = (
  1041. form_data.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD
  1042. )
  1043. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  1044. form_data.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  1045. )
  1046. return {
  1047. "TASK_MODEL": app.state.config.TASK_MODEL,
  1048. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  1049. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  1050. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  1051. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  1052. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  1053. }
  1054. @app.post("/api/task/title/completions")
  1055. async def generate_title(form_data: dict, user=Depends(get_verified_user)):
  1056. print("generate_title")
  1057. model_id = form_data["model"]
  1058. if model_id not in app.state.MODELS:
  1059. raise HTTPException(
  1060. status_code=status.HTTP_404_NOT_FOUND,
  1061. detail="Model not found",
  1062. )
  1063. # Check if the user has a custom task model
  1064. # If the user has a custom task model, use that model
  1065. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1066. if app.state.config.TASK_MODEL:
  1067. task_model_id = app.state.config.TASK_MODEL
  1068. if task_model_id in app.state.MODELS:
  1069. model_id = task_model_id
  1070. else:
  1071. if app.state.config.TASK_MODEL_EXTERNAL:
  1072. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1073. if task_model_id in app.state.MODELS:
  1074. model_id = task_model_id
  1075. print(model_id)
  1076. model = app.state.MODELS[model_id]
  1077. template = app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE
  1078. content = title_generation_template(
  1079. template,
  1080. form_data["prompt"],
  1081. {
  1082. "name": user.name,
  1083. "location": user.info.get("location") if user.info else None,
  1084. },
  1085. )
  1086. payload = {
  1087. "model": model_id,
  1088. "messages": [{"role": "user", "content": content}],
  1089. "stream": False,
  1090. "max_tokens": 50,
  1091. "chat_id": form_data.get("chat_id", None),
  1092. "title": True,
  1093. }
  1094. log.debug(payload)
  1095. try:
  1096. payload = filter_pipeline(payload, user)
  1097. except Exception as e:
  1098. return JSONResponse(
  1099. status_code=e.args[0],
  1100. content={"detail": e.args[1]},
  1101. )
  1102. if model["owned_by"] == "ollama":
  1103. return await generate_ollama_chat_completion(payload, user=user)
  1104. else:
  1105. return await generate_openai_chat_completion(payload, user=user)
  1106. @app.post("/api/task/query/completions")
  1107. async def generate_search_query(form_data: dict, user=Depends(get_verified_user)):
  1108. print("generate_search_query")
  1109. if len(form_data["prompt"]) < app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD:
  1110. raise HTTPException(
  1111. status_code=status.HTTP_400_BAD_REQUEST,
  1112. detail=f"Skip search query generation for short prompts (< {app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD} characters)",
  1113. )
  1114. model_id = form_data["model"]
  1115. if model_id not in app.state.MODELS:
  1116. raise HTTPException(
  1117. status_code=status.HTTP_404_NOT_FOUND,
  1118. detail="Model not found",
  1119. )
  1120. # Check if the user has a custom task model
  1121. # If the user has a custom task model, use that model
  1122. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1123. if app.state.config.TASK_MODEL:
  1124. task_model_id = app.state.config.TASK_MODEL
  1125. if task_model_id in app.state.MODELS:
  1126. model_id = task_model_id
  1127. else:
  1128. if app.state.config.TASK_MODEL_EXTERNAL:
  1129. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1130. if task_model_id in app.state.MODELS:
  1131. model_id = task_model_id
  1132. print(model_id)
  1133. model = app.state.MODELS[model_id]
  1134. template = app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  1135. content = search_query_generation_template(
  1136. template, form_data["prompt"], {"name": user.name}
  1137. )
  1138. payload = {
  1139. "model": model_id,
  1140. "messages": [{"role": "user", "content": content}],
  1141. "stream": False,
  1142. "max_tokens": 30,
  1143. "task": True,
  1144. }
  1145. print(payload)
  1146. try:
  1147. payload = filter_pipeline(payload, user)
  1148. except Exception as e:
  1149. return JSONResponse(
  1150. status_code=e.args[0],
  1151. content={"detail": e.args[1]},
  1152. )
  1153. if model["owned_by"] == "ollama":
  1154. return await generate_ollama_chat_completion(payload, user=user)
  1155. else:
  1156. return await generate_openai_chat_completion(payload, user=user)
  1157. @app.post("/api/task/emoji/completions")
  1158. async def generate_emoji(form_data: dict, user=Depends(get_verified_user)):
  1159. print("generate_emoji")
  1160. model_id = form_data["model"]
  1161. if model_id not in app.state.MODELS:
  1162. raise HTTPException(
  1163. status_code=status.HTTP_404_NOT_FOUND,
  1164. detail="Model not found",
  1165. )
  1166. # Check if the user has a custom task model
  1167. # If the user has a custom task model, use that model
  1168. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1169. if app.state.config.TASK_MODEL:
  1170. task_model_id = app.state.config.TASK_MODEL
  1171. if task_model_id in app.state.MODELS:
  1172. model_id = task_model_id
  1173. else:
  1174. if app.state.config.TASK_MODEL_EXTERNAL:
  1175. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1176. if task_model_id in app.state.MODELS:
  1177. model_id = task_model_id
  1178. print(model_id)
  1179. model = app.state.MODELS[model_id]
  1180. template = '''
  1181. 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., 😊, 😢, 😡, 😱).
  1182. Message: """{{prompt}}"""
  1183. '''
  1184. content = title_generation_template(
  1185. template,
  1186. form_data["prompt"],
  1187. {
  1188. "name": user.name,
  1189. "location": user.info.get("location") if user.info else None,
  1190. },
  1191. )
  1192. payload = {
  1193. "model": model_id,
  1194. "messages": [{"role": "user", "content": content}],
  1195. "stream": False,
  1196. "max_tokens": 4,
  1197. "chat_id": form_data.get("chat_id", None),
  1198. "task": True,
  1199. }
  1200. log.debug(payload)
  1201. try:
  1202. payload = filter_pipeline(payload, user)
  1203. except Exception as e:
  1204. return JSONResponse(
  1205. status_code=e.args[0],
  1206. content={"detail": e.args[1]},
  1207. )
  1208. if model["owned_by"] == "ollama":
  1209. return await generate_ollama_chat_completion(payload, user=user)
  1210. else:
  1211. return await generate_openai_chat_completion(payload, user=user)
  1212. @app.post("/api/task/tools/completions")
  1213. async def get_tools_function_calling(form_data: dict, user=Depends(get_verified_user)):
  1214. print("get_tools_function_calling")
  1215. model_id = form_data["model"]
  1216. if model_id not in app.state.MODELS:
  1217. raise HTTPException(
  1218. status_code=status.HTTP_404_NOT_FOUND,
  1219. detail="Model not found",
  1220. )
  1221. # Check if the user has a custom task model
  1222. # If the user has a custom task model, use that model
  1223. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1224. if app.state.config.TASK_MODEL:
  1225. task_model_id = app.state.config.TASK_MODEL
  1226. if task_model_id in app.state.MODELS:
  1227. model_id = task_model_id
  1228. else:
  1229. if app.state.config.TASK_MODEL_EXTERNAL:
  1230. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1231. if task_model_id in app.state.MODELS:
  1232. model_id = task_model_id
  1233. print(model_id)
  1234. template = app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  1235. try:
  1236. context, citation, file_handler = await get_function_call_response(
  1237. form_data["messages"],
  1238. form_data.get("files", []),
  1239. form_data["tool_id"],
  1240. template,
  1241. model_id,
  1242. user,
  1243. )
  1244. return context
  1245. except Exception as e:
  1246. return JSONResponse(
  1247. status_code=e.args[0],
  1248. content={"detail": e.args[1]},
  1249. )
  1250. ##################################
  1251. #
  1252. # Pipelines Endpoints
  1253. #
  1254. ##################################
  1255. # TODO: Refactor pipelines API endpoints below into a separate file
  1256. @app.get("/api/pipelines/list")
  1257. async def get_pipelines_list(user=Depends(get_admin_user)):
  1258. responses = await get_openai_models(raw=True)
  1259. print(responses)
  1260. urlIdxs = [
  1261. idx
  1262. for idx, response in enumerate(responses)
  1263. if response != None and "pipelines" in response
  1264. ]
  1265. return {
  1266. "data": [
  1267. {
  1268. "url": openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx],
  1269. "idx": urlIdx,
  1270. }
  1271. for urlIdx in urlIdxs
  1272. ]
  1273. }
  1274. @app.post("/api/pipelines/upload")
  1275. async def upload_pipeline(
  1276. urlIdx: int = Form(...), file: UploadFile = File(...), user=Depends(get_admin_user)
  1277. ):
  1278. print("upload_pipeline", urlIdx, file.filename)
  1279. # Check if the uploaded file is a python file
  1280. if not file.filename.endswith(".py"):
  1281. raise HTTPException(
  1282. status_code=status.HTTP_400_BAD_REQUEST,
  1283. detail="Only Python (.py) files are allowed.",
  1284. )
  1285. upload_folder = f"{CACHE_DIR}/pipelines"
  1286. os.makedirs(upload_folder, exist_ok=True)
  1287. file_path = os.path.join(upload_folder, file.filename)
  1288. try:
  1289. # Save the uploaded file
  1290. with open(file_path, "wb") as buffer:
  1291. shutil.copyfileobj(file.file, buffer)
  1292. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1293. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1294. headers = {"Authorization": f"Bearer {key}"}
  1295. with open(file_path, "rb") as f:
  1296. files = {"file": f}
  1297. r = requests.post(f"{url}/pipelines/upload", headers=headers, files=files)
  1298. r.raise_for_status()
  1299. data = r.json()
  1300. return {**data}
  1301. except Exception as e:
  1302. # Handle connection error here
  1303. print(f"Connection error: {e}")
  1304. detail = "Pipeline not found"
  1305. if r is not None:
  1306. try:
  1307. res = r.json()
  1308. if "detail" in res:
  1309. detail = res["detail"]
  1310. except:
  1311. pass
  1312. raise HTTPException(
  1313. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1314. detail=detail,
  1315. )
  1316. finally:
  1317. # Ensure the file is deleted after the upload is completed or on failure
  1318. if os.path.exists(file_path):
  1319. os.remove(file_path)
  1320. class AddPipelineForm(BaseModel):
  1321. url: str
  1322. urlIdx: int
  1323. @app.post("/api/pipelines/add")
  1324. async def add_pipeline(form_data: AddPipelineForm, user=Depends(get_admin_user)):
  1325. r = None
  1326. try:
  1327. urlIdx = form_data.urlIdx
  1328. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1329. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1330. headers = {"Authorization": f"Bearer {key}"}
  1331. r = requests.post(
  1332. f"{url}/pipelines/add", headers=headers, json={"url": form_data.url}
  1333. )
  1334. r.raise_for_status()
  1335. data = r.json()
  1336. return {**data}
  1337. except Exception as e:
  1338. # Handle connection error here
  1339. print(f"Connection error: {e}")
  1340. detail = "Pipeline not found"
  1341. if r is not None:
  1342. try:
  1343. res = r.json()
  1344. if "detail" in res:
  1345. detail = res["detail"]
  1346. except:
  1347. pass
  1348. raise HTTPException(
  1349. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1350. detail=detail,
  1351. )
  1352. class DeletePipelineForm(BaseModel):
  1353. id: str
  1354. urlIdx: int
  1355. @app.delete("/api/pipelines/delete")
  1356. async def delete_pipeline(form_data: DeletePipelineForm, user=Depends(get_admin_user)):
  1357. r = None
  1358. try:
  1359. urlIdx = form_data.urlIdx
  1360. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1361. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1362. headers = {"Authorization": f"Bearer {key}"}
  1363. r = requests.delete(
  1364. f"{url}/pipelines/delete", headers=headers, json={"id": form_data.id}
  1365. )
  1366. r.raise_for_status()
  1367. data = r.json()
  1368. return {**data}
  1369. except Exception as e:
  1370. # Handle connection error here
  1371. print(f"Connection error: {e}")
  1372. detail = "Pipeline not found"
  1373. if r is not None:
  1374. try:
  1375. res = r.json()
  1376. if "detail" in res:
  1377. detail = res["detail"]
  1378. except:
  1379. pass
  1380. raise HTTPException(
  1381. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1382. detail=detail,
  1383. )
  1384. @app.get("/api/pipelines")
  1385. async def get_pipelines(urlIdx: Optional[int] = None, user=Depends(get_admin_user)):
  1386. r = None
  1387. try:
  1388. urlIdx
  1389. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1390. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1391. headers = {"Authorization": f"Bearer {key}"}
  1392. r = requests.get(f"{url}/pipelines", headers=headers)
  1393. r.raise_for_status()
  1394. data = r.json()
  1395. return {**data}
  1396. except Exception as e:
  1397. # Handle connection error here
  1398. print(f"Connection error: {e}")
  1399. detail = "Pipeline not found"
  1400. if r is not None:
  1401. try:
  1402. res = r.json()
  1403. if "detail" in res:
  1404. detail = res["detail"]
  1405. except:
  1406. pass
  1407. raise HTTPException(
  1408. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1409. detail=detail,
  1410. )
  1411. @app.get("/api/pipelines/{pipeline_id}/valves")
  1412. async def get_pipeline_valves(
  1413. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  1414. ):
  1415. models = await get_all_models()
  1416. r = None
  1417. try:
  1418. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1419. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1420. headers = {"Authorization": f"Bearer {key}"}
  1421. r = requests.get(f"{url}/{pipeline_id}/valves", headers=headers)
  1422. r.raise_for_status()
  1423. data = r.json()
  1424. return {**data}
  1425. except Exception as e:
  1426. # Handle connection error here
  1427. print(f"Connection error: {e}")
  1428. detail = "Pipeline not found"
  1429. if r is not None:
  1430. try:
  1431. res = r.json()
  1432. if "detail" in res:
  1433. detail = res["detail"]
  1434. except:
  1435. pass
  1436. raise HTTPException(
  1437. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1438. detail=detail,
  1439. )
  1440. @app.get("/api/pipelines/{pipeline_id}/valves/spec")
  1441. async def get_pipeline_valves_spec(
  1442. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  1443. ):
  1444. models = await get_all_models()
  1445. r = None
  1446. try:
  1447. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1448. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1449. headers = {"Authorization": f"Bearer {key}"}
  1450. r = requests.get(f"{url}/{pipeline_id}/valves/spec", headers=headers)
  1451. r.raise_for_status()
  1452. data = r.json()
  1453. return {**data}
  1454. except Exception as e:
  1455. # Handle connection error here
  1456. print(f"Connection error: {e}")
  1457. detail = "Pipeline not found"
  1458. if r is not None:
  1459. try:
  1460. res = r.json()
  1461. if "detail" in res:
  1462. detail = res["detail"]
  1463. except:
  1464. pass
  1465. raise HTTPException(
  1466. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1467. detail=detail,
  1468. )
  1469. @app.post("/api/pipelines/{pipeline_id}/valves/update")
  1470. async def update_pipeline_valves(
  1471. urlIdx: Optional[int],
  1472. pipeline_id: str,
  1473. form_data: dict,
  1474. user=Depends(get_admin_user),
  1475. ):
  1476. models = await get_all_models()
  1477. r = None
  1478. try:
  1479. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1480. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1481. headers = {"Authorization": f"Bearer {key}"}
  1482. r = requests.post(
  1483. f"{url}/{pipeline_id}/valves/update",
  1484. headers=headers,
  1485. json={**form_data},
  1486. )
  1487. r.raise_for_status()
  1488. data = r.json()
  1489. return {**data}
  1490. except Exception as e:
  1491. # Handle connection error here
  1492. print(f"Connection error: {e}")
  1493. detail = "Pipeline not found"
  1494. if r is not None:
  1495. try:
  1496. res = r.json()
  1497. if "detail" in res:
  1498. detail = res["detail"]
  1499. except:
  1500. pass
  1501. raise HTTPException(
  1502. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1503. detail=detail,
  1504. )
  1505. ##################################
  1506. #
  1507. # Config Endpoints
  1508. #
  1509. ##################################
  1510. @app.get("/api/config")
  1511. async def get_app_config():
  1512. # Checking and Handling the Absence of 'ui' in CONFIG_DATA
  1513. default_locale = "en-US"
  1514. if "ui" in CONFIG_DATA:
  1515. default_locale = CONFIG_DATA["ui"].get("default_locale", "en-US")
  1516. # The Rest of the Function Now Uses the Variables Defined Above
  1517. return {
  1518. "status": True,
  1519. "name": WEBUI_NAME,
  1520. "version": VERSION,
  1521. "default_locale": default_locale,
  1522. "default_models": webui_app.state.config.DEFAULT_MODELS,
  1523. "default_prompt_suggestions": webui_app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
  1524. "features": {
  1525. "auth": WEBUI_AUTH,
  1526. "auth_trusted_header": bool(webui_app.state.AUTH_TRUSTED_EMAIL_HEADER),
  1527. "enable_signup": webui_app.state.config.ENABLE_SIGNUP,
  1528. "enable_web_search": rag_app.state.config.ENABLE_RAG_WEB_SEARCH,
  1529. "enable_image_generation": images_app.state.config.ENABLED,
  1530. "enable_community_sharing": webui_app.state.config.ENABLE_COMMUNITY_SHARING,
  1531. "enable_admin_export": ENABLE_ADMIN_EXPORT,
  1532. },
  1533. "audio": {
  1534. "tts": {
  1535. "engine": audio_app.state.config.TTS_ENGINE,
  1536. "voice": audio_app.state.config.TTS_VOICE,
  1537. },
  1538. "stt": {
  1539. "engine": audio_app.state.config.STT_ENGINE,
  1540. },
  1541. },
  1542. }
  1543. @app.get("/api/config/model/filter")
  1544. async def get_model_filter_config(user=Depends(get_admin_user)):
  1545. return {
  1546. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1547. "models": app.state.config.MODEL_FILTER_LIST,
  1548. }
  1549. class ModelFilterConfigForm(BaseModel):
  1550. enabled: bool
  1551. models: List[str]
  1552. @app.post("/api/config/model/filter")
  1553. async def update_model_filter_config(
  1554. form_data: ModelFilterConfigForm, user=Depends(get_admin_user)
  1555. ):
  1556. app.state.config.ENABLE_MODEL_FILTER = form_data.enabled
  1557. app.state.config.MODEL_FILTER_LIST = form_data.models
  1558. return {
  1559. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1560. "models": app.state.config.MODEL_FILTER_LIST,
  1561. }
  1562. # TODO: webhook endpoint should be under config endpoints
  1563. @app.get("/api/webhook")
  1564. async def get_webhook_url(user=Depends(get_admin_user)):
  1565. return {
  1566. "url": app.state.config.WEBHOOK_URL,
  1567. }
  1568. class UrlForm(BaseModel):
  1569. url: str
  1570. @app.post("/api/webhook")
  1571. async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)):
  1572. app.state.config.WEBHOOK_URL = form_data.url
  1573. webui_app.state.WEBHOOK_URL = app.state.config.WEBHOOK_URL
  1574. return {"url": app.state.config.WEBHOOK_URL}
  1575. @app.get("/api/version")
  1576. async def get_app_config():
  1577. return {
  1578. "version": VERSION,
  1579. }
  1580. @app.get("/api/changelog")
  1581. async def get_app_changelog():
  1582. return {key: CHANGELOG[key] for idx, key in enumerate(CHANGELOG) if idx < 5}
  1583. @app.get("/api/version/updates")
  1584. async def get_app_latest_release_version():
  1585. try:
  1586. async with aiohttp.ClientSession(trust_env=True) as session:
  1587. async with session.get(
  1588. "https://api.github.com/repos/open-webui/open-webui/releases/latest"
  1589. ) as response:
  1590. response.raise_for_status()
  1591. data = await response.json()
  1592. latest_version = data["tag_name"]
  1593. return {"current": VERSION, "latest": latest_version[1:]}
  1594. except aiohttp.ClientError as e:
  1595. raise HTTPException(
  1596. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  1597. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  1598. )
  1599. @app.get("/manifest.json")
  1600. async def get_manifest_json():
  1601. return {
  1602. "name": WEBUI_NAME,
  1603. "short_name": WEBUI_NAME,
  1604. "start_url": "/",
  1605. "display": "standalone",
  1606. "background_color": "#343541",
  1607. "orientation": "portrait-primary",
  1608. "icons": [{"src": "/static/logo.png", "type": "image/png", "sizes": "500x500"}],
  1609. }
  1610. @app.get("/opensearch.xml")
  1611. async def get_opensearch_xml():
  1612. xml_content = rf"""
  1613. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  1614. <ShortName>{WEBUI_NAME}</ShortName>
  1615. <Description>Search {WEBUI_NAME}</Description>
  1616. <InputEncoding>UTF-8</InputEncoding>
  1617. <Image width="16" height="16" type="image/x-icon">{WEBUI_URL}/favicon.png</Image>
  1618. <Url type="text/html" method="get" template="{WEBUI_URL}/?q={"{searchTerms}"}"/>
  1619. <moz:SearchForm>{WEBUI_URL}</moz:SearchForm>
  1620. </OpenSearchDescription>
  1621. """
  1622. return Response(content=xml_content, media_type="application/xml")
  1623. @app.get("/health")
  1624. async def healthcheck():
  1625. return {"status": True}
  1626. app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
  1627. app.mount("/cache", StaticFiles(directory=CACHE_DIR), name="cache")
  1628. if os.path.exists(FRONTEND_BUILD_DIR):
  1629. mimetypes.add_type("text/javascript", ".js")
  1630. app.mount(
  1631. "/",
  1632. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  1633. name="spa-static-files",
  1634. )
  1635. else:
  1636. log.warning(
  1637. f"Frontend build directory not found at '{FRONTEND_BUILD_DIR}'. Serving API only."
  1638. )