config.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. try:
  10. from dotenv import load_dotenv, find_dotenv
  11. load_dotenv(find_dotenv("../.env"))
  12. except ImportError:
  13. print("dotenv not installed, skipping...")
  14. ####################################
  15. # ENV (dev,test,prod)
  16. ####################################
  17. ENV = os.environ.get("ENV", "dev")
  18. try:
  19. with open(f"../package.json", "r") as f:
  20. PACKAGE_DATA = json.load(f)
  21. except:
  22. PACKAGE_DATA = {"version": "0.0.0"}
  23. VERSION = PACKAGE_DATA["version"]
  24. ####################################
  25. # DATA/FRONTEND BUILD DIR
  26. ####################################
  27. DATA_DIR = str(Path(os.getenv("DATA_DIR", "./data")).resolve())
  28. FRONTEND_BUILD_DIR = str(Path(os.getenv("FRONTEND_BUILD_DIR", "../build")))
  29. try:
  30. with open(f"{DATA_DIR}/config.json", "r") as f:
  31. CONFIG_DATA = json.load(f)
  32. except:
  33. CONFIG_DATA = {}
  34. ####################################
  35. # File Upload DIR
  36. ####################################
  37. UPLOAD_DIR = f"{DATA_DIR}/uploads"
  38. Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)
  39. ####################################
  40. # Cache DIR
  41. ####################################
  42. CACHE_DIR = f"{DATA_DIR}/cache"
  43. Path(CACHE_DIR).mkdir(parents=True, exist_ok=True)
  44. ####################################
  45. # Docs DIR
  46. ####################################
  47. DOCS_DIR = f"{DATA_DIR}/docs"
  48. Path(DOCS_DIR).mkdir(parents=True, exist_ok=True)
  49. ####################################
  50. # OLLAMA_API_BASE_URL
  51. ####################################
  52. OLLAMA_API_BASE_URL = os.environ.get(
  53. "OLLAMA_API_BASE_URL", "http://localhost:11434/api"
  54. )
  55. if ENV == "prod":
  56. if OLLAMA_API_BASE_URL == "/ollama/api":
  57. OLLAMA_API_BASE_URL = "http://host.docker.internal:11434/api"
  58. ####################################
  59. # OPENAI_API
  60. ####################################
  61. OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
  62. OPENAI_API_BASE_URL = os.environ.get("OPENAI_API_BASE_URL", "")
  63. if OPENAI_API_BASE_URL == "":
  64. OPENAI_API_BASE_URL = "https://api.openai.com/v1"
  65. ####################################
  66. # WEBUI
  67. ####################################
  68. ENABLE_SIGNUP = os.environ.get("ENABLE_SIGNUP", True)
  69. DEFAULT_MODELS = os.environ.get("DEFAULT_MODELS", None)
  70. DEFAULT_PROMPT_SUGGESTIONS = (
  71. CONFIG_DATA["ui"]["prompt_suggestions"]
  72. if "ui" in CONFIG_DATA
  73. and "prompt_suggestions" in CONFIG_DATA["ui"]
  74. and type(CONFIG_DATA["ui"]["prompt_suggestions"]) is list
  75. else [
  76. {
  77. "title": ["Help me study", "vocabulary for a college entrance exam"],
  78. "content": "Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option.",
  79. },
  80. {
  81. "title": ["Give me ideas", "for what to do with my kids' art"],
  82. "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.",
  83. },
  84. {
  85. "title": ["Tell me a fun fact", "about the Roman Empire"],
  86. "content": "Tell me a random fun fact about the Roman Empire",
  87. },
  88. {
  89. "title": ["Show me a code snippet", "of a website's sticky header"],
  90. "content": "Show me a code snippet of a website's sticky header in CSS and JavaScript.",
  91. },
  92. ]
  93. )
  94. DEFAULT_USER_ROLE = "pending"
  95. USER_PERMISSIONS = {"chat": {"deletion": True}}
  96. ####################################
  97. # WEBUI_VERSION
  98. ####################################
  99. WEBUI_VERSION = os.environ.get("WEBUI_VERSION", "v1.0.0-alpha.100")
  100. ####################################
  101. # WEBUI_AUTH (Required for security)
  102. ####################################
  103. WEBUI_AUTH = True
  104. ####################################
  105. # WEBUI_SECRET_KEY
  106. ####################################
  107. WEBUI_SECRET_KEY = os.environ.get(
  108. "WEBUI_SECRET_KEY",
  109. os.environ.get(
  110. "WEBUI_JWT_SECRET_KEY", "t0p-s3cr3t"
  111. ), # DEPRECATED: remove at next major version
  112. )
  113. if WEBUI_AUTH and WEBUI_SECRET_KEY == "":
  114. raise ValueError(ERROR_MESSAGES.ENV_VAR_NOT_FOUND)
  115. ####################################
  116. # RAG
  117. ####################################
  118. CHROMA_DATA_PATH = f"{DATA_DIR}/vector_db"
  119. # 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)
  120. RAG_EMBEDDING_MODEL = os.environ.get("RAG_EMBEDDING_MODEL", "all-MiniLM-L6-v2")
  121. # device type ebbeding models - "cpu" (default), "cuda" (nvidia gpu required) or "mps" (apple silicon) - choosing this right can lead to better performance
  122. RAG_EMBEDDING_MODEL_DEVICE_TYPE = os.environ.get(
  123. "RAG_EMBEDDING_MODEL_DEVICE_TYPE", "cpu"
  124. )
  125. CHROMA_CLIENT = chromadb.PersistentClient(
  126. path=CHROMA_DATA_PATH,
  127. settings=Settings(allow_reset=True, anonymized_telemetry=False),
  128. )
  129. CHUNK_SIZE = 1500
  130. CHUNK_OVERLAP = 100
  131. RAG_TEMPLATE = """Use the following context as your learned knowledge, inside <context></context> XML tags.
  132. <context>
  133. [context]
  134. </context>
  135. When answer to user:
  136. - If you don't know, just say that you don't know.
  137. - If you don't know when you are not sure, ask for clarification.
  138. Avoid mentioning that you obtained the information from the context.
  139. And answer according to the language of the user's question.
  140. Given the context information, answer the query.
  141. Query: [query]"""
  142. ####################################
  143. # Transcribe
  144. ####################################
  145. WHISPER_MODEL = os.getenv("WHISPER_MODEL", "base")
  146. WHISPER_MODEL_DIR = os.getenv("WHISPER_MODEL_DIR", f"{CACHE_DIR}/whisper/models")
  147. ####################################
  148. # Images
  149. ####################################
  150. AUTOMATIC1111_BASE_URL = os.getenv("AUTOMATIC1111_BASE_URL", "")