hosting_configuration.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import os
  2. from typing import Optional
  3. from flask import Flask
  4. from pydantic import BaseModel
  5. from core.entities.provider_entities import QuotaUnit, RestrictModel
  6. from core.model_runtime.entities.model_entities import ModelType
  7. from models.provider import ProviderQuotaType
  8. class HostingQuota(BaseModel):
  9. quota_type: ProviderQuotaType
  10. restrict_models: list[RestrictModel] = []
  11. class TrialHostingQuota(HostingQuota):
  12. quota_type: ProviderQuotaType = ProviderQuotaType.TRIAL
  13. quota_limit: int = 0
  14. """Quota limit for the hosting provider models. -1 means unlimited."""
  15. class PaidHostingQuota(HostingQuota):
  16. quota_type: ProviderQuotaType = ProviderQuotaType.PAID
  17. stripe_price_id: str = None
  18. increase_quota: int = 1
  19. min_quantity: int = 20
  20. max_quantity: int = 100
  21. class FreeHostingQuota(HostingQuota):
  22. quota_type: ProviderQuotaType = ProviderQuotaType.FREE
  23. class HostingProvider(BaseModel):
  24. enabled: bool = False
  25. credentials: Optional[dict] = None
  26. quota_unit: Optional[QuotaUnit] = None
  27. quotas: list[HostingQuota] = []
  28. class HostedModerationConfig(BaseModel):
  29. enabled: bool = False
  30. providers: list[str] = []
  31. class HostingConfiguration:
  32. provider_map: dict[str, HostingProvider] = {}
  33. moderation_config: HostedModerationConfig = None
  34. def init_app(self, app: Flask) -> None:
  35. self.provider_map["azure_openai"] = self.init_azure_openai()
  36. self.provider_map["openai"] = self.init_openai()
  37. self.provider_map["anthropic"] = self.init_anthropic()
  38. self.provider_map["minimax"] = self.init_minimax()
  39. self.provider_map["spark"] = self.init_spark()
  40. self.provider_map["zhipuai"] = self.init_zhipuai()
  41. self.moderation_config = self.init_moderation_config()
  42. def init_azure_openai(self) -> HostingProvider:
  43. quota_unit = QuotaUnit.TIMES
  44. if os.environ.get("HOSTED_AZURE_OPENAI_ENABLED") and os.environ.get("HOSTED_AZURE_OPENAI_ENABLED").lower() == 'true':
  45. credentials = {
  46. "openai_api_key": os.environ.get("HOSTED_AZURE_OPENAI_API_KEY"),
  47. "openai_api_base": os.environ.get("HOSTED_AZURE_OPENAI_API_BASE"),
  48. "base_model_name": "gpt-35-turbo"
  49. }
  50. quotas = []
  51. hosted_quota_limit = int(os.environ.get("HOSTED_AZURE_OPENAI_QUOTA_LIMIT", "1000"))
  52. if hosted_quota_limit != -1 or hosted_quota_limit > 0:
  53. trial_quota = TrialHostingQuota(
  54. quota_limit=hosted_quota_limit,
  55. restrict_models=[
  56. RestrictModel(model="gpt-4", base_model_name="gpt-4", model_type=ModelType.LLM),
  57. RestrictModel(model="gpt-4-32k", base_model_name="gpt-4-32k", model_type=ModelType.LLM),
  58. RestrictModel(model="gpt-4-1106-preview", base_model_name="gpt-4-1106-preview", model_type=ModelType.LLM),
  59. RestrictModel(model="gpt-4-vision-preview", base_model_name="gpt-4-vision-preview", model_type=ModelType.LLM),
  60. RestrictModel(model="gpt-35-turbo", base_model_name="gpt-35-turbo", model_type=ModelType.LLM),
  61. RestrictModel(model="gpt-35-turbo-1106", base_model_name="gpt-35-turbo-1106", model_type=ModelType.LLM),
  62. RestrictModel(model="gpt-35-turbo-instruct", base_model_name="gpt-35-turbo-instruct", model_type=ModelType.LLM),
  63. RestrictModel(model="gpt-35-turbo-16k", base_model_name="gpt-35-turbo-16k", model_type=ModelType.LLM),
  64. RestrictModel(model="text-davinci-003", base_model_name="text-davinci-003", model_type=ModelType.LLM),
  65. RestrictModel(model="text-embedding-ada-002", base_model_name="text-embedding-ada-002", model_type=ModelType.TEXT_EMBEDDING),
  66. ]
  67. )
  68. quotas.append(trial_quota)
  69. return HostingProvider(
  70. enabled=True,
  71. credentials=credentials,
  72. quota_unit=quota_unit,
  73. quotas=quotas
  74. )
  75. return HostingProvider(
  76. enabled=False,
  77. quota_unit=quota_unit,
  78. )
  79. def init_openai(self) -> HostingProvider:
  80. quota_unit = QuotaUnit.TIMES
  81. if os.environ.get("HOSTED_OPENAI_ENABLED") and os.environ.get("HOSTED_OPENAI_ENABLED").lower() == 'true':
  82. credentials = {
  83. "openai_api_key": os.environ.get("HOSTED_OPENAI_API_KEY"),
  84. }
  85. if os.environ.get("HOSTED_OPENAI_API_BASE"):
  86. credentials["openai_api_base"] = os.environ.get("HOSTED_OPENAI_API_BASE")
  87. if os.environ.get("HOSTED_OPENAI_API_ORGANIZATION"):
  88. credentials["openai_organization"] = os.environ.get("HOSTED_OPENAI_API_ORGANIZATION")
  89. quotas = []
  90. hosted_quota_limit = int(os.environ.get("HOSTED_OPENAI_QUOTA_LIMIT", "200"))
  91. if hosted_quota_limit != -1 or hosted_quota_limit > 0:
  92. trial_quota = TrialHostingQuota(
  93. quota_limit=hosted_quota_limit,
  94. restrict_models=[
  95. RestrictModel(model="gpt-3.5-turbo", model_type=ModelType.LLM),
  96. RestrictModel(model="gpt-3.5-turbo-1106", model_type=ModelType.LLM),
  97. RestrictModel(model="gpt-3.5-turbo-instruct", model_type=ModelType.LLM),
  98. RestrictModel(model="gpt-3.5-turbo-16k", model_type=ModelType.LLM),
  99. RestrictModel(model="text-davinci-003", model_type=ModelType.LLM),
  100. ]
  101. )
  102. quotas.append(trial_quota)
  103. if os.environ.get("HOSTED_OPENAI_PAID_ENABLED") and os.environ.get(
  104. "HOSTED_OPENAI_PAID_ENABLED").lower() == 'true':
  105. paid_quota = PaidHostingQuota(
  106. stripe_price_id=os.environ.get("HOSTED_OPENAI_PAID_STRIPE_PRICE_ID"),
  107. increase_quota=int(os.environ.get("HOSTED_OPENAI_PAID_INCREASE_QUOTA", "1")),
  108. min_quantity=int(os.environ.get("HOSTED_OPENAI_PAID_MIN_QUANTITY", "1")),
  109. max_quantity=int(os.environ.get("HOSTED_OPENAI_PAID_MAX_QUANTITY", "1"))
  110. )
  111. quotas.append(paid_quota)
  112. return HostingProvider(
  113. enabled=True,
  114. credentials=credentials,
  115. quota_unit=quota_unit,
  116. quotas=quotas
  117. )
  118. return HostingProvider(
  119. enabled=False,
  120. quota_unit=quota_unit,
  121. )
  122. def init_anthropic(self) -> HostingProvider:
  123. quota_unit = QuotaUnit.TOKENS
  124. if os.environ.get("HOSTED_ANTHROPIC_ENABLED") and os.environ.get("HOSTED_ANTHROPIC_ENABLED").lower() == 'true':
  125. credentials = {
  126. "anthropic_api_key": os.environ.get("HOSTED_ANTHROPIC_API_KEY"),
  127. }
  128. if os.environ.get("HOSTED_ANTHROPIC_API_BASE"):
  129. credentials["anthropic_api_url"] = os.environ.get("HOSTED_ANTHROPIC_API_BASE")
  130. quotas = []
  131. hosted_quota_limit = int(os.environ.get("HOSTED_ANTHROPIC_QUOTA_LIMIT", "0"))
  132. if hosted_quota_limit != -1 or hosted_quota_limit > 0:
  133. trial_quota = TrialHostingQuota(
  134. quota_limit=hosted_quota_limit
  135. )
  136. quotas.append(trial_quota)
  137. if os.environ.get("HOSTED_ANTHROPIC_PAID_ENABLED") and os.environ.get(
  138. "HOSTED_ANTHROPIC_PAID_ENABLED").lower() == 'true':
  139. paid_quota = PaidHostingQuota(
  140. stripe_price_id=os.environ.get("HOSTED_ANTHROPIC_PAID_STRIPE_PRICE_ID"),
  141. increase_quota=int(os.environ.get("HOSTED_ANTHROPIC_PAID_INCREASE_QUOTA", "1000000")),
  142. min_quantity=int(os.environ.get("HOSTED_ANTHROPIC_PAID_MIN_QUANTITY", "20")),
  143. max_quantity=int(os.environ.get("HOSTED_ANTHROPIC_PAID_MAX_QUANTITY", "100"))
  144. )
  145. quotas.append(paid_quota)
  146. return HostingProvider(
  147. enabled=True,
  148. credentials=credentials,
  149. quota_unit=quota_unit,
  150. quotas=quotas
  151. )
  152. return HostingProvider(
  153. enabled=False,
  154. quota_unit=quota_unit,
  155. )
  156. def init_minimax(self) -> HostingProvider:
  157. quota_unit = QuotaUnit.TOKENS
  158. if os.environ.get("HOSTED_MINIMAX_ENABLED") and os.environ.get("HOSTED_MINIMAX_ENABLED").lower() == 'true':
  159. quotas = [FreeHostingQuota()]
  160. return HostingProvider(
  161. enabled=True,
  162. credentials=None, # use credentials from the provider
  163. quota_unit=quota_unit,
  164. quotas=quotas
  165. )
  166. return HostingProvider(
  167. enabled=False,
  168. quota_unit=quota_unit,
  169. )
  170. def init_spark(self) -> HostingProvider:
  171. quota_unit = QuotaUnit.TOKENS
  172. if os.environ.get("HOSTED_SPARK_ENABLED") and os.environ.get("HOSTED_SPARK_ENABLED").lower() == 'true':
  173. quotas = [FreeHostingQuota()]
  174. return HostingProvider(
  175. enabled=True,
  176. credentials=None, # use credentials from the provider
  177. quota_unit=quota_unit,
  178. quotas=quotas
  179. )
  180. return HostingProvider(
  181. enabled=False,
  182. quota_unit=quota_unit,
  183. )
  184. def init_zhipuai(self) -> HostingProvider:
  185. quota_unit = QuotaUnit.TOKENS
  186. if os.environ.get("HOSTED_ZHIPUAI_ENABLED") and os.environ.get("HOSTED_ZHIPUAI_ENABLED").lower() == 'true':
  187. quotas = [FreeHostingQuota()]
  188. return HostingProvider(
  189. enabled=True,
  190. credentials=None, # use credentials from the provider
  191. quota_unit=quota_unit,
  192. quotas=quotas
  193. )
  194. return HostingProvider(
  195. enabled=False,
  196. quota_unit=quota_unit,
  197. )
  198. def init_moderation_config(self) -> HostedModerationConfig:
  199. if os.environ.get("HOSTED_MODERATION_ENABLED") and os.environ.get("HOSTED_MODERATION_ENABLED").lower() == 'true' \
  200. and os.environ.get("HOSTED_MODERATION_PROVIDERS"):
  201. return HostedModerationConfig(
  202. enabled=True,
  203. providers=os.environ.get("HOSTED_MODERATION_PROVIDERS").split(',')
  204. )
  205. return HostedModerationConfig(
  206. enabled=False
  207. )