hosting_configuration.py 10 KB

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