main.py 9.2 KB

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