main.py 63 KB

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