azure_provider.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import json
  2. import logging
  3. from typing import Optional, Union
  4. import requests
  5. from core.llm.provider.base import BaseProvider
  6. from core.llm.provider.errors import ValidateFailedError
  7. from models.provider import ProviderName
  8. class AzureProvider(BaseProvider):
  9. def get_models(self, model_id: Optional[str] = None, credentials: Optional[dict] = None) -> list[dict]:
  10. credentials = self.get_credentials(model_id) if not credentials else credentials
  11. url = "{}/openai/deployments?api-version={}".format(
  12. str(credentials.get('openai_api_base')),
  13. str(credentials.get('openai_api_version'))
  14. )
  15. headers = {
  16. "api-key": str(credentials.get('openai_api_key')),
  17. "content-type": "application/json; charset=utf-8"
  18. }
  19. response = requests.get(url, headers=headers)
  20. if response.status_code == 200:
  21. result = response.json()
  22. return [{
  23. 'id': deployment['id'],
  24. 'name': '{} ({})'.format(deployment['id'], deployment['model'])
  25. } for deployment in result['data'] if deployment['status'] == 'succeeded']
  26. else:
  27. if response.status_code == 401:
  28. raise AzureAuthenticationError()
  29. else:
  30. raise AzureRequestFailedError('Failed to request Azure OpenAI. Status code: {}'.format(response.status_code))
  31. def get_credentials(self, model_id: Optional[str] = None) -> dict:
  32. """
  33. Returns the API credentials for Azure OpenAI as a dictionary.
  34. """
  35. config = self.get_provider_api_key(model_id=model_id)
  36. config['openai_api_type'] = 'azure'
  37. config['deployment_name'] = model_id.replace('.', '') if model_id else None
  38. return config
  39. def get_provider_name(self):
  40. return ProviderName.AZURE_OPENAI
  41. def get_provider_configs(self, obfuscated: bool = False) -> Union[str | dict]:
  42. """
  43. Returns the provider configs.
  44. """
  45. try:
  46. config = self.get_provider_api_key()
  47. except:
  48. config = {
  49. 'openai_api_type': 'azure',
  50. 'openai_api_version': '2023-03-15-preview',
  51. 'openai_api_base': '',
  52. 'openai_api_key': ''
  53. }
  54. if obfuscated:
  55. if not config.get('openai_api_key'):
  56. config = {
  57. 'openai_api_type': 'azure',
  58. 'openai_api_version': '2023-03-15-preview',
  59. 'openai_api_base': '',
  60. 'openai_api_key': ''
  61. }
  62. config['openai_api_key'] = self.obfuscated_token(config.get('openai_api_key'))
  63. return config
  64. return config
  65. def get_token_type(self):
  66. # TODO: change to dict when implemented
  67. return lambda value: value
  68. def config_validate(self, config: Union[dict | str]):
  69. """
  70. Validates the given config.
  71. """
  72. try:
  73. if not isinstance(config, dict):
  74. raise ValueError('Config must be a object.')
  75. if 'openai_api_version' not in config:
  76. config['openai_api_version'] = '2023-03-15-preview'
  77. self.get_models(credentials=config)
  78. except AzureAuthenticationError:
  79. raise ValidateFailedError('Azure OpenAI Credentials validation failed, please check your API Key.')
  80. except requests.ConnectionError:
  81. raise ValidateFailedError('Azure OpenAI Credentials validation failed, please check your API Base Endpoint.')
  82. except AzureRequestFailedError as ex:
  83. raise ValidateFailedError('Azure OpenAI Credentials validation failed, error: {}.'.format(str(ex)))
  84. except Exception as ex:
  85. logging.exception('Azure OpenAI Credentials validation failed')
  86. raise ex
  87. def get_encrypted_token(self, config: Union[dict | str]):
  88. """
  89. Returns the encrypted token.
  90. """
  91. return json.dumps({
  92. 'openai_api_type': 'azure',
  93. 'openai_api_version': '2023-03-15-preview',
  94. 'openai_api_base': config['openai_api_base'],
  95. 'openai_api_key': self.encrypt_token(config['openai_api_key'])
  96. })
  97. def get_decrypted_token(self, token: str):
  98. """
  99. Returns the decrypted token.
  100. """
  101. config = json.loads(token)
  102. config['openai_api_key'] = self.decrypt_token(config['openai_api_key'])
  103. return config
  104. class AzureAuthenticationError(Exception):
  105. pass
  106. class AzureRequestFailedError(Exception):
  107. pass