anthropic_provider.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import json
  2. import logging
  3. from json import JSONDecodeError
  4. from typing import Type, Optional
  5. import anthropic
  6. from flask import current_app
  7. from langchain.schema import HumanMessage
  8. from core.helper import encrypter
  9. from core.model_providers.models.base import BaseProviderModel
  10. from core.model_providers.models.entity.model_params import ModelKwargsRules, KwargRule
  11. from core.model_providers.models.entity.provider import ModelFeature
  12. from core.model_providers.models.llm.anthropic_model import AnthropicModel
  13. from core.model_providers.models.llm.base import ModelType
  14. from core.model_providers.providers.base import BaseModelProvider, CredentialsValidateFailedError
  15. from core.model_providers.providers.hosted import hosted_model_providers
  16. from core.third_party.langchain.llms.anthropic_llm import AnthropicLLM
  17. from models.provider import ProviderType
  18. class AnthropicProvider(BaseModelProvider):
  19. @property
  20. def provider_name(self):
  21. """
  22. Returns the name of a provider.
  23. """
  24. return 'anthropic'
  25. def _get_fixed_model_list(self, model_type: ModelType) -> list[dict]:
  26. if model_type == ModelType.TEXT_GENERATION:
  27. return [
  28. {
  29. 'id': 'claude-instant-1',
  30. 'name': 'claude-instant-1',
  31. },
  32. {
  33. 'id': 'claude-2',
  34. 'name': 'claude-2',
  35. 'features': [
  36. ModelFeature.AGENT_THOUGHT.value
  37. ]
  38. },
  39. ]
  40. else:
  41. return []
  42. def get_model_class(self, model_type: ModelType) -> Type[BaseProviderModel]:
  43. """
  44. Returns the model class.
  45. :param model_type:
  46. :return:
  47. """
  48. if model_type == ModelType.TEXT_GENERATION:
  49. model_class = AnthropicModel
  50. else:
  51. raise NotImplementedError
  52. return model_class
  53. def get_model_parameter_rules(self, model_name: str, model_type: ModelType) -> ModelKwargsRules:
  54. """
  55. get model parameter rules.
  56. :param model_name:
  57. :param model_type:
  58. :return:
  59. """
  60. return ModelKwargsRules(
  61. temperature=KwargRule[float](min=0, max=1, default=1, precision=2),
  62. top_p=KwargRule[float](min=0, max=1, default=0.7, precision=2),
  63. presence_penalty=KwargRule[float](enabled=False),
  64. frequency_penalty=KwargRule[float](enabled=False),
  65. max_tokens=KwargRule[int](alias="max_tokens_to_sample", min=10, max=100000, default=256, precision=0),
  66. )
  67. @classmethod
  68. def is_provider_credentials_valid_or_raise(cls, credentials: dict):
  69. """
  70. Validates the given credentials.
  71. """
  72. if 'anthropic_api_key' not in credentials:
  73. raise CredentialsValidateFailedError('Anthropic API Key must be provided.')
  74. try:
  75. credential_kwargs = {
  76. 'anthropic_api_key': credentials['anthropic_api_key']
  77. }
  78. if 'anthropic_api_url' in credentials:
  79. credential_kwargs['anthropic_api_url'] = credentials['anthropic_api_url']
  80. chat_llm = AnthropicLLM(
  81. model='claude-instant-1',
  82. max_tokens_to_sample=10,
  83. temperature=0,
  84. default_request_timeout=60,
  85. **credential_kwargs
  86. )
  87. messages = [
  88. HumanMessage(
  89. content="ping"
  90. )
  91. ]
  92. chat_llm(messages)
  93. except anthropic.APIConnectionError as ex:
  94. raise CredentialsValidateFailedError(str(ex))
  95. except (anthropic.APIStatusError, anthropic.RateLimitError) as ex:
  96. raise CredentialsValidateFailedError(str(ex))
  97. except Exception as ex:
  98. logging.exception('Anthropic config validation failed')
  99. raise ex
  100. @classmethod
  101. def encrypt_provider_credentials(cls, tenant_id: str, credentials: dict) -> dict:
  102. credentials['anthropic_api_key'] = encrypter.encrypt_token(tenant_id, credentials['anthropic_api_key'])
  103. return credentials
  104. def get_provider_credentials(self, obfuscated: bool = False) -> dict:
  105. if self.provider.provider_type == ProviderType.CUSTOM.value:
  106. try:
  107. credentials = json.loads(self.provider.encrypted_config)
  108. except JSONDecodeError:
  109. credentials = {
  110. 'anthropic_api_url': None,
  111. 'anthropic_api_key': None
  112. }
  113. if credentials['anthropic_api_key']:
  114. credentials['anthropic_api_key'] = encrypter.decrypt_token(
  115. self.provider.tenant_id,
  116. credentials['anthropic_api_key']
  117. )
  118. if obfuscated:
  119. credentials['anthropic_api_key'] = encrypter.obfuscated_token(credentials['anthropic_api_key'])
  120. if 'anthropic_api_url' not in credentials:
  121. credentials['anthropic_api_url'] = None
  122. return credentials
  123. else:
  124. if hosted_model_providers.anthropic:
  125. return {
  126. 'anthropic_api_url': hosted_model_providers.anthropic.api_base,
  127. 'anthropic_api_key': hosted_model_providers.anthropic.api_key,
  128. }
  129. else:
  130. return {
  131. 'anthropic_api_url': None,
  132. 'anthropic_api_key': None
  133. }
  134. @classmethod
  135. def is_provider_type_system_supported(cls) -> bool:
  136. if current_app.config['EDITION'] != 'CLOUD':
  137. return False
  138. if hosted_model_providers.anthropic:
  139. return True
  140. return False
  141. def should_deduct_quota(self):
  142. if hosted_model_providers.anthropic and \
  143. hosted_model_providers.anthropic.quota_limit and hosted_model_providers.anthropic.quota_limit > 0:
  144. return True
  145. return False
  146. def get_payment_info(self) -> Optional[dict]:
  147. """
  148. get product info if it payable.
  149. :return:
  150. """
  151. if hosted_model_providers.anthropic \
  152. and hosted_model_providers.anthropic.paid_enabled:
  153. return {
  154. 'product_id': hosted_model_providers.anthropic.paid_stripe_price_id,
  155. 'increase_quota': hosted_model_providers.anthropic.paid_increase_quota,
  156. 'min_quantity': hosted_model_providers.anthropic.paid_min_quantity,
  157. 'max_quantity': hosted_model_providers.anthropic.paid_max_quantity,
  158. }
  159. return None
  160. @classmethod
  161. def is_model_credentials_valid_or_raise(cls, model_name: str, model_type: ModelType, credentials: dict):
  162. """
  163. check model credentials valid.
  164. :param model_name:
  165. :param model_type:
  166. :param credentials:
  167. """
  168. return
  169. @classmethod
  170. def encrypt_model_credentials(cls, tenant_id: str, model_name: str, model_type: ModelType,
  171. credentials: dict) -> dict:
  172. """
  173. encrypt model credentials for save.
  174. :param tenant_id:
  175. :param model_name:
  176. :param model_type:
  177. :param credentials:
  178. :return:
  179. """
  180. return {}
  181. def get_model_credentials(self, model_name: str, model_type: ModelType, obfuscated: bool = False) -> dict:
  182. """
  183. get credentials for llm use.
  184. :param model_name:
  185. :param model_type:
  186. :param obfuscated:
  187. :return:
  188. """
  189. return self.get_provider_credentials(obfuscated)