config.py 5.2 KB

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