| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | from typing import Optional | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  | from flask import Flask | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from pydantic import BaseModel | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from core.entities import DEFAULT_PLUGIN_ID | 
					
						
							|  |  |  | from core.entities.provider_entities import ProviderQuotaType, QuotaUnit, RestrictModel | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  | from core.model_runtime.entities.model_entities import ModelType | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class HostingQuota(BaseModel): | 
					
						
							|  |  |  |     quota_type: ProviderQuotaType | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  |     restrict_models: list[RestrictModel] = [] | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TrialHostingQuota(HostingQuota): | 
					
						
							|  |  |  |     quota_type: ProviderQuotaType = ProviderQuotaType.TRIAL | 
					
						
							|  |  |  |     quota_limit: int = 0 | 
					
						
							|  |  |  |     """Quota limit for the hosting provider models. -1 means unlimited.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PaidHostingQuota(HostingQuota): | 
					
						
							|  |  |  |     quota_type: ProviderQuotaType = ProviderQuotaType.PAID | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FreeHostingQuota(HostingQuota): | 
					
						
							|  |  |  |     quota_type: ProviderQuotaType = ProviderQuotaType.FREE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class HostingProvider(BaseModel): | 
					
						
							|  |  |  |     enabled: bool = False | 
					
						
							|  |  |  |     credentials: Optional[dict] = None | 
					
						
							|  |  |  |     quota_unit: Optional[QuotaUnit] = None | 
					
						
							|  |  |  |     quotas: list[HostingQuota] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class HostedModerationConfig(BaseModel): | 
					
						
							|  |  |  |     enabled: bool = False | 
					
						
							|  |  |  |     providers: list[str] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class HostingConfiguration: | 
					
						
							| 
									
										
										
										
											2025-02-25 12:20:47 +08:00
										 |  |  |     provider_map: dict[str, HostingProvider] | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |     moderation_config: Optional[HostedModerationConfig] = None | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 12:20:47 +08:00
										 |  |  |     def __init__(self) -> None: | 
					
						
							|  |  |  |         self.provider_map = {} | 
					
						
							|  |  |  |         self.moderation_config = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  |     def init_app(self, app: Flask) -> None: | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.EDITION != "CLOUD": | 
					
						
							| 
									
										
										
										
											2024-01-09 19:56:09 +08:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 18:47:33 +08:00
										 |  |  |         self.provider_map[f"{DEFAULT_PLUGIN_ID}/azure_openai/azure_openai"] = self.init_azure_openai() | 
					
						
							|  |  |  |         self.provider_map[f"{DEFAULT_PLUGIN_ID}/openai/openai"] = self.init_openai() | 
					
						
							|  |  |  |         self.provider_map[f"{DEFAULT_PLUGIN_ID}/anthropic/anthropic"] = self.init_anthropic() | 
					
						
							|  |  |  |         self.provider_map[f"{DEFAULT_PLUGIN_ID}/minimax/minimax"] = self.init_minimax() | 
					
						
							|  |  |  |         self.provider_map[f"{DEFAULT_PLUGIN_ID}/spark/spark"] = self.init_spark() | 
					
						
							|  |  |  |         self.provider_map[f"{DEFAULT_PLUGIN_ID}/zhipuai/zhipuai"] = self.init_zhipuai() | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         self.moderation_config = self.init_moderation_config() | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 20:45:03 +09:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def init_azure_openai() -> HostingProvider: | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  |         quota_unit = QuotaUnit.TIMES | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_AZURE_OPENAI_ENABLED: | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  |             credentials = { | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |                 "openai_api_key": dify_config.HOSTED_AZURE_OPENAI_API_KEY, | 
					
						
							|  |  |  |                 "openai_api_base": dify_config.HOSTED_AZURE_OPENAI_API_BASE, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                 "base_model_name": "gpt-35-turbo", | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             quotas: list[HostingQuota] = [] | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |             hosted_quota_limit = dify_config.HOSTED_AZURE_OPENAI_QUOTA_LIMIT | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |             trial_quota = TrialHostingQuota( | 
					
						
							|  |  |  |                 quota_limit=hosted_quota_limit, | 
					
						
							|  |  |  |                 restrict_models=[ | 
					
						
							|  |  |  |                     RestrictModel(model="gpt-4", base_model_name="gpt-4", model_type=ModelType.LLM), | 
					
						
							| 
									
										
										
										
											2024-08-01 12:57:52 +08:00
										 |  |  |                     RestrictModel(model="gpt-4o", base_model_name="gpt-4o", model_type=ModelType.LLM), | 
					
						
							|  |  |  |                     RestrictModel(model="gpt-4o-mini", base_model_name="gpt-4o-mini", model_type=ModelType.LLM), | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |                     RestrictModel(model="gpt-4-32k", base_model_name="gpt-4-32k", model_type=ModelType.LLM), | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="gpt-4-1106-preview", base_model_name="gpt-4-1106-preview", model_type=ModelType.LLM | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="gpt-4-vision-preview", base_model_name="gpt-4-vision-preview", model_type=ModelType.LLM | 
					
						
							|  |  |  |                     ), | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |                     RestrictModel(model="gpt-35-turbo", base_model_name="gpt-35-turbo", model_type=ModelType.LLM), | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="gpt-35-turbo-1106", base_model_name="gpt-35-turbo-1106", model_type=ModelType.LLM | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="gpt-35-turbo-instruct", base_model_name="gpt-35-turbo-instruct", model_type=ModelType.LLM | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="gpt-35-turbo-16k", base_model_name="gpt-35-turbo-16k", model_type=ModelType.LLM | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="text-davinci-003", base_model_name="text-davinci-003", model_type=ModelType.LLM | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="text-embedding-ada-002", | 
					
						
							|  |  |  |                         base_model_name="text-embedding-ada-002", | 
					
						
							|  |  |  |                         model_type=ModelType.TEXT_EMBEDDING, | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="text-embedding-3-small", | 
					
						
							|  |  |  |                         base_model_name="text-embedding-3-small", | 
					
						
							|  |  |  |                         model_type=ModelType.TEXT_EMBEDDING, | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     RestrictModel( | 
					
						
							|  |  |  |                         model="text-embedding-3-large", | 
					
						
							|  |  |  |                         base_model_name="text-embedding-3-large", | 
					
						
							|  |  |  |                         model_type=ModelType.TEXT_EMBEDDING, | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |             quotas.append(trial_quota) | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             return HostingProvider(enabled=True, credentials=credentials, quota_unit=quota_unit, quotas=quotas) | 
					
						
							| 
									
										
										
										
											2024-01-09 19:17:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return HostingProvider( | 
					
						
							|  |  |  |             enabled=False, | 
					
						
							|  |  |  |             quota_unit=quota_unit, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def init_openai(self) -> HostingProvider: | 
					
						
							| 
									
										
										
										
											2024-01-26 18:26:15 +08:00
										 |  |  |         quota_unit = QuotaUnit.CREDITS | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         quotas: list[HostingQuota] = [] | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_OPENAI_TRIAL_ENABLED: | 
					
						
							|  |  |  |             hosted_quota_limit = dify_config.HOSTED_OPENAI_QUOTA_LIMIT | 
					
						
							|  |  |  |             trial_models = self.parse_restrict_models_from_env("HOSTED_OPENAI_TRIAL_MODELS") | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             trial_quota = TrialHostingQuota(quota_limit=hosted_quota_limit, restrict_models=trial_models) | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |             quotas.append(trial_quota) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_OPENAI_PAID_ENABLED: | 
					
						
							|  |  |  |             paid_models = self.parse_restrict_models_from_env("HOSTED_OPENAI_PAID_MODELS") | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             paid_quota = PaidHostingQuota(restrict_models=paid_models) | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |             quotas.append(paid_quota) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if len(quotas) > 0: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             credentials = { | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |                 "openai_api_key": dify_config.HOSTED_OPENAI_API_KEY, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |             if dify_config.HOSTED_OPENAI_API_BASE: | 
					
						
							|  |  |  |                 credentials["openai_api_base"] = dify_config.HOSTED_OPENAI_API_BASE | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |             if dify_config.HOSTED_OPENAI_API_ORGANIZATION: | 
					
						
							|  |  |  |                 credentials["openai_organization"] = dify_config.HOSTED_OPENAI_API_ORGANIZATION | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             return HostingProvider(enabled=True, credentials=credentials, quota_unit=quota_unit, quotas=quotas) | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return HostingProvider( | 
					
						
							|  |  |  |             enabled=False, | 
					
						
							|  |  |  |             quota_unit=quota_unit, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 20:45:03 +09:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def init_anthropic() -> HostingProvider: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         quota_unit = QuotaUnit.TOKENS | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         quotas: list[HostingQuota] = [] | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_ANTHROPIC_TRIAL_ENABLED: | 
					
						
							|  |  |  |             hosted_quota_limit = dify_config.HOSTED_ANTHROPIC_QUOTA_LIMIT | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             trial_quota = TrialHostingQuota(quota_limit=hosted_quota_limit) | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |             quotas.append(trial_quota) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_ANTHROPIC_PAID_ENABLED: | 
					
						
							| 
									
										
										
										
											2024-01-26 18:26:15 +08:00
										 |  |  |             paid_quota = PaidHostingQuota() | 
					
						
							| 
									
										
										
										
											2024-01-17 15:02:27 +08:00
										 |  |  |             quotas.append(paid_quota) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if len(quotas) > 0: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             credentials = { | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |                 "anthropic_api_key": dify_config.HOSTED_ANTHROPIC_API_KEY, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |             if dify_config.HOSTED_ANTHROPIC_API_BASE: | 
					
						
							|  |  |  |                 credentials["anthropic_api_url"] = dify_config.HOSTED_ANTHROPIC_API_BASE | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             return HostingProvider(enabled=True, credentials=credentials, quota_unit=quota_unit, quotas=quotas) | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return HostingProvider( | 
					
						
							|  |  |  |             enabled=False, | 
					
						
							|  |  |  |             quota_unit=quota_unit, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 20:45:03 +09:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def init_minimax() -> HostingProvider: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         quota_unit = QuotaUnit.TOKENS | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_MINIMAX_ENABLED: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             quotas: list[HostingQuota] = [FreeHostingQuota()] | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return HostingProvider( | 
					
						
							|  |  |  |                 enabled=True, | 
					
						
							|  |  |  |                 credentials=None,  # use credentials from the provider | 
					
						
							|  |  |  |                 quota_unit=quota_unit, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                 quotas=quotas, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return HostingProvider( | 
					
						
							|  |  |  |             enabled=False, | 
					
						
							|  |  |  |             quota_unit=quota_unit, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 20:45:03 +09:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def init_spark() -> HostingProvider: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         quota_unit = QuotaUnit.TOKENS | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_SPARK_ENABLED: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             quotas: list[HostingQuota] = [FreeHostingQuota()] | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return HostingProvider( | 
					
						
							|  |  |  |                 enabled=True, | 
					
						
							|  |  |  |                 credentials=None,  # use credentials from the provider | 
					
						
							|  |  |  |                 quota_unit=quota_unit, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                 quotas=quotas, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return HostingProvider( | 
					
						
							|  |  |  |             enabled=False, | 
					
						
							|  |  |  |             quota_unit=quota_unit, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 20:45:03 +09:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def init_zhipuai() -> HostingProvider: | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         quota_unit = QuotaUnit.TOKENS | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.HOSTED_ZHIPUAI_ENABLED: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             quotas: list[HostingQuota] = [FreeHostingQuota()] | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return HostingProvider( | 
					
						
							|  |  |  |                 enabled=True, | 
					
						
							|  |  |  |                 credentials=None,  # use credentials from the provider | 
					
						
							|  |  |  |                 quota_unit=quota_unit, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                 quotas=quotas, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return HostingProvider( | 
					
						
							|  |  |  |             enabled=False, | 
					
						
							|  |  |  |             quota_unit=quota_unit, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 20:45:03 +09:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def init_moderation_config() -> HostedModerationConfig: | 
					
						
							|  |  |  |         if dify_config.HOSTED_MODERATION_ENABLED and dify_config.HOSTED_MODERATION_PROVIDERS: | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             providers = dify_config.HOSTED_MODERATION_PROVIDERS.split(",") | 
					
						
							|  |  |  |             hosted_providers = [] | 
					
						
							|  |  |  |             for provider in providers: | 
					
						
							|  |  |  |                 if "/" not in provider: | 
					
						
							|  |  |  |                     provider = f"{DEFAULT_PLUGIN_ID}/{provider}/{provider}" | 
					
						
							|  |  |  |                 hosted_providers.append(provider) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return HostedModerationConfig(enabled=True, providers=hosted_providers) | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         return HostedModerationConfig(enabled=False) | 
					
						
							| 
									
										
										
										
											2024-02-23 16:12:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def parse_restrict_models_from_env(env_var: str) -> list[RestrictModel]: | 
					
						
							|  |  |  |         models_str = dify_config.model_dump().get(env_var) | 
					
						
							| 
									
										
										
										
											2024-02-23 16:12:43 +08:00
										 |  |  |         models_list = models_str.split(",") if models_str else [] | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             RestrictModel(model=model_name.strip(), model_type=ModelType.LLM) | 
					
						
							|  |  |  |             for model_name in models_list | 
					
						
							|  |  |  |             if model_name.strip() | 
					
						
							|  |  |  |         ] |