fix(configs): Update pydantic settings in config files (#6023)

This commit is contained in:
-LAN- 2024-07-07 12:18:15 +08:00 committed by GitHub
parent a877d4831d
commit c436454cd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 115 additions and 93 deletions

View File

@ -1,4 +1,3 @@
from .app_config import DifyConfig from .app_config import DifyConfig
dify_config = DifyConfig() dify_config = DifyConfig()

View File

@ -1,5 +1,5 @@
from pydantic import computed_field from pydantic import Field, computed_field
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import SettingsConfigDict
from configs.deploy import DeploymentConfig from configs.deploy import DeploymentConfig
from configs.enterprise import EnterpriseFeatureConfig from configs.enterprise import EnterpriseFeatureConfig
@ -9,13 +9,7 @@ from configs.middleware import MiddlewareConfig
from configs.packaging import PackagingInfo from configs.packaging import PackagingInfo
# TODO: Both `BaseModel` and `BaseSettings` has `model_config` attribute but they are in different types.
# This inheritance is depends on the order of the classes.
# It is better to use `BaseSettings` as the base class.
class DifyConfig( class DifyConfig(
# based on pydantic-settings
BaseSettings,
# Packaging info # Packaging info
PackagingInfo, PackagingInfo,
@ -35,11 +29,13 @@ class DifyConfig(
# **Before using, please contact business@dify.ai by email to inquire about licensing matters.** # **Before using, please contact business@dify.ai by email to inquire about licensing matters.**
EnterpriseFeatureConfig, EnterpriseFeatureConfig,
): ):
DEBUG: bool = Field(default=False, description='whether to enable debug mode.')
model_config = SettingsConfigDict( model_config = SettingsConfigDict(
# read from dotenv format config file # read from dotenv format config file
env_file='.env', env_file='.env',
env_file_encoding='utf-8', env_file_encoding='utf-8',
frozen=True,
# ignore extra attributes # ignore extra attributes
extra='ignore', extra='ignore',

View File

@ -1,7 +1,8 @@
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class DeploymentConfig(BaseModel): class DeploymentConfig(BaseSettings):
""" """
Deployment configs Deployment configs
""" """

View File

@ -1,7 +1,8 @@
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class EnterpriseFeatureConfig(BaseModel): class EnterpriseFeatureConfig(BaseSettings):
""" """
Enterprise feature configs. Enterprise feature configs.
**Before using, please contact business@dify.ai by email to inquire about licensing matters.** **Before using, please contact business@dify.ai by email to inquire about licensing matters.**

View File

@ -1,5 +1,3 @@
from pydantic import BaseModel
from configs.extra.notion_config import NotionConfig from configs.extra.notion_config import NotionConfig
from configs.extra.sentry_config import SentryConfig from configs.extra.sentry_config import SentryConfig

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class NotionConfig(BaseModel): class NotionConfig(BaseSettings):
""" """
Notion integration configs Notion integration configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, NonNegativeFloat from pydantic import Field, NonNegativeFloat
from pydantic_settings import BaseSettings
class SentryConfig(BaseModel): class SentryConfig(BaseSettings):
""" """
Sentry configs Sentry configs
""" """

View File

@ -1,11 +1,12 @@
from typing import Optional from typing import Optional
from pydantic import AliasChoices, BaseModel, Field, NonNegativeInt, PositiveInt, computed_field from pydantic import AliasChoices, Field, NonNegativeInt, PositiveInt, computed_field
from pydantic_settings import BaseSettings
from configs.feature.hosted_service import HostedServiceConfig from configs.feature.hosted_service import HostedServiceConfig
class SecurityConfig(BaseModel): class SecurityConfig(BaseSettings):
""" """
Secret Key configs Secret Key configs
""" """
@ -22,7 +23,7 @@ class SecurityConfig(BaseModel):
default=24, default=24,
) )
class AppExecutionConfig(BaseModel): class AppExecutionConfig(BaseSettings):
""" """
App Execution configs App Execution configs
""" """
@ -32,7 +33,7 @@ class AppExecutionConfig(BaseModel):
) )
class CodeExecutionSandboxConfig(BaseModel): class CodeExecutionSandboxConfig(BaseSettings):
""" """
Code Execution Sandbox configs Code Execution Sandbox configs
""" """
@ -47,7 +48,7 @@ class CodeExecutionSandboxConfig(BaseModel):
) )
class EndpointConfig(BaseModel): class EndpointConfig(BaseSettings):
""" """
Module URL configs Module URL configs
""" """
@ -76,7 +77,7 @@ class EndpointConfig(BaseModel):
) )
class FileAccessConfig(BaseModel): class FileAccessConfig(BaseSettings):
""" """
File Access configs File Access configs
""" """
@ -95,7 +96,7 @@ class FileAccessConfig(BaseModel):
) )
class FileUploadConfig(BaseModel): class FileUploadConfig(BaseSettings):
""" """
File Uploading configs File Uploading configs
""" """
@ -120,7 +121,7 @@ class FileUploadConfig(BaseModel):
) )
class HttpConfig(BaseModel): class HttpConfig(BaseSettings):
""" """
HTTP configs HTTP configs
""" """
@ -152,7 +153,7 @@ class HttpConfig(BaseModel):
return self.inner_WEB_API_CORS_ALLOW_ORIGINS.split(',') return self.inner_WEB_API_CORS_ALLOW_ORIGINS.split(',')
class InnerAPIConfig(BaseModel): class InnerAPIConfig(BaseSettings):
""" """
Inner API configs Inner API configs
""" """
@ -167,7 +168,7 @@ class InnerAPIConfig(BaseModel):
) )
class LoggingConfig(BaseModel): class LoggingConfig(BaseSettings):
""" """
Logging configs Logging configs
""" """
@ -199,7 +200,7 @@ class LoggingConfig(BaseModel):
) )
class ModelLoadBalanceConfig(BaseModel): class ModelLoadBalanceConfig(BaseSettings):
""" """
Model load balance configs Model load balance configs
""" """
@ -209,7 +210,7 @@ class ModelLoadBalanceConfig(BaseModel):
) )
class BillingConfig(BaseModel): class BillingConfig(BaseSettings):
""" """
Platform Billing Configurations Platform Billing Configurations
""" """
@ -219,7 +220,7 @@ class BillingConfig(BaseModel):
) )
class UpdateConfig(BaseModel): class UpdateConfig(BaseSettings):
""" """
Update configs Update configs
""" """
@ -229,7 +230,7 @@ class UpdateConfig(BaseModel):
) )
class WorkflowConfig(BaseModel): class WorkflowConfig(BaseSettings):
""" """
Workflow feature configs Workflow feature configs
""" """
@ -250,7 +251,7 @@ class WorkflowConfig(BaseModel):
) )
class OAuthConfig(BaseModel): class OAuthConfig(BaseSettings):
""" """
oauth configs oauth configs
""" """
@ -280,7 +281,7 @@ class OAuthConfig(BaseModel):
) )
class ModerationConfig(BaseModel): class ModerationConfig(BaseSettings):
""" """
Moderation in app configs. Moderation in app configs.
""" """
@ -292,7 +293,7 @@ class ModerationConfig(BaseModel):
) )
class ToolConfig(BaseModel): class ToolConfig(BaseSettings):
""" """
Tool configs Tool configs
""" """
@ -303,7 +304,7 @@ class ToolConfig(BaseModel):
) )
class MailConfig(BaseModel): class MailConfig(BaseSettings):
""" """
Mail Configurations Mail Configurations
""" """
@ -359,7 +360,7 @@ class MailConfig(BaseModel):
) )
class RagEtlConfig(BaseModel): class RagEtlConfig(BaseSettings):
""" """
RAG ETL Configurations. RAG ETL Configurations.
""" """
@ -385,7 +386,7 @@ class RagEtlConfig(BaseModel):
) )
class DataSetConfig(BaseModel): class DataSetConfig(BaseSettings):
""" """
Dataset configs Dataset configs
""" """
@ -396,7 +397,7 @@ class DataSetConfig(BaseModel):
) )
class WorkspaceConfig(BaseModel): class WorkspaceConfig(BaseSettings):
""" """
Workspace configs Workspace configs
""" """
@ -407,7 +408,7 @@ class WorkspaceConfig(BaseModel):
) )
class IndexingConfig(BaseModel): class IndexingConfig(BaseSettings):
""" """
Indexing configs. Indexing configs.
""" """
@ -418,7 +419,7 @@ class IndexingConfig(BaseModel):
) )
class ImageFormatConfig(BaseModel): class ImageFormatConfig(BaseSettings):
MULTIMODAL_SEND_IMAGE_FORMAT: str = Field( MULTIMODAL_SEND_IMAGE_FORMAT: str = Field(
description='multi model send image format, support base64, url, default is base64', description='multi model send image format, support base64, url, default is base64',
default='base64', default='base64',

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, NonNegativeInt from pydantic import Field, NonNegativeInt
from pydantic_settings import BaseSettings
class HostedOpenAiConfig(BaseModel): class HostedOpenAiConfig(BaseSettings):
""" """
Hosted OpenAI service config Hosted OpenAI service config
""" """
@ -68,7 +69,7 @@ class HostedOpenAiConfig(BaseModel):
) )
class HostedAzureOpenAiConfig(BaseModel): class HostedAzureOpenAiConfig(BaseSettings):
""" """
Hosted OpenAI service config Hosted OpenAI service config
""" """
@ -94,7 +95,7 @@ class HostedAzureOpenAiConfig(BaseModel):
) )
class HostedAnthropicConfig(BaseModel): class HostedAnthropicConfig(BaseSettings):
""" """
Hosted Azure OpenAI service config Hosted Azure OpenAI service config
""" """
@ -125,7 +126,7 @@ class HostedAnthropicConfig(BaseModel):
) )
class HostedMinmaxConfig(BaseModel): class HostedMinmaxConfig(BaseSettings):
""" """
Hosted Minmax service config Hosted Minmax service config
""" """
@ -136,7 +137,7 @@ class HostedMinmaxConfig(BaseModel):
) )
class HostedSparkConfig(BaseModel): class HostedSparkConfig(BaseSettings):
""" """
Hosted Spark service config Hosted Spark service config
""" """
@ -147,7 +148,7 @@ class HostedSparkConfig(BaseModel):
) )
class HostedZhipuAIConfig(BaseModel): class HostedZhipuAIConfig(BaseSettings):
""" """
Hosted Minmax service config Hosted Minmax service config
""" """
@ -158,7 +159,7 @@ class HostedZhipuAIConfig(BaseModel):
) )
class HostedModerationConfig(BaseModel): class HostedModerationConfig(BaseSettings):
""" """
Hosted Moderation service config Hosted Moderation service config
""" """
@ -174,7 +175,7 @@ class HostedModerationConfig(BaseModel):
) )
class HostedFetchAppTemplateConfig(BaseModel): class HostedFetchAppTemplateConfig(BaseSettings):
""" """
Hosted Moderation service config Hosted Moderation service config
""" """

View File

@ -1,6 +1,7 @@
from typing import Any, Optional from typing import Any, Optional
from pydantic import BaseModel, Field, NonNegativeInt, PositiveInt, computed_field from pydantic import Field, NonNegativeInt, PositiveInt, computed_field
from pydantic_settings import BaseSettings
from configs.middleware.cache.redis_config import RedisConfig from configs.middleware.cache.redis_config import RedisConfig
from configs.middleware.storage.aliyun_oss_storage_config import AliyunOSSStorageConfig from configs.middleware.storage.aliyun_oss_storage_config import AliyunOSSStorageConfig
@ -22,7 +23,7 @@ from configs.middleware.vdb.tidb_vector_config import TiDBVectorConfig
from configs.middleware.vdb.weaviate_config import WeaviateConfig from configs.middleware.vdb.weaviate_config import WeaviateConfig
class StorageConfig(BaseModel): class StorageConfig(BaseSettings):
STORAGE_TYPE: str = Field( STORAGE_TYPE: str = Field(
description='storage type,' description='storage type,'
' default to `local`,' ' default to `local`,'
@ -36,14 +37,14 @@ class StorageConfig(BaseModel):
) )
class VectorStoreConfig(BaseModel): class VectorStoreConfig(BaseSettings):
VECTOR_STORE: Optional[str] = Field( VECTOR_STORE: Optional[str] = Field(
description='vector store type', description='vector store type',
default=None, default=None,
) )
class KeywordStoreConfig(BaseModel): class KeywordStoreConfig(BaseSettings):
KEYWORD_STORE: str = Field( KEYWORD_STORE: str = Field(
description='keyword store type', description='keyword store type',
default='jieba', default='jieba',

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, NonNegativeInt, PositiveInt from pydantic import Field, NonNegativeInt, PositiveInt
from pydantic_settings import BaseSettings
class RedisConfig(BaseModel): class RedisConfig(BaseSettings):
""" """
Redis configs Redis configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class AliyunOSSStorageConfig(BaseModel): class AliyunOSSStorageConfig(BaseSettings):
""" """
Aliyun storage configs Aliyun storage configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class S3StorageConfig(BaseModel): class S3StorageConfig(BaseSettings):
""" """
S3 storage configs S3 storage configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class AzureBlobStorageConfig(BaseModel): class AzureBlobStorageConfig(BaseSettings):
""" """
Azure Blob storage configs Azure Blob storage configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class GoogleCloudStorageConfig(BaseModel): class GoogleCloudStorageConfig(BaseSettings):
""" """
Google Cloud storage configs Google Cloud storage configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class OCIStorageConfig(BaseModel): class OCIStorageConfig(BaseSettings):
""" """
OCI storage configs OCI storage configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class TencentCloudCOSStorageConfig(BaseModel): class TencentCloudCOSStorageConfig(BaseSettings):
""" """
Tencent Cloud COS storage configs Tencent Cloud COS storage configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class ChromaConfig(BaseModel): class ChromaConfig(BaseSettings):
""" """
Chroma configs Chroma configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class MilvusConfig(BaseModel): class MilvusConfig(BaseSettings):
""" """
Milvus configs Milvus configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class OpenSearchConfig(BaseModel): class OpenSearchConfig(BaseSettings):
""" """
OpenSearch configs OpenSearch configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class OracleConfig(BaseModel): class OracleConfig(BaseSettings):
""" """
ORACLE configs ORACLE configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class PGVectorConfig(BaseModel): class PGVectorConfig(BaseSettings):
""" """
PGVector configs PGVector configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class PGVectoRSConfig(BaseModel): class PGVectoRSConfig(BaseSettings):
""" """
PGVectoRS configs PGVectoRS configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, NonNegativeInt, PositiveInt from pydantic import Field, NonNegativeInt, PositiveInt
from pydantic_settings import BaseSettings
class QdrantConfig(BaseModel): class QdrantConfig(BaseSettings):
""" """
Qdrant configs Qdrant configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class RelytConfig(BaseModel): class RelytConfig(BaseSettings):
""" """
Relyt configs Relyt configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, NonNegativeInt, PositiveInt from pydantic import Field, NonNegativeInt, PositiveInt
from pydantic_settings import BaseSettings
class TencentVectorDBConfig(BaseModel): class TencentVectorDBConfig(BaseSettings):
""" """
Tencent Vector configs Tencent Vector configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class TiDBVectorConfig(BaseModel): class TiDBVectorConfig(BaseSettings):
""" """
TiDB Vector configs TiDB Vector configs
""" """

View File

@ -1,9 +1,10 @@
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field, PositiveInt from pydantic import Field, PositiveInt
from pydantic_settings import BaseSettings
class WeaviateConfig(BaseModel): class WeaviateConfig(BaseSettings):
""" """
Weaviate configs Weaviate configs
""" """

View File

@ -1,7 +1,8 @@
from pydantic import BaseModel, Field from pydantic import Field
from pydantic_settings import BaseSettings
class PackagingInfo(BaseModel): class PackagingInfo(BaseSettings):
""" """
Packaging build information Packaging build information
""" """

View File

@ -21,6 +21,7 @@ def example_env_file(tmp_path, monkeypatch) -> str:
def test_dify_config_undefined_entry(example_env_file): def test_dify_config_undefined_entry(example_env_file):
# NOTE: See https://github.com/microsoft/pylance-release/issues/6099 for more details about this type error.
# load dotenv file with pydantic-settings # load dotenv file with pydantic-settings
config = DifyConfig(_env_file=example_env_file) config = DifyConfig(_env_file=example_env_file)
@ -43,6 +44,8 @@ def test_dify_config(example_env_file):
assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0 assert config.SENTRY_TRACES_SAMPLE_RATE == 1.0
# NOTE: If there is a `.env` file in your Workspace, this test might not succeed as expected.
# This is due to `pymilvus` loading all the variables from the `.env` file into `os.environ`.
def test_flask_configs(example_env_file): def test_flask_configs(example_env_file):
flask_app = Flask('app') flask_app = Flask('app')
flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump()) flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump())