main.py 64 KB

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