Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

__init__.py 6.4KB

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