__init__.py 11 KB

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