config.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import os
  2. import chromadb
  3. from chromadb import Settings
  4. from secrets import token_bytes
  5. from base64 import b64encode
  6. from constants import ERROR_MESSAGES
  7. from pathlib import Path
  8. import json
  9. import markdown
  10. from bs4 import BeautifulSoup
  11. try:
  12. from dotenv import load_dotenv, find_dotenv
  13. load_dotenv(find_dotenv("../.env"))
  14. except ImportError:
  15. print("dotenv not installed, skipping...")
  16. ####################################
  17. # ENV (dev,test,prod)
  18. ####################################
  19. ENV = os.environ.get("ENV", "dev")
  20. try:
  21. with open(f"../package.json", "r") as f:
  22. PACKAGE_DATA = json.load(f)
  23. except:
  24. PACKAGE_DATA = {"version": "0.0.0"}
  25. VERSION = PACKAGE_DATA["version"]
  26. # Function to parse each section
  27. def parse_section(section):
  28. items = []
  29. for li in section.find_all("li"):
  30. # Extract raw HTML string
  31. raw_html = str(li)
  32. # Extract text without HTML tags
  33. text = li.get_text(separator=" ", strip=True)
  34. # Split into title and content
  35. parts = text.split(": ", 1)
  36. title = parts[0].strip() if len(parts) > 1 else ""
  37. content = parts[1].strip() if len(parts) > 1 else text
  38. items.append({"title": title, "content": content, "raw": raw_html})
  39. return items
  40. try:
  41. with open("../CHANGELOG.md", "r") as file:
  42. changelog_content = file.read()
  43. except:
  44. changelog_content = ""
  45. # Convert markdown content to HTML
  46. html_content = markdown.markdown(changelog_content)
  47. # Parse the HTML content
  48. soup = BeautifulSoup(html_content, "html.parser")
  49. # Initialize JSON structure
  50. changelog_json = {}
  51. # Iterate over each version
  52. for version in soup.find_all("h2"):
  53. version_number = version.get_text().strip().split(" - ")[0][1:-1] # Remove brackets
  54. date = version.get_text().strip().split(" - ")[1]
  55. version_data = {"date": date}
  56. # Find the next sibling that is a h3 tag (section title)
  57. current = version.find_next_sibling()
  58. print(current)
  59. while current and current.name != "h2":
  60. if current.name == "h3":
  61. section_title = current.get_text().lower() # e.g., "added", "fixed"
  62. section_items = parse_section(current.find_next_sibling("ul"))
  63. version_data[section_title] = section_items
  64. # Move to the next element
  65. current = current.find_next_sibling()
  66. changelog_json[version_number] = version_data
  67. CHANGELOG = changelog_json
  68. ####################################
  69. # DATA/FRONTEND BUILD DIR
  70. ####################################
  71. DATA_DIR = str(Path(os.getenv("DATA_DIR", "./data")).resolve())
  72. FRONTEND_BUILD_DIR = str(Path(os.getenv("FRONTEND_BUILD_DIR", "../build")))
  73. try:
  74. with open(f"{DATA_DIR}/config.json", "r") as f:
  75. CONFIG_DATA = json.load(f)
  76. except:
  77. CONFIG_DATA = {}
  78. ####################################
  79. # File Upload DIR
  80. ####################################
  81. UPLOAD_DIR = f"{DATA_DIR}/uploads"
  82. Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)
  83. ####################################
  84. # Cache DIR
  85. ####################################
  86. CACHE_DIR = f"{DATA_DIR}/cache"
  87. Path(CACHE_DIR).mkdir(parents=True, exist_ok=True)
  88. ####################################
  89. # Docs DIR
  90. ####################################
  91. DOCS_DIR = f"{DATA_DIR}/docs"
  92. Path(DOCS_DIR).mkdir(parents=True, exist_ok=True)
  93. ####################################
  94. # OLLAMA_API_BASE_URL
  95. ####################################
  96. OLLAMA_API_BASE_URL = os.environ.get(
  97. "OLLAMA_API_BASE_URL", "http://localhost:11434/api"
  98. )
  99. if ENV == "prod":
  100. if OLLAMA_API_BASE_URL == "/ollama/api":
  101. OLLAMA_API_BASE_URL = "http://host.docker.internal:11434/api"
  102. ####################################
  103. # OPENAI_API
  104. ####################################
  105. OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
  106. OPENAI_API_BASE_URL = os.environ.get("OPENAI_API_BASE_URL", "")
  107. if OPENAI_API_BASE_URL == "":
  108. OPENAI_API_BASE_URL = "https://api.openai.com/v1"
  109. ####################################
  110. # WEBUI
  111. ####################################
  112. ENABLE_SIGNUP = os.environ.get("ENABLE_SIGNUP", True)
  113. DEFAULT_MODELS = os.environ.get("DEFAULT_MODELS", None)
  114. DEFAULT_PROMPT_SUGGESTIONS = (
  115. CONFIG_DATA["ui"]["prompt_suggestions"]
  116. if "ui" in CONFIG_DATA
  117. and "prompt_suggestions" in CONFIG_DATA["ui"]
  118. and type(CONFIG_DATA["ui"]["prompt_suggestions"]) is list
  119. else [
  120. {
  121. "title": ["Help me study", "vocabulary for a college entrance exam"],
  122. "content": "Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option.",
  123. },
  124. {
  125. "title": ["Give me ideas", "for what to do with my kids' art"],
  126. "content": "What are 5 creative things I could do with my kids' art? I don't want to throw them away, but it's also so much clutter.",
  127. },
  128. {
  129. "title": ["Tell me a fun fact", "about the Roman Empire"],
  130. "content": "Tell me a random fun fact about the Roman Empire",
  131. },
  132. {
  133. "title": ["Show me a code snippet", "of a website's sticky header"],
  134. "content": "Show me a code snippet of a website's sticky header in CSS and JavaScript.",
  135. },
  136. ]
  137. )
  138. DEFAULT_USER_ROLE = os.getenv("DEFAULT_USER_ROLE", "pending")
  139. USER_PERMISSIONS = {"chat": {"deletion": True}}
  140. ####################################
  141. # WEBUI_VERSION
  142. ####################################
  143. WEBUI_VERSION = os.environ.get("WEBUI_VERSION", "v1.0.0-alpha.100")
  144. ####################################
  145. # WEBUI_AUTH (Required for security)
  146. ####################################
  147. WEBUI_AUTH = True
  148. ####################################
  149. # WEBUI_SECRET_KEY
  150. ####################################
  151. WEBUI_SECRET_KEY = os.environ.get(
  152. "WEBUI_SECRET_KEY",
  153. os.environ.get(
  154. "WEBUI_JWT_SECRET_KEY", "t0p-s3cr3t"
  155. ), # DEPRECATED: remove at next major version
  156. )
  157. if WEBUI_AUTH and WEBUI_SECRET_KEY == "":
  158. raise ValueError(ERROR_MESSAGES.ENV_VAR_NOT_FOUND)
  159. ####################################
  160. # RAG
  161. ####################################
  162. CHROMA_DATA_PATH = f"{DATA_DIR}/vector_db"
  163. # this uses the model defined in the Dockerfile ENV variable. If you dont use docker or docker based deployments such as k8s, the default embedding model will be used (all-MiniLM-L6-v2)
  164. RAG_EMBEDDING_MODEL = os.environ.get("RAG_EMBEDDING_MODEL", "all-MiniLM-L6-v2")
  165. # device type ebbeding models - "cpu" (default), "cuda" (nvidia gpu required) or "mps" (apple silicon) - choosing this right can lead to better performance
  166. RAG_EMBEDDING_MODEL_DEVICE_TYPE = os.environ.get(
  167. "RAG_EMBEDDING_MODEL_DEVICE_TYPE", "cpu"
  168. )
  169. CHROMA_CLIENT = chromadb.PersistentClient(
  170. path=CHROMA_DATA_PATH,
  171. settings=Settings(allow_reset=True, anonymized_telemetry=False),
  172. )
  173. CHUNK_SIZE = 1500
  174. CHUNK_OVERLAP = 100
  175. RAG_TEMPLATE = """Use the following context as your learned knowledge, inside <context></context> XML tags.
  176. <context>
  177. [context]
  178. </context>
  179. When answer to user:
  180. - If you don't know, just say that you don't know.
  181. - If you don't know when you are not sure, ask for clarification.
  182. Avoid mentioning that you obtained the information from the context.
  183. And answer according to the language of the user's question.
  184. Given the context information, answer the query.
  185. Query: [query]"""
  186. ####################################
  187. # Transcribe
  188. ####################################
  189. WHISPER_MODEL = os.getenv("WHISPER_MODEL", "base")
  190. WHISPER_MODEL_DIR = os.getenv("WHISPER_MODEL_DIR", f"{CACHE_DIR}/whisper/models")
  191. ####################################
  192. # Images
  193. ####################################
  194. AUTOMATIC1111_BASE_URL = os.getenv("AUTOMATIC1111_BASE_URL", "")