main.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 apps.rag.utils import query_doc, query_collection, rag_template
  23. from config import WEBUI_NAME, ENV, VERSION, CHANGELOG, FRONTEND_BUILD_DIR
  24. from constants import ERROR_MESSAGES
  25. class SPAStaticFiles(StaticFiles):
  26. async def get_response(self, path: str, scope):
  27. try:
  28. return await super().get_response(path, scope)
  29. except (HTTPException, StarletteHTTPException) as ex:
  30. if ex.status_code == 404:
  31. return await super().get_response("index.html", scope)
  32. else:
  33. raise ex
  34. app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
  35. origins = ["*"]
  36. app.add_middleware(
  37. CORSMiddleware,
  38. allow_origins=origins,
  39. allow_credentials=True,
  40. allow_methods=["*"],
  41. allow_headers=["*"],
  42. )
  43. @app.on_event("startup")
  44. async def on_startup():
  45. await litellm_app_startup()
  46. class RAGMiddleware(BaseHTTPMiddleware):
  47. async def dispatch(self, request: Request, call_next):
  48. "chat/completions" in request.url.path
  49. print(request.url.path)
  50. if request.method == "POST" and (
  51. "/api/chat" in request.url.path or "/chat/completions" in request.url.path
  52. ):
  53. # Read the original request body
  54. body = await request.body()
  55. # Decode body to string
  56. body_str = body.decode("utf-8")
  57. # Parse string to JSON
  58. data = json.loads(body_str) if body_str else {}
  59. # Example: Add a new key-value pair or modify existing ones
  60. # data["modified"] = True # Example modification
  61. if "docs" in data:
  62. docs = data["docs"]
  63. print(docs)
  64. last_user_message_idx = None
  65. for i in range(len(data["messages"]) - 1, -1, -1):
  66. if data["messages"][i]["role"] == "user":
  67. last_user_message_idx = i
  68. break
  69. query = data["messages"][last_user_message_idx]["content"]
  70. relevant_contexts = []
  71. for doc in docs:
  72. context = None
  73. if doc["type"] == "collection":
  74. context = query_collection(
  75. collection_names=doc["collection_names"],
  76. query=query,
  77. k=rag_app.state.TOP_K,
  78. embedding_function=rag_app.state.sentence_transformer_ef,
  79. )
  80. else:
  81. context = query_doc(
  82. collection_name=doc["collection_name"],
  83. query=query,
  84. k=rag_app.state.TOP_K,
  85. embedding_function=rag_app.state.sentence_transformer_ef,
  86. )
  87. relevant_contexts.append(context)
  88. context_string = ""
  89. for context in relevant_contexts:
  90. if context:
  91. context_string += " ".join(context["documents"][0]) + "\n"
  92. content = rag_template(
  93. template=rag_app.state.RAG_TEMPLATE,
  94. context=context_string,
  95. query=query,
  96. )
  97. new_user_message = {
  98. **data["messages"][last_user_message_idx],
  99. "content": content,
  100. }
  101. data["messages"][last_user_message_idx] = new_user_message
  102. del data["docs"]
  103. modified_body_bytes = json.dumps(data).encode("utf-8")
  104. # Create a new request with the modified body
  105. scope = request.scope
  106. scope["body"] = modified_body_bytes
  107. request = Request(scope, receive=lambda: self._receive(modified_body_bytes))
  108. response = await call_next(request)
  109. return response
  110. async def _receive(self, body: bytes):
  111. return {"type": "http.request", "body": body, "more_body": False}
  112. app.add_middleware(RAGMiddleware)
  113. @app.middleware("http")
  114. async def check_url(request: Request, call_next):
  115. start_time = int(time.time())
  116. response = await call_next(request)
  117. process_time = int(time.time()) - start_time
  118. response.headers["X-Process-Time"] = str(process_time)
  119. return response
  120. app.mount("/api/v1", webui_app)
  121. app.mount("/litellm/api", litellm_app)
  122. app.mount("/ollama", ollama_app)
  123. app.mount("/openai/api", openai_app)
  124. app.mount("/images/api/v1", images_app)
  125. app.mount("/audio/api/v1", audio_app)
  126. app.mount("/rag/api/v1", rag_app)
  127. @app.get("/api/config")
  128. async def get_app_config():
  129. return {
  130. "status": True,
  131. "name": WEBUI_NAME,
  132. "version": VERSION,
  133. "images": images_app.state.ENABLED,
  134. "default_models": webui_app.state.DEFAULT_MODELS,
  135. "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS,
  136. }
  137. @app.get("/api/version")
  138. async def get_app_config():
  139. return {
  140. "version": VERSION,
  141. }
  142. @app.get("/api/changelog")
  143. async def get_app_changelog():
  144. return CHANGELOG
  145. @app.get("/api/version/updates")
  146. async def get_app_latest_release_version():
  147. try:
  148. response = requests.get(
  149. f"https://api.github.com/repos/open-webui/open-webui/releases/latest"
  150. )
  151. response.raise_for_status()
  152. latest_version = response.json()["tag_name"]
  153. return {"current": VERSION, "latest": latest_version[1:]}
  154. except Exception as e:
  155. raise HTTPException(
  156. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  157. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  158. )
  159. app.mount("/static", StaticFiles(directory="static"), name="static")
  160. app.mount("/cache", StaticFiles(directory="data/cache"), name="cache")
  161. app.mount(
  162. "/",
  163. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  164. name="spa-static-files",
  165. )