__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. from typing import Optional
  2. from pydantic import AliasChoices, Field, NonNegativeInt, PositiveInt, computed_field
  3. from pydantic_settings import BaseSettings
  4. from configs.feature.hosted_service import HostedServiceConfig
  5. class SecurityConfig(BaseSettings):
  6. """
  7. Secret Key configs
  8. """
  9. SECRET_KEY: Optional[str] = Field(
  10. description='Your App secret key will be used for securely signing the session cookie'
  11. 'Make sure you are changing this key for your deployment with a strong key.'
  12. 'You can generate a strong key using `openssl rand -base64 42`.'
  13. 'Alternatively you can set it with `SECRET_KEY` environment variable.',
  14. default=None,
  15. )
  16. RESET_PASSWORD_TOKEN_EXPIRY_HOURS: PositiveInt = Field(
  17. description='Expiry time in hours for reset token',
  18. default=24,
  19. )
  20. class AppExecutionConfig(BaseSettings):
  21. """
  22. App Execution configs
  23. """
  24. APP_MAX_EXECUTION_TIME: PositiveInt = Field(
  25. description='execution timeout in seconds for app execution',
  26. default=1200,
  27. )
  28. APP_MAX_ACTIVE_REQUESTS: NonNegativeInt = Field(
  29. description='max active request per app, 0 means unlimited',
  30. default=0,
  31. )
  32. class CodeExecutionSandboxConfig(BaseSettings):
  33. """
  34. Code Execution Sandbox configs
  35. """
  36. CODE_EXECUTION_ENDPOINT: str = Field(
  37. description='endpoint URL of code execution servcie',
  38. default='http://sandbox:8194',
  39. )
  40. CODE_EXECUTION_API_KEY: str = Field(
  41. description='API key for code execution service',
  42. default='dify-sandbox',
  43. )
  44. class EndpointConfig(BaseSettings):
  45. """
  46. Module URL configs
  47. """
  48. CONSOLE_API_URL: str = Field(
  49. description='The backend URL prefix of the console API.'
  50. 'used to concatenate the login authorization callback or notion integration callback.',
  51. default='',
  52. )
  53. CONSOLE_WEB_URL: str = Field(
  54. description='The front-end URL prefix of the console web.'
  55. 'used to concatenate some front-end addresses and for CORS configuration use.',
  56. default='',
  57. )
  58. SERVICE_API_URL: str = Field(
  59. description='Service API Url prefix.'
  60. 'used to display Service API Base Url to the front-end.',
  61. default='',
  62. )
  63. APP_WEB_URL: str = Field(
  64. description='WebApp Url prefix.'
  65. 'used to display WebAPP API Base Url to the front-end.',
  66. default='',
  67. )
  68. class FileAccessConfig(BaseSettings):
  69. """
  70. File Access configs
  71. """
  72. FILES_URL: str = Field(
  73. description='File preview or download Url prefix.'
  74. ' used to display File preview or download Url to the front-end or as Multi-model inputs;'
  75. 'Url is signed and has expiration time.',
  76. validation_alias=AliasChoices('FILES_URL', 'CONSOLE_API_URL'),
  77. alias_priority=1,
  78. default='',
  79. )
  80. FILES_ACCESS_TIMEOUT: int = Field(
  81. description='timeout in seconds for file accessing',
  82. default=300,
  83. )
  84. class FileUploadConfig(BaseSettings):
  85. """
  86. File Uploading configs
  87. """
  88. UPLOAD_FILE_SIZE_LIMIT: NonNegativeInt = Field(
  89. description='size limit in Megabytes for uploading files',
  90. default=15,
  91. )
  92. UPLOAD_FILE_BATCH_LIMIT: NonNegativeInt = Field(
  93. description='batch size limit for uploading files',
  94. default=5,
  95. )
  96. UPLOAD_IMAGE_FILE_SIZE_LIMIT: NonNegativeInt = Field(
  97. description='image file size limit in Megabytes for uploading files',
  98. default=10,
  99. )
  100. BATCH_UPLOAD_LIMIT: NonNegativeInt = Field(
  101. description='', # todo: to be clarified
  102. default=20,
  103. )
  104. class HttpConfig(BaseSettings):
  105. """
  106. HTTP configs
  107. """
  108. API_COMPRESSION_ENABLED: bool = Field(
  109. description='whether to enable HTTP response compression of gzip',
  110. default=False,
  111. )
  112. inner_CONSOLE_CORS_ALLOW_ORIGINS: str = Field(
  113. description='',
  114. validation_alias=AliasChoices('CONSOLE_CORS_ALLOW_ORIGINS', 'CONSOLE_WEB_URL'),
  115. default='',
  116. )
  117. @computed_field
  118. @property
  119. def CONSOLE_CORS_ALLOW_ORIGINS(self) -> list[str]:
  120. return self.inner_CONSOLE_CORS_ALLOW_ORIGINS.split(',')
  121. inner_WEB_API_CORS_ALLOW_ORIGINS: str = Field(
  122. description='',
  123. validation_alias=AliasChoices('WEB_API_CORS_ALLOW_ORIGINS'),
  124. default='*',
  125. )
  126. @computed_field
  127. @property
  128. def WEB_API_CORS_ALLOW_ORIGINS(self) -> list[str]:
  129. return self.inner_WEB_API_CORS_ALLOW_ORIGINS.split(',')
  130. class InnerAPIConfig(BaseSettings):
  131. """
  132. Inner API configs
  133. """
  134. INNER_API: bool = Field(
  135. description='whether to enable the inner API',
  136. default=False,
  137. )
  138. INNER_API_KEY: Optional[str] = Field(
  139. description='The inner API key is used to authenticate the inner API',
  140. default=None,
  141. )
  142. class LoggingConfig(BaseSettings):
  143. """
  144. Logging configs
  145. """
  146. LOG_LEVEL: str = Field(
  147. description='Log output level, default to INFO.'
  148. 'It is recommended to set it to ERROR for production.',
  149. default='INFO',
  150. )
  151. LOG_FILE: Optional[str] = Field(
  152. description='logging output file path',
  153. default=None,
  154. )
  155. LOG_FORMAT: str = Field(
  156. description='log format',
  157. default='%(asctime)s.%(msecs)03d %(levelname)s [%(threadName)s] [%(filename)s:%(lineno)d] - %(message)s',
  158. )
  159. LOG_DATEFORMAT: Optional[str] = Field(
  160. description='log date format',
  161. default=None,
  162. )
  163. LOG_TZ: Optional[str] = Field(
  164. description='specify log timezone, eg: America/New_York',
  165. default=None,
  166. )
  167. class ModelLoadBalanceConfig(BaseSettings):
  168. """
  169. Model load balance configs
  170. """
  171. MODEL_LB_ENABLED: bool = Field(
  172. description='whether to enable model load balancing',
  173. default=False,
  174. )
  175. class BillingConfig(BaseSettings):
  176. """
  177. Platform Billing Configurations
  178. """
  179. BILLING_ENABLED: bool = Field(
  180. description='whether to enable billing',
  181. default=False,
  182. )
  183. class UpdateConfig(BaseSettings):
  184. """
  185. Update configs
  186. """
  187. CHECK_UPDATE_URL: str = Field(
  188. description='url for checking updates',
  189. default='https://updates.dify.ai',
  190. )
  191. class WorkflowConfig(BaseSettings):
  192. """
  193. Workflow feature configs
  194. """
  195. WORKFLOW_MAX_EXECUTION_STEPS: PositiveInt = Field(
  196. description='max execution steps in single workflow execution',
  197. default=500,
  198. )
  199. WORKFLOW_MAX_EXECUTION_TIME: PositiveInt = Field(
  200. description='max execution time in seconds in single workflow execution',
  201. default=1200,
  202. )
  203. WORKFLOW_CALL_MAX_DEPTH: PositiveInt = Field(
  204. description='max depth of calling in single workflow execution',
  205. default=5,
  206. )
  207. class OAuthConfig(BaseSettings):
  208. """
  209. oauth configs
  210. """
  211. OAUTH_REDIRECT_PATH: str = Field(
  212. description='redirect path for OAuth',
  213. default='/console/api/oauth/authorize',
  214. )
  215. GITHUB_CLIENT_ID: Optional[str] = Field(
  216. description='GitHub client id for OAuth',
  217. default=None,
  218. )
  219. GITHUB_CLIENT_SECRET: Optional[str] = Field(
  220. description='GitHub client secret key for OAuth',
  221. default=None,
  222. )
  223. GOOGLE_CLIENT_ID: Optional[str] = Field(
  224. description='Google client id for OAuth',
  225. default=None,
  226. )
  227. GOOGLE_CLIENT_SECRET: Optional[str] = Field(
  228. description='Google client secret key for OAuth',
  229. default=None,
  230. )
  231. class ModerationConfig(BaseSettings):
  232. """
  233. Moderation in app configs.
  234. """
  235. # todo: to be clarified in usage and unit
  236. OUTPUT_MODERATION_BUFFER_SIZE: PositiveInt = Field(
  237. description='buffer size for moderation',
  238. default=300,
  239. )
  240. class ToolConfig(BaseSettings):
  241. """
  242. Tool configs
  243. """
  244. TOOL_ICON_CACHE_MAX_AGE: PositiveInt = Field(
  245. description='max age in seconds for tool icon caching',
  246. default=3600,
  247. )
  248. class MailConfig(BaseSettings):
  249. """
  250. Mail Configurations
  251. """
  252. MAIL_TYPE: Optional[str] = Field(
  253. description='Mail provider type name, default to None, availabile values are `smtp` and `resend`.',
  254. default=None,
  255. )
  256. MAIL_DEFAULT_SEND_FROM: Optional[str] = Field(
  257. description='default email address for sending from ',
  258. default=None,
  259. )
  260. RESEND_API_KEY: Optional[str] = Field(
  261. description='API key for Resend',
  262. default=None,
  263. )
  264. RESEND_API_URL: Optional[str] = Field(
  265. description='API URL for Resend',
  266. default=None,
  267. )
  268. SMTP_SERVER: Optional[str] = Field(
  269. description='smtp server host',
  270. default=None,
  271. )
  272. SMTP_PORT: Optional[int] = Field(
  273. description='smtp server port',
  274. default=465,
  275. )
  276. SMTP_USERNAME: Optional[str] = Field(
  277. description='smtp server username',
  278. default=None,
  279. )
  280. SMTP_PASSWORD: Optional[str] = Field(
  281. description='smtp server password',
  282. default=None,
  283. )
  284. SMTP_USE_TLS: bool = Field(
  285. description='whether to use TLS connection to smtp server',
  286. default=False,
  287. )
  288. SMTP_OPPORTUNISTIC_TLS: bool = Field(
  289. description='whether to use opportunistic TLS connection to smtp server',
  290. default=False,
  291. )
  292. class RagEtlConfig(BaseSettings):
  293. """
  294. RAG ETL Configurations.
  295. """
  296. ETL_TYPE: str = Field(
  297. description='RAG ETL type name, default to `dify`, available values are `dify` and `Unstructured`. ',
  298. default='dify',
  299. )
  300. KEYWORD_DATA_SOURCE_TYPE: str = Field(
  301. description='source type for keyword data, default to `database`, available values are `database` .',
  302. default='database',
  303. )
  304. UNSTRUCTURED_API_URL: Optional[str] = Field(
  305. description='API URL for Unstructured',
  306. default=None,
  307. )
  308. UNSTRUCTURED_API_KEY: Optional[str] = Field(
  309. description='API key for Unstructured',
  310. default=None,
  311. )
  312. class DataSetConfig(BaseSettings):
  313. """
  314. Dataset configs
  315. """
  316. CLEAN_DAY_SETTING: PositiveInt = Field(
  317. description='interval in days for cleaning up dataset',
  318. default=30,
  319. )
  320. DATASET_OPERATOR_ENABLED: bool = Field(
  321. description='whether to enable dataset operator',
  322. default=False,
  323. )
  324. class WorkspaceConfig(BaseSettings):
  325. """
  326. Workspace configs
  327. """
  328. INVITE_EXPIRY_HOURS: PositiveInt = Field(
  329. description='workspaces invitation expiration in hours',
  330. default=72,
  331. )
  332. class IndexingConfig(BaseSettings):
  333. """
  334. Indexing configs.
  335. """
  336. INDEXING_MAX_SEGMENTATION_TOKENS_LENGTH: PositiveInt = Field(
  337. description='max segmentation token length for indexing',
  338. default=1000,
  339. )
  340. class ImageFormatConfig(BaseSettings):
  341. MULTIMODAL_SEND_IMAGE_FORMAT: str = Field(
  342. description='multi model send image format, support base64, url, default is base64',
  343. default='base64',
  344. )
  345. class CeleryBeatConfig(BaseSettings):
  346. CELERY_BEAT_SCHEDULER_TIME: int = Field(
  347. description='the time of the celery scheduler, default to 1 day',
  348. default=1,
  349. )
  350. class FeatureConfig(
  351. # place the configs in alphabet order
  352. AppExecutionConfig,
  353. BillingConfig,
  354. CodeExecutionSandboxConfig,
  355. DataSetConfig,
  356. EndpointConfig,
  357. FileAccessConfig,
  358. FileUploadConfig,
  359. HttpConfig,
  360. ImageFormatConfig,
  361. InnerAPIConfig,
  362. IndexingConfig,
  363. LoggingConfig,
  364. MailConfig,
  365. ModelLoadBalanceConfig,
  366. ModerationConfig,
  367. OAuthConfig,
  368. RagEtlConfig,
  369. SecurityConfig,
  370. ToolConfig,
  371. UpdateConfig,
  372. WorkflowConfig,
  373. WorkspaceConfig,
  374. # hosted services config
  375. HostedServiceConfig,
  376. CeleryBeatConfig,
  377. ):
  378. pass