__init__.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. from typing import Optional
  2. from pydantic import Field, NonNegativeInt, computed_field
  3. from pydantic_settings import BaseSettings
  4. class HostedCreditConfig(BaseSettings):
  5. HOSTED_MODEL_CREDIT_CONFIG: str = Field(
  6. description="Model credit configuration in format 'model:credits,model:credits', e.g., 'gpt-4:20,gpt-4o:10'",
  7. default="",
  8. )
  9. def get_model_credits(self, model_name: str) -> int:
  10. """
  11. Get credit value for a specific model name.
  12. Returns 1 if model is not found in configuration (default credit).
  13. :param model_name: The name of the model to search for
  14. :return: The credit value for the model
  15. """
  16. if not self.HOSTED_MODEL_CREDIT_CONFIG:
  17. return 1
  18. try:
  19. credit_map = dict(
  20. item.strip().split(":", 1) for item in self.HOSTED_MODEL_CREDIT_CONFIG.split(",") if ":" in item
  21. )
  22. # Search for matching model pattern
  23. for pattern, credit in credit_map.items():
  24. if pattern.strip() == model_name:
  25. return int(credit)
  26. return 1 # Default quota if no match found
  27. except (ValueError, AttributeError):
  28. return 1 # Return default quota if parsing fails
  29. class HostedOpenAiConfig(BaseSettings):
  30. """
  31. Configuration for hosted OpenAI service
  32. """
  33. HOSTED_OPENAI_API_KEY: Optional[str] = Field(
  34. description="API key for hosted OpenAI service",
  35. default=None,
  36. )
  37. HOSTED_OPENAI_API_BASE: Optional[str] = Field(
  38. description="Base URL for hosted OpenAI API",
  39. default=None,
  40. )
  41. HOSTED_OPENAI_API_ORGANIZATION: Optional[str] = Field(
  42. description="Organization ID for hosted OpenAI service",
  43. default=None,
  44. )
  45. HOSTED_OPENAI_TRIAL_ENABLED: bool = Field(
  46. description="Enable trial access to hosted OpenAI service",
  47. default=False,
  48. )
  49. HOSTED_OPENAI_TRIAL_MODELS: str = Field(
  50. description="Comma-separated list of available models for trial access",
  51. default="gpt-3.5-turbo,"
  52. "gpt-3.5-turbo-1106,"
  53. "gpt-3.5-turbo-instruct,"
  54. "gpt-3.5-turbo-16k,"
  55. "gpt-3.5-turbo-16k-0613,"
  56. "gpt-3.5-turbo-0613,"
  57. "gpt-3.5-turbo-0125,"
  58. "text-davinci-003",
  59. )
  60. HOSTED_OPENAI_QUOTA_LIMIT: NonNegativeInt = Field(
  61. description="Quota limit for hosted OpenAI service usage",
  62. default=200,
  63. )
  64. HOSTED_OPENAI_PAID_ENABLED: bool = Field(
  65. description="Enable paid access to hosted OpenAI service",
  66. default=False,
  67. )
  68. HOSTED_OPENAI_PAID_MODELS: str = Field(
  69. description="Comma-separated list of available models for paid access",
  70. default="gpt-4,"
  71. "gpt-4-turbo-preview,"
  72. "gpt-4-turbo-2024-04-09,"
  73. "gpt-4-1106-preview,"
  74. "gpt-4-0125-preview,"
  75. "gpt-3.5-turbo,"
  76. "gpt-3.5-turbo-16k,"
  77. "gpt-3.5-turbo-16k-0613,"
  78. "gpt-3.5-turbo-1106,"
  79. "gpt-3.5-turbo-0613,"
  80. "gpt-3.5-turbo-0125,"
  81. "gpt-3.5-turbo-instruct,"
  82. "text-davinci-003",
  83. )
  84. class HostedAzureOpenAiConfig(BaseSettings):
  85. """
  86. Configuration for hosted Azure OpenAI service
  87. """
  88. HOSTED_AZURE_OPENAI_ENABLED: bool = Field(
  89. description="Enable hosted Azure OpenAI service",
  90. default=False,
  91. )
  92. HOSTED_AZURE_OPENAI_API_KEY: Optional[str] = Field(
  93. description="API key for hosted Azure OpenAI service",
  94. default=None,
  95. )
  96. HOSTED_AZURE_OPENAI_API_BASE: Optional[str] = Field(
  97. description="Base URL for hosted Azure OpenAI API",
  98. default=None,
  99. )
  100. HOSTED_AZURE_OPENAI_QUOTA_LIMIT: NonNegativeInt = Field(
  101. description="Quota limit for hosted Azure OpenAI service usage",
  102. default=200,
  103. )
  104. class HostedAnthropicConfig(BaseSettings):
  105. """
  106. Configuration for hosted Anthropic service
  107. """
  108. HOSTED_ANTHROPIC_API_BASE: Optional[str] = Field(
  109. description="Base URL for hosted Anthropic API",
  110. default=None,
  111. )
  112. HOSTED_ANTHROPIC_API_KEY: Optional[str] = Field(
  113. description="API key for hosted Anthropic service",
  114. default=None,
  115. )
  116. HOSTED_ANTHROPIC_TRIAL_ENABLED: bool = Field(
  117. description="Enable trial access to hosted Anthropic service",
  118. default=False,
  119. )
  120. HOSTED_ANTHROPIC_QUOTA_LIMIT: NonNegativeInt = Field(
  121. description="Quota limit for hosted Anthropic service usage",
  122. default=600000,
  123. )
  124. HOSTED_ANTHROPIC_PAID_ENABLED: bool = Field(
  125. description="Enable paid access to hosted Anthropic service",
  126. default=False,
  127. )
  128. class HostedMinmaxConfig(BaseSettings):
  129. """
  130. Configuration for hosted Minmax service
  131. """
  132. HOSTED_MINIMAX_ENABLED: bool = Field(
  133. description="Enable hosted Minmax service",
  134. default=False,
  135. )
  136. class HostedSparkConfig(BaseSettings):
  137. """
  138. Configuration for hosted Spark service
  139. """
  140. HOSTED_SPARK_ENABLED: bool = Field(
  141. description="Enable hosted Spark service",
  142. default=False,
  143. )
  144. class HostedZhipuAIConfig(BaseSettings):
  145. """
  146. Configuration for hosted ZhipuAI service
  147. """
  148. HOSTED_ZHIPUAI_ENABLED: bool = Field(
  149. description="Enable hosted ZhipuAI service",
  150. default=False,
  151. )
  152. class HostedModerationConfig(BaseSettings):
  153. """
  154. Configuration for hosted Moderation service
  155. """
  156. HOSTED_MODERATION_ENABLED: bool = Field(
  157. description="Enable hosted Moderation service",
  158. default=False,
  159. )
  160. HOSTED_MODERATION_PROVIDERS: str = Field(
  161. description="Comma-separated list of moderation providers",
  162. default="",
  163. )
  164. class HostedFetchAppTemplateConfig(BaseSettings):
  165. """
  166. Configuration for fetching app templates
  167. """
  168. HOSTED_FETCH_APP_TEMPLATES_MODE: str = Field(
  169. description="Mode for fetching app templates: remote, db, or builtin default to remote,",
  170. default="remote",
  171. )
  172. HOSTED_FETCH_APP_TEMPLATES_REMOTE_DOMAIN: str = Field(
  173. description="Domain for fetching remote app templates",
  174. default="https://tmpl.dify.ai",
  175. )
  176. class HostedServiceConfig(
  177. # place the configs in alphabet order
  178. HostedAnthropicConfig,
  179. HostedAzureOpenAiConfig,
  180. HostedFetchAppTemplateConfig,
  181. HostedMinmaxConfig,
  182. HostedOpenAiConfig,
  183. HostedSparkConfig,
  184. HostedZhipuAIConfig,
  185. # moderation
  186. HostedModerationConfig,
  187. # credit config
  188. HostedCreditConfig,
  189. ):
  190. pass