main.py 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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, frontmatter = 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, frontmatter = (
  349. load_function_module_by_id(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, frontmatter = (
  748. load_function_module_by_id(pipe_id)
  749. )
  750. webui_app.state.FUNCTIONS[pipe_id] = function_module
  751. else:
  752. function_module = webui_app.state.FUNCTIONS[pipe_id]
  753. if hasattr(function_module, "valves") and hasattr(
  754. function_module, "Valves"
  755. ):
  756. valves = Functions.get_function_valves_by_id(pipe_id)
  757. function_module.valves = function_module.Valves(
  758. **(valves if valves else {})
  759. )
  760. pipe = function_module.pipe
  761. # Get the signature of the function
  762. sig = inspect.signature(pipe)
  763. params = {"body": form_data}
  764. if "__user__" in sig.parameters:
  765. __user__ = {
  766. "id": user.id,
  767. "email": user.email,
  768. "name": user.name,
  769. "role": user.role,
  770. }
  771. try:
  772. if hasattr(function_module, "UserValves"):
  773. __user__["valves"] = function_module.UserValves(
  774. **Functions.get_user_valves_by_id_and_user_id(
  775. pipe_id, user.id
  776. )
  777. )
  778. except Exception as e:
  779. print(e)
  780. params = {**params, "__user__": __user__}
  781. if form_data["stream"]:
  782. async def stream_content():
  783. try:
  784. if inspect.iscoroutinefunction(pipe):
  785. res = await pipe(**params)
  786. else:
  787. res = pipe(**params)
  788. except Exception as e:
  789. print(f"Error: {e}")
  790. yield f"data: {json.dumps({'error': {'detail':str(e)}})}\n\n"
  791. return
  792. if isinstance(res, str):
  793. message = stream_message_template(form_data["model"], res)
  794. yield f"data: {json.dumps(message)}\n\n"
  795. if isinstance(res, Iterator):
  796. for line in res:
  797. if isinstance(line, BaseModel):
  798. line = line.model_dump_json()
  799. line = f"data: {line}"
  800. try:
  801. line = line.decode("utf-8")
  802. except:
  803. pass
  804. if line.startswith("data:"):
  805. yield f"{line}\n\n"
  806. else:
  807. line = stream_message_template(form_data["model"], line)
  808. yield f"data: {json.dumps(line)}\n\n"
  809. if isinstance(res, str) or isinstance(res, Generator):
  810. finish_message = {
  811. "id": f"{form_data['model']}-{str(uuid.uuid4())}",
  812. "object": "chat.completion.chunk",
  813. "created": int(time.time()),
  814. "model": form_data["model"],
  815. "choices": [
  816. {
  817. "index": 0,
  818. "delta": {},
  819. "logprobs": None,
  820. "finish_reason": "stop",
  821. }
  822. ],
  823. }
  824. yield f"data: {json.dumps(finish_message)}\n\n"
  825. yield f"data: [DONE]"
  826. return StreamingResponse(
  827. stream_content(), media_type="text/event-stream"
  828. )
  829. else:
  830. try:
  831. if inspect.iscoroutinefunction(pipe):
  832. res = await pipe(**params)
  833. else:
  834. res = pipe(**params)
  835. except Exception as e:
  836. print(f"Error: {e}")
  837. return {"error": {"detail": str(e)}}
  838. if inspect.iscoroutinefunction(pipe):
  839. res = await pipe(**params)
  840. else:
  841. res = pipe(**params)
  842. if isinstance(res, dict):
  843. return res
  844. elif isinstance(res, BaseModel):
  845. return res.model_dump()
  846. else:
  847. message = ""
  848. if isinstance(res, str):
  849. message = res
  850. if isinstance(res, Generator):
  851. for stream in res:
  852. message = f"{message}{stream}"
  853. return {
  854. "id": f"{form_data['model']}-{str(uuid.uuid4())}",
  855. "object": "chat.completion",
  856. "created": int(time.time()),
  857. "model": form_data["model"],
  858. "choices": [
  859. {
  860. "index": 0,
  861. "message": {
  862. "role": "assistant",
  863. "content": message,
  864. },
  865. "logprobs": None,
  866. "finish_reason": "stop",
  867. }
  868. ],
  869. }
  870. return await job()
  871. if model["owned_by"] == "ollama":
  872. return await generate_ollama_chat_completion(form_data, user=user)
  873. else:
  874. return await generate_openai_chat_completion(form_data, user=user)
  875. @app.post("/api/chat/completed")
  876. async def chat_completed(form_data: dict, user=Depends(get_verified_user)):
  877. data = form_data
  878. model_id = data["model"]
  879. if model_id not in app.state.MODELS:
  880. raise HTTPException(
  881. status_code=status.HTTP_404_NOT_FOUND,
  882. detail="Model not found",
  883. )
  884. model = app.state.MODELS[model_id]
  885. filters = [
  886. model
  887. for model in app.state.MODELS.values()
  888. if "pipeline" in model
  889. and "type" in model["pipeline"]
  890. and model["pipeline"]["type"] == "filter"
  891. and (
  892. model["pipeline"]["pipelines"] == ["*"]
  893. or any(
  894. model_id == target_model_id
  895. for target_model_id in model["pipeline"]["pipelines"]
  896. )
  897. )
  898. ]
  899. sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"])
  900. if "pipeline" in model:
  901. sorted_filters = [model] + sorted_filters
  902. for filter in sorted_filters:
  903. r = None
  904. try:
  905. urlIdx = filter["urlIdx"]
  906. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  907. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  908. if key != "":
  909. headers = {"Authorization": f"Bearer {key}"}
  910. r = requests.post(
  911. f"{url}/{filter['id']}/filter/outlet",
  912. headers=headers,
  913. json={
  914. "user": {
  915. "id": user.id,
  916. "name": user.name,
  917. "email": user.email,
  918. "role": user.role,
  919. },
  920. "body": data,
  921. },
  922. )
  923. r.raise_for_status()
  924. data = r.json()
  925. except Exception as e:
  926. # Handle connection error here
  927. print(f"Connection error: {e}")
  928. if r is not None:
  929. try:
  930. res = r.json()
  931. if "detail" in res:
  932. return JSONResponse(
  933. status_code=r.status_code,
  934. content=res,
  935. )
  936. except:
  937. pass
  938. else:
  939. pass
  940. def get_priority(function_id):
  941. function = Functions.get_function_by_id(function_id)
  942. if function is not None and hasattr(function, "valves"):
  943. return (function.valves if function.valves else {}).get("priority", 0)
  944. return 0
  945. filter_ids = [
  946. function.id
  947. for function in Functions.get_functions_by_type("filter", active_only=True)
  948. ]
  949. # Check if the model has any filters
  950. if "info" in model and "meta" in model["info"]:
  951. filter_ids.extend(model["info"]["meta"].get("filterIds", []))
  952. filter_ids = list(set(filter_ids))
  953. # Sort filter_ids by priority, using the get_priority function
  954. filter_ids.sort(key=get_priority)
  955. for filter_id in filter_ids:
  956. filter = Functions.get_function_by_id(filter_id)
  957. if filter:
  958. if filter_id in webui_app.state.FUNCTIONS:
  959. function_module = webui_app.state.FUNCTIONS[filter_id]
  960. else:
  961. function_module, function_type, frontmatter = (
  962. load_function_module_by_id(filter_id)
  963. )
  964. webui_app.state.FUNCTIONS[filter_id] = function_module
  965. if hasattr(function_module, "valves") and hasattr(
  966. function_module, "Valves"
  967. ):
  968. valves = Functions.get_function_valves_by_id(filter_id)
  969. function_module.valves = function_module.Valves(
  970. **(valves if valves else {})
  971. )
  972. try:
  973. if hasattr(function_module, "outlet"):
  974. outlet = function_module.outlet
  975. # Get the signature of the function
  976. sig = inspect.signature(outlet)
  977. params = {"body": data}
  978. if "__user__" in sig.parameters:
  979. __user__ = {
  980. "id": user.id,
  981. "email": user.email,
  982. "name": user.name,
  983. "role": user.role,
  984. }
  985. try:
  986. if hasattr(function_module, "UserValves"):
  987. __user__["valves"] = function_module.UserValves(
  988. **Functions.get_user_valves_by_id_and_user_id(
  989. filter_id, user.id
  990. )
  991. )
  992. except Exception as e:
  993. print(e)
  994. params = {**params, "__user__": __user__}
  995. if "__id__" in sig.parameters:
  996. params = {
  997. **params,
  998. "__id__": filter_id,
  999. }
  1000. if inspect.iscoroutinefunction(outlet):
  1001. data = await outlet(**params)
  1002. else:
  1003. data = outlet(**params)
  1004. except Exception as e:
  1005. print(f"Error: {e}")
  1006. return JSONResponse(
  1007. status_code=status.HTTP_400_BAD_REQUEST,
  1008. content={"detail": str(e)},
  1009. )
  1010. return data
  1011. ##################################
  1012. #
  1013. # Task Endpoints
  1014. #
  1015. ##################################
  1016. # TODO: Refactor task API endpoints below into a separate file
  1017. @app.get("/api/task/config")
  1018. async def get_task_config(user=Depends(get_verified_user)):
  1019. return {
  1020. "TASK_MODEL": app.state.config.TASK_MODEL,
  1021. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  1022. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  1023. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  1024. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  1025. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  1026. }
  1027. class TaskConfigForm(BaseModel):
  1028. TASK_MODEL: Optional[str]
  1029. TASK_MODEL_EXTERNAL: Optional[str]
  1030. TITLE_GENERATION_PROMPT_TEMPLATE: str
  1031. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE: str
  1032. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD: int
  1033. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: str
  1034. @app.post("/api/task/config/update")
  1035. async def update_task_config(form_data: TaskConfigForm, user=Depends(get_admin_user)):
  1036. app.state.config.TASK_MODEL = form_data.TASK_MODEL
  1037. app.state.config.TASK_MODEL_EXTERNAL = form_data.TASK_MODEL_EXTERNAL
  1038. app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = (
  1039. form_data.TITLE_GENERATION_PROMPT_TEMPLATE
  1040. )
  1041. app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE = (
  1042. form_data.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  1043. )
  1044. app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD = (
  1045. form_data.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD
  1046. )
  1047. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  1048. form_data.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  1049. )
  1050. return {
  1051. "TASK_MODEL": app.state.config.TASK_MODEL,
  1052. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  1053. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  1054. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  1055. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  1056. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  1057. }
  1058. @app.post("/api/task/title/completions")
  1059. async def generate_title(form_data: dict, user=Depends(get_verified_user)):
  1060. print("generate_title")
  1061. model_id = form_data["model"]
  1062. if model_id not in app.state.MODELS:
  1063. raise HTTPException(
  1064. status_code=status.HTTP_404_NOT_FOUND,
  1065. detail="Model not found",
  1066. )
  1067. # Check if the user has a custom task model
  1068. # If the user has a custom task model, use that model
  1069. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1070. if app.state.config.TASK_MODEL:
  1071. task_model_id = app.state.config.TASK_MODEL
  1072. if task_model_id in app.state.MODELS:
  1073. model_id = task_model_id
  1074. else:
  1075. if app.state.config.TASK_MODEL_EXTERNAL:
  1076. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1077. if task_model_id in app.state.MODELS:
  1078. model_id = task_model_id
  1079. print(model_id)
  1080. model = app.state.MODELS[model_id]
  1081. template = app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE
  1082. content = title_generation_template(
  1083. template,
  1084. form_data["prompt"],
  1085. {
  1086. "name": user.name,
  1087. "location": user.info.get("location") if user.info else None,
  1088. },
  1089. )
  1090. payload = {
  1091. "model": model_id,
  1092. "messages": [{"role": "user", "content": content}],
  1093. "stream": False,
  1094. "max_tokens": 50,
  1095. "chat_id": form_data.get("chat_id", None),
  1096. "title": True,
  1097. }
  1098. log.debug(payload)
  1099. try:
  1100. payload = filter_pipeline(payload, user)
  1101. except Exception as e:
  1102. return JSONResponse(
  1103. status_code=e.args[0],
  1104. content={"detail": e.args[1]},
  1105. )
  1106. if model["owned_by"] == "ollama":
  1107. return await generate_ollama_chat_completion(payload, user=user)
  1108. else:
  1109. return await generate_openai_chat_completion(payload, user=user)
  1110. @app.post("/api/task/query/completions")
  1111. async def generate_search_query(form_data: dict, user=Depends(get_verified_user)):
  1112. print("generate_search_query")
  1113. if len(form_data["prompt"]) < app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD:
  1114. raise HTTPException(
  1115. status_code=status.HTTP_400_BAD_REQUEST,
  1116. detail=f"Skip search query generation for short prompts (< {app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD} characters)",
  1117. )
  1118. model_id = form_data["model"]
  1119. if model_id not in app.state.MODELS:
  1120. raise HTTPException(
  1121. status_code=status.HTTP_404_NOT_FOUND,
  1122. detail="Model not found",
  1123. )
  1124. # Check if the user has a custom task model
  1125. # If the user has a custom task model, use that model
  1126. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1127. if app.state.config.TASK_MODEL:
  1128. task_model_id = app.state.config.TASK_MODEL
  1129. if task_model_id in app.state.MODELS:
  1130. model_id = task_model_id
  1131. else:
  1132. if app.state.config.TASK_MODEL_EXTERNAL:
  1133. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1134. if task_model_id in app.state.MODELS:
  1135. model_id = task_model_id
  1136. print(model_id)
  1137. model = app.state.MODELS[model_id]
  1138. template = app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  1139. content = search_query_generation_template(
  1140. template, form_data["prompt"], {"name": user.name}
  1141. )
  1142. payload = {
  1143. "model": model_id,
  1144. "messages": [{"role": "user", "content": content}],
  1145. "stream": False,
  1146. "max_tokens": 30,
  1147. "task": True,
  1148. }
  1149. print(payload)
  1150. try:
  1151. payload = filter_pipeline(payload, user)
  1152. except Exception as e:
  1153. return JSONResponse(
  1154. status_code=e.args[0],
  1155. content={"detail": e.args[1]},
  1156. )
  1157. if model["owned_by"] == "ollama":
  1158. return await generate_ollama_chat_completion(payload, user=user)
  1159. else:
  1160. return await generate_openai_chat_completion(payload, user=user)
  1161. @app.post("/api/task/emoji/completions")
  1162. async def generate_emoji(form_data: dict, user=Depends(get_verified_user)):
  1163. print("generate_emoji")
  1164. model_id = form_data["model"]
  1165. if model_id not in app.state.MODELS:
  1166. raise HTTPException(
  1167. status_code=status.HTTP_404_NOT_FOUND,
  1168. detail="Model not found",
  1169. )
  1170. # Check if the user has a custom task model
  1171. # If the user has a custom task model, use that model
  1172. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1173. if app.state.config.TASK_MODEL:
  1174. task_model_id = app.state.config.TASK_MODEL
  1175. if task_model_id in app.state.MODELS:
  1176. model_id = task_model_id
  1177. else:
  1178. if app.state.config.TASK_MODEL_EXTERNAL:
  1179. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1180. if task_model_id in app.state.MODELS:
  1181. model_id = task_model_id
  1182. print(model_id)
  1183. model = app.state.MODELS[model_id]
  1184. template = '''
  1185. 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., 😊, 😢, 😡, 😱).
  1186. Message: """{{prompt}}"""
  1187. '''
  1188. content = title_generation_template(
  1189. template,
  1190. form_data["prompt"],
  1191. {
  1192. "name": user.name,
  1193. "location": user.info.get("location") if user.info else None,
  1194. },
  1195. )
  1196. payload = {
  1197. "model": model_id,
  1198. "messages": [{"role": "user", "content": content}],
  1199. "stream": False,
  1200. "max_tokens": 4,
  1201. "chat_id": form_data.get("chat_id", None),
  1202. "task": True,
  1203. }
  1204. log.debug(payload)
  1205. try:
  1206. payload = filter_pipeline(payload, user)
  1207. except Exception as e:
  1208. return JSONResponse(
  1209. status_code=e.args[0],
  1210. content={"detail": e.args[1]},
  1211. )
  1212. if model["owned_by"] == "ollama":
  1213. return await generate_ollama_chat_completion(payload, user=user)
  1214. else:
  1215. return await generate_openai_chat_completion(payload, user=user)
  1216. @app.post("/api/task/tools/completions")
  1217. async def get_tools_function_calling(form_data: dict, user=Depends(get_verified_user)):
  1218. print("get_tools_function_calling")
  1219. model_id = form_data["model"]
  1220. if model_id not in app.state.MODELS:
  1221. raise HTTPException(
  1222. status_code=status.HTTP_404_NOT_FOUND,
  1223. detail="Model not found",
  1224. )
  1225. # Check if the user has a custom task model
  1226. # If the user has a custom task model, use that model
  1227. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1228. if app.state.config.TASK_MODEL:
  1229. task_model_id = app.state.config.TASK_MODEL
  1230. if task_model_id in app.state.MODELS:
  1231. model_id = task_model_id
  1232. else:
  1233. if app.state.config.TASK_MODEL_EXTERNAL:
  1234. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1235. if task_model_id in app.state.MODELS:
  1236. model_id = task_model_id
  1237. print(model_id)
  1238. template = app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  1239. try:
  1240. context, citation, file_handler = await get_function_call_response(
  1241. form_data["messages"],
  1242. form_data.get("files", []),
  1243. form_data["tool_id"],
  1244. template,
  1245. model_id,
  1246. user,
  1247. )
  1248. return context
  1249. except Exception as e:
  1250. return JSONResponse(
  1251. status_code=e.args[0],
  1252. content={"detail": e.args[1]},
  1253. )
  1254. ##################################
  1255. #
  1256. # Pipelines Endpoints
  1257. #
  1258. ##################################
  1259. # TODO: Refactor pipelines API endpoints below into a separate file
  1260. @app.get("/api/pipelines/list")
  1261. async def get_pipelines_list(user=Depends(get_admin_user)):
  1262. responses = await get_openai_models(raw=True)
  1263. print(responses)
  1264. urlIdxs = [
  1265. idx
  1266. for idx, response in enumerate(responses)
  1267. if response != None and "pipelines" in response
  1268. ]
  1269. return {
  1270. "data": [
  1271. {
  1272. "url": openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx],
  1273. "idx": urlIdx,
  1274. }
  1275. for urlIdx in urlIdxs
  1276. ]
  1277. }
  1278. @app.post("/api/pipelines/upload")
  1279. async def upload_pipeline(
  1280. urlIdx: int = Form(...), file: UploadFile = File(...), user=Depends(get_admin_user)
  1281. ):
  1282. print("upload_pipeline", urlIdx, file.filename)
  1283. # Check if the uploaded file is a python file
  1284. if not file.filename.endswith(".py"):
  1285. raise HTTPException(
  1286. status_code=status.HTTP_400_BAD_REQUEST,
  1287. detail="Only Python (.py) files are allowed.",
  1288. )
  1289. upload_folder = f"{CACHE_DIR}/pipelines"
  1290. os.makedirs(upload_folder, exist_ok=True)
  1291. file_path = os.path.join(upload_folder, file.filename)
  1292. try:
  1293. # Save the uploaded file
  1294. with open(file_path, "wb") as buffer:
  1295. shutil.copyfileobj(file.file, buffer)
  1296. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1297. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1298. headers = {"Authorization": f"Bearer {key}"}
  1299. with open(file_path, "rb") as f:
  1300. files = {"file": f}
  1301. r = requests.post(f"{url}/pipelines/upload", headers=headers, files=files)
  1302. r.raise_for_status()
  1303. data = r.json()
  1304. return {**data}
  1305. except Exception as e:
  1306. # Handle connection error here
  1307. print(f"Connection error: {e}")
  1308. detail = "Pipeline not found"
  1309. if r is not None:
  1310. try:
  1311. res = r.json()
  1312. if "detail" in res:
  1313. detail = res["detail"]
  1314. except:
  1315. pass
  1316. raise HTTPException(
  1317. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1318. detail=detail,
  1319. )
  1320. finally:
  1321. # Ensure the file is deleted after the upload is completed or on failure
  1322. if os.path.exists(file_path):
  1323. os.remove(file_path)
  1324. class AddPipelineForm(BaseModel):
  1325. url: str
  1326. urlIdx: int
  1327. @app.post("/api/pipelines/add")
  1328. async def add_pipeline(form_data: AddPipelineForm, user=Depends(get_admin_user)):
  1329. r = None
  1330. try:
  1331. urlIdx = form_data.urlIdx
  1332. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1333. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1334. headers = {"Authorization": f"Bearer {key}"}
  1335. r = requests.post(
  1336. f"{url}/pipelines/add", headers=headers, json={"url": form_data.url}
  1337. )
  1338. r.raise_for_status()
  1339. data = r.json()
  1340. return {**data}
  1341. except Exception as e:
  1342. # Handle connection error here
  1343. print(f"Connection error: {e}")
  1344. detail = "Pipeline not found"
  1345. if r is not None:
  1346. try:
  1347. res = r.json()
  1348. if "detail" in res:
  1349. detail = res["detail"]
  1350. except:
  1351. pass
  1352. raise HTTPException(
  1353. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1354. detail=detail,
  1355. )
  1356. class DeletePipelineForm(BaseModel):
  1357. id: str
  1358. urlIdx: int
  1359. @app.delete("/api/pipelines/delete")
  1360. async def delete_pipeline(form_data: DeletePipelineForm, user=Depends(get_admin_user)):
  1361. r = None
  1362. try:
  1363. urlIdx = form_data.urlIdx
  1364. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1365. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1366. headers = {"Authorization": f"Bearer {key}"}
  1367. r = requests.delete(
  1368. f"{url}/pipelines/delete", headers=headers, json={"id": form_data.id}
  1369. )
  1370. r.raise_for_status()
  1371. data = r.json()
  1372. return {**data}
  1373. except Exception as e:
  1374. # Handle connection error here
  1375. print(f"Connection error: {e}")
  1376. detail = "Pipeline not found"
  1377. if r is not None:
  1378. try:
  1379. res = r.json()
  1380. if "detail" in res:
  1381. detail = res["detail"]
  1382. except:
  1383. pass
  1384. raise HTTPException(
  1385. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1386. detail=detail,
  1387. )
  1388. @app.get("/api/pipelines")
  1389. async def get_pipelines(urlIdx: Optional[int] = None, user=Depends(get_admin_user)):
  1390. r = None
  1391. try:
  1392. urlIdx
  1393. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1394. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1395. headers = {"Authorization": f"Bearer {key}"}
  1396. r = requests.get(f"{url}/pipelines", headers=headers)
  1397. r.raise_for_status()
  1398. data = r.json()
  1399. return {**data}
  1400. except Exception as e:
  1401. # Handle connection error here
  1402. print(f"Connection error: {e}")
  1403. detail = "Pipeline not found"
  1404. if r is not None:
  1405. try:
  1406. res = r.json()
  1407. if "detail" in res:
  1408. detail = res["detail"]
  1409. except:
  1410. pass
  1411. raise HTTPException(
  1412. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1413. detail=detail,
  1414. )
  1415. @app.get("/api/pipelines/{pipeline_id}/valves")
  1416. async def get_pipeline_valves(
  1417. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  1418. ):
  1419. models = await get_all_models()
  1420. r = None
  1421. try:
  1422. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1423. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1424. headers = {"Authorization": f"Bearer {key}"}
  1425. r = requests.get(f"{url}/{pipeline_id}/valves", headers=headers)
  1426. r.raise_for_status()
  1427. data = r.json()
  1428. return {**data}
  1429. except Exception as e:
  1430. # Handle connection error here
  1431. print(f"Connection error: {e}")
  1432. detail = "Pipeline not found"
  1433. if r is not None:
  1434. try:
  1435. res = r.json()
  1436. if "detail" in res:
  1437. detail = res["detail"]
  1438. except:
  1439. pass
  1440. raise HTTPException(
  1441. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1442. detail=detail,
  1443. )
  1444. @app.get("/api/pipelines/{pipeline_id}/valves/spec")
  1445. async def get_pipeline_valves_spec(
  1446. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  1447. ):
  1448. models = await get_all_models()
  1449. r = None
  1450. try:
  1451. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1452. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1453. headers = {"Authorization": f"Bearer {key}"}
  1454. r = requests.get(f"{url}/{pipeline_id}/valves/spec", headers=headers)
  1455. r.raise_for_status()
  1456. data = r.json()
  1457. return {**data}
  1458. except Exception as e:
  1459. # Handle connection error here
  1460. print(f"Connection error: {e}")
  1461. detail = "Pipeline not found"
  1462. if r is not None:
  1463. try:
  1464. res = r.json()
  1465. if "detail" in res:
  1466. detail = res["detail"]
  1467. except:
  1468. pass
  1469. raise HTTPException(
  1470. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1471. detail=detail,
  1472. )
  1473. @app.post("/api/pipelines/{pipeline_id}/valves/update")
  1474. async def update_pipeline_valves(
  1475. urlIdx: Optional[int],
  1476. pipeline_id: str,
  1477. form_data: dict,
  1478. user=Depends(get_admin_user),
  1479. ):
  1480. models = await get_all_models()
  1481. r = None
  1482. try:
  1483. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1484. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1485. headers = {"Authorization": f"Bearer {key}"}
  1486. r = requests.post(
  1487. f"{url}/{pipeline_id}/valves/update",
  1488. headers=headers,
  1489. json={**form_data},
  1490. )
  1491. r.raise_for_status()
  1492. data = r.json()
  1493. return {**data}
  1494. except Exception as e:
  1495. # Handle connection error here
  1496. print(f"Connection error: {e}")
  1497. detail = "Pipeline not found"
  1498. if r is not None:
  1499. try:
  1500. res = r.json()
  1501. if "detail" in res:
  1502. detail = res["detail"]
  1503. except:
  1504. pass
  1505. raise HTTPException(
  1506. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1507. detail=detail,
  1508. )
  1509. ##################################
  1510. #
  1511. # Config Endpoints
  1512. #
  1513. ##################################
  1514. @app.get("/api/config")
  1515. async def get_app_config():
  1516. # Checking and Handling the Absence of 'ui' in CONFIG_DATA
  1517. default_locale = "en-US"
  1518. if "ui" in CONFIG_DATA:
  1519. default_locale = CONFIG_DATA["ui"].get("default_locale", "en-US")
  1520. # The Rest of the Function Now Uses the Variables Defined Above
  1521. return {
  1522. "status": True,
  1523. "name": WEBUI_NAME,
  1524. "version": VERSION,
  1525. "default_locale": default_locale,
  1526. "default_models": webui_app.state.config.DEFAULT_MODELS,
  1527. "default_prompt_suggestions": webui_app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
  1528. "features": {
  1529. "auth": WEBUI_AUTH,
  1530. "auth_trusted_header": bool(webui_app.state.AUTH_TRUSTED_EMAIL_HEADER),
  1531. "enable_signup": webui_app.state.config.ENABLE_SIGNUP,
  1532. "enable_web_search": rag_app.state.config.ENABLE_RAG_WEB_SEARCH,
  1533. "enable_image_generation": images_app.state.config.ENABLED,
  1534. "enable_community_sharing": webui_app.state.config.ENABLE_COMMUNITY_SHARING,
  1535. "enable_admin_export": ENABLE_ADMIN_EXPORT,
  1536. },
  1537. "audio": {
  1538. "tts": {
  1539. "engine": audio_app.state.config.TTS_ENGINE,
  1540. "voice": audio_app.state.config.TTS_VOICE,
  1541. },
  1542. "stt": {
  1543. "engine": audio_app.state.config.STT_ENGINE,
  1544. },
  1545. },
  1546. }
  1547. @app.get("/api/config/model/filter")
  1548. async def get_model_filter_config(user=Depends(get_admin_user)):
  1549. return {
  1550. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1551. "models": app.state.config.MODEL_FILTER_LIST,
  1552. }
  1553. class ModelFilterConfigForm(BaseModel):
  1554. enabled: bool
  1555. models: List[str]
  1556. @app.post("/api/config/model/filter")
  1557. async def update_model_filter_config(
  1558. form_data: ModelFilterConfigForm, user=Depends(get_admin_user)
  1559. ):
  1560. app.state.config.ENABLE_MODEL_FILTER = form_data.enabled
  1561. app.state.config.MODEL_FILTER_LIST = form_data.models
  1562. return {
  1563. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1564. "models": app.state.config.MODEL_FILTER_LIST,
  1565. }
  1566. # TODO: webhook endpoint should be under config endpoints
  1567. @app.get("/api/webhook")
  1568. async def get_webhook_url(user=Depends(get_admin_user)):
  1569. return {
  1570. "url": app.state.config.WEBHOOK_URL,
  1571. }
  1572. class UrlForm(BaseModel):
  1573. url: str
  1574. @app.post("/api/webhook")
  1575. async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)):
  1576. app.state.config.WEBHOOK_URL = form_data.url
  1577. webui_app.state.WEBHOOK_URL = app.state.config.WEBHOOK_URL
  1578. return {"url": app.state.config.WEBHOOK_URL}
  1579. @app.get("/api/version")
  1580. async def get_app_config():
  1581. return {
  1582. "version": VERSION,
  1583. }
  1584. @app.get("/api/changelog")
  1585. async def get_app_changelog():
  1586. return {key: CHANGELOG[key] for idx, key in enumerate(CHANGELOG) if idx < 5}
  1587. @app.get("/api/version/updates")
  1588. async def get_app_latest_release_version():
  1589. try:
  1590. async with aiohttp.ClientSession(trust_env=True) as session:
  1591. async with session.get(
  1592. "https://api.github.com/repos/open-webui/open-webui/releases/latest"
  1593. ) as response:
  1594. response.raise_for_status()
  1595. data = await response.json()
  1596. latest_version = data["tag_name"]
  1597. return {"current": VERSION, "latest": latest_version[1:]}
  1598. except aiohttp.ClientError as e:
  1599. raise HTTPException(
  1600. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  1601. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  1602. )
  1603. @app.get("/manifest.json")
  1604. async def get_manifest_json():
  1605. return {
  1606. "name": WEBUI_NAME,
  1607. "short_name": WEBUI_NAME,
  1608. "start_url": "/",
  1609. "display": "standalone",
  1610. "background_color": "#343541",
  1611. "orientation": "portrait-primary",
  1612. "icons": [{"src": "/static/logo.png", "type": "image/png", "sizes": "500x500"}],
  1613. }
  1614. @app.get("/opensearch.xml")
  1615. async def get_opensearch_xml():
  1616. xml_content = rf"""
  1617. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  1618. <ShortName>{WEBUI_NAME}</ShortName>
  1619. <Description>Search {WEBUI_NAME}</Description>
  1620. <InputEncoding>UTF-8</InputEncoding>
  1621. <Image width="16" height="16" type="image/x-icon">{WEBUI_URL}/favicon.png</Image>
  1622. <Url type="text/html" method="get" template="{WEBUI_URL}/?q={"{searchTerms}"}"/>
  1623. <moz:SearchForm>{WEBUI_URL}</moz:SearchForm>
  1624. </OpenSearchDescription>
  1625. """
  1626. return Response(content=xml_content, media_type="application/xml")
  1627. @app.get("/health")
  1628. async def healthcheck():
  1629. return {"status": True}
  1630. app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
  1631. app.mount("/cache", StaticFiles(directory=CACHE_DIR), name="cache")
  1632. if os.path.exists(FRONTEND_BUILD_DIR):
  1633. mimetypes.add_type("text/javascript", ".js")
  1634. app.mount(
  1635. "/",
  1636. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  1637. name="spa-static-files",
  1638. )
  1639. else:
  1640. log.warning(
  1641. f"Frontend build directory not found at '{FRONTEND_BUILD_DIR}'. Serving API only."
  1642. )