main.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. from bs4 import BeautifulSoup
  2. import json
  3. import markdown
  4. import time
  5. import os
  6. import sys
  7. import requests
  8. from fastapi import FastAPI, Request, Depends, status
  9. from fastapi.staticfiles import StaticFiles
  10. from fastapi import HTTPException
  11. from fastapi.middleware.wsgi import WSGIMiddleware
  12. from fastapi.middleware.cors import CORSMiddleware
  13. from starlette.exceptions import HTTPException as StarletteHTTPException
  14. from starlette.middleware.base import BaseHTTPMiddleware
  15. from apps.ollama.main import app as ollama_app
  16. from apps.openai.main import app as openai_app
  17. from apps.litellm.main import app as litellm_app, startup as litellm_app_startup
  18. from apps.audio.main import app as audio_app
  19. from apps.images.main import app as images_app
  20. from apps.rag.main import app as rag_app
  21. from apps.web.main import app as webui_app
  22. from pydantic import BaseModel
  23. from typing import List
  24. from utils.utils import get_admin_user
  25. from apps.rag.utils import query_doc, query_collection, rag_template
  26. from config import (
  27. WEBUI_NAME,
  28. ENV,
  29. VERSION,
  30. CHANGELOG,
  31. FRONTEND_BUILD_DIR,
  32. MODEL_FILTER_ENABLED,
  33. MODEL_FILTER_LIST,
  34. )
  35. from constants import ERROR_MESSAGES
  36. class SPAStaticFiles(StaticFiles):
  37. async def get_response(self, path: str, scope):
  38. try:
  39. return await super().get_response(path, scope)
  40. except (HTTPException, StarletteHTTPException) as ex:
  41. if ex.status_code == 404:
  42. return await super().get_response("index.html", scope)
  43. else:
  44. raise ex
  45. app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
  46. app.state.MODEL_FILTER_ENABLED = MODEL_FILTER_ENABLED
  47. app.state.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  48. origins = ["*"]
  49. app.add_middleware(
  50. CORSMiddleware,
  51. allow_origins=origins,
  52. allow_credentials=True,
  53. allow_methods=["*"],
  54. allow_headers=["*"],
  55. )
  56. @app.on_event("startup")
  57. async def on_startup():
  58. await litellm_app_startup()
  59. class RAGMiddleware(BaseHTTPMiddleware):
  60. async def dispatch(self, request: Request, call_next):
  61. if request.method == "POST" and (
  62. "/api/chat" in request.url.path or "/chat/completions" in request.url.path
  63. ):
  64. print(request.url.path)
  65. # Read the original request body
  66. body = await request.body()
  67. # Decode body to string
  68. body_str = body.decode("utf-8")
  69. # Parse string to JSON
  70. data = json.loads(body_str) if body_str else {}
  71. # Example: Add a new key-value pair or modify existing ones
  72. # data["modified"] = True # Example modification
  73. if "docs" in data:
  74. docs = data["docs"]
  75. print(docs)
  76. last_user_message_idx = None
  77. for i in range(len(data["messages"]) - 1, -1, -1):
  78. if data["messages"][i]["role"] == "user":
  79. last_user_message_idx = i
  80. break
  81. user_message = data["messages"][last_user_message_idx]
  82. if isinstance(user_message["content"], list):
  83. # Handle list content input
  84. content_type = "list"
  85. query = ""
  86. for content_item in user_message["content"]:
  87. if content_item["type"] == "text":
  88. query = content_item["text"]
  89. break
  90. elif isinstance(user_message["content"], str):
  91. # Handle text content input
  92. content_type = "text"
  93. query = user_message["content"]
  94. else:
  95. # Fallback in case the input does not match expected types
  96. content_type = None
  97. query = ""
  98. relevant_contexts = []
  99. for doc in docs:
  100. context = None
  101. try:
  102. if doc["type"] == "collection":
  103. context = query_collection(
  104. collection_names=doc["collection_names"],
  105. query=query,
  106. k=rag_app.state.TOP_K,
  107. embedding_function=rag_app.state.sentence_transformer_ef,
  108. )
  109. else:
  110. context = query_doc(
  111. collection_name=doc["collection_name"],
  112. query=query,
  113. k=rag_app.state.TOP_K,
  114. embedding_function=rag_app.state.sentence_transformer_ef,
  115. )
  116. except Exception as e:
  117. print(e)
  118. context = None
  119. relevant_contexts.append(context)
  120. context_string = ""
  121. for context in relevant_contexts:
  122. if context:
  123. context_string += " ".join(context["documents"][0]) + "\n"
  124. ra_content = rag_template(
  125. template=rag_app.state.RAG_TEMPLATE,
  126. context=context_string,
  127. query=query,
  128. )
  129. if content_type == "list":
  130. new_content = []
  131. for content_item in user_message["content"]:
  132. if content_item["type"] == "text":
  133. # Update the text item's content with ra_content
  134. new_content.append({"type": "text", "text": ra_content})
  135. else:
  136. # Keep other types of content as they are
  137. new_content.append(content_item)
  138. new_user_message = {**user_message, "content": new_content}
  139. else:
  140. new_user_message = {
  141. **user_message,
  142. "content": ra_content,
  143. }
  144. data["messages"][last_user_message_idx] = new_user_message
  145. del data["docs"]
  146. print(data["messages"])
  147. modified_body_bytes = json.dumps(data).encode("utf-8")
  148. # Create a new request with the modified body
  149. scope = request.scope
  150. scope["body"] = modified_body_bytes
  151. request = Request(scope, receive=lambda: self._receive(modified_body_bytes))
  152. response = await call_next(request)
  153. return response
  154. async def _receive(self, body: bytes):
  155. return {"type": "http.request", "body": body, "more_body": False}
  156. app.add_middleware(RAGMiddleware)
  157. @app.middleware("http")
  158. async def check_url(request: Request, call_next):
  159. start_time = int(time.time())
  160. response = await call_next(request)
  161. process_time = int(time.time()) - start_time
  162. response.headers["X-Process-Time"] = str(process_time)
  163. return response
  164. app.mount("/api/v1", webui_app)
  165. app.mount("/litellm/api", litellm_app)
  166. app.mount("/ollama", ollama_app)
  167. app.mount("/openai/api", openai_app)
  168. app.mount("/images/api/v1", images_app)
  169. app.mount("/audio/api/v1", audio_app)
  170. app.mount("/rag/api/v1", rag_app)
  171. @app.get("/api/config")
  172. async def get_app_config():
  173. return {
  174. "status": True,
  175. "name": WEBUI_NAME,
  176. "version": VERSION,
  177. "images": images_app.state.ENABLED,
  178. "default_models": webui_app.state.DEFAULT_MODELS,
  179. "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS,
  180. }
  181. @app.get("/api/config/model/filter")
  182. async def get_model_filter_config(user=Depends(get_admin_user)):
  183. return {
  184. "enabled": app.state.MODEL_FILTER_ENABLED,
  185. "models": app.state.MODEL_FILTER_LIST,
  186. }
  187. class ModelFilterConfigForm(BaseModel):
  188. enabled: bool
  189. models: List[str]
  190. @app.post("/api/config/model/filter")
  191. async def get_model_filter_config(
  192. form_data: ModelFilterConfigForm, user=Depends(get_admin_user)
  193. ):
  194. app.state.MODEL_FILTER_ENABLED = form_data.enabled
  195. app.state.MODEL_FILTER_LIST = form_data.models
  196. ollama_app.state.MODEL_FILTER_ENABLED = app.state.MODEL_FILTER_ENABLED
  197. ollama_app.state.MODEL_FILTER_LIST = app.state.MODEL_FILTER_LIST
  198. openai_app.state.MODEL_FILTER_ENABLED = app.state.MODEL_FILTER_ENABLED
  199. openai_app.state.MODEL_FILTER_LIST = app.state.MODEL_FILTER_LIST
  200. return {
  201. "enabled": app.state.MODEL_FILTER_ENABLED,
  202. "models": app.state.MODEL_FILTER_LIST,
  203. }
  204. @app.get("/api/version")
  205. async def get_app_config():
  206. return {
  207. "version": VERSION,
  208. }
  209. @app.get("/api/changelog")
  210. async def get_app_changelog():
  211. return CHANGELOG
  212. @app.get("/api/version/updates")
  213. async def get_app_latest_release_version():
  214. try:
  215. response = requests.get(
  216. f"https://api.github.com/repos/open-webui/open-webui/releases/latest"
  217. )
  218. response.raise_for_status()
  219. latest_version = response.json()["tag_name"]
  220. return {"current": VERSION, "latest": latest_version[1:]}
  221. except Exception as e:
  222. raise HTTPException(
  223. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  224. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  225. )
  226. app.mount("/static", StaticFiles(directory="static"), name="static")
  227. app.mount("/cache", StaticFiles(directory="data/cache"), name="cache")
  228. app.mount(
  229. "/",
  230. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  231. name="spa-static-files",
  232. )