service.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from __future__ import annotations
  2. from typing import Union
  3. from .. import debug, version
  4. from ..errors import ProviderNotFoundError, ModelNotFoundError, ProviderNotWorkingError, StreamNotSupportedError
  5. from ..models import Model, ModelUtils, default
  6. from ..Provider import ProviderUtils
  7. from ..providers.types import BaseRetryProvider, ProviderType
  8. from ..providers.retry_provider import IterProvider
  9. def convert_to_provider(provider: str) -> ProviderType:
  10. if " " in provider:
  11. provider_list = [ProviderUtils.convert[p] for p in provider.split() if p in ProviderUtils.convert]
  12. if not provider_list:
  13. raise ProviderNotFoundError(f'Providers not found: {provider}')
  14. provider = IterProvider(provider_list)
  15. elif provider in ProviderUtils.convert:
  16. provider = ProviderUtils.convert[provider]
  17. elif provider:
  18. raise ProviderNotFoundError(f'Provider not found: {provider}')
  19. return provider
  20. def get_model_and_provider(model : Union[Model, str],
  21. provider : Union[ProviderType, str, None],
  22. stream : bool,
  23. ignored : list[str] = None,
  24. ignore_working: bool = False,
  25. ignore_stream: bool = False) -> tuple[str, ProviderType]:
  26. """
  27. Retrieves the model and provider based on input parameters.
  28. Args:
  29. model (Union[Model, str]): The model to use, either as an object or a string identifier.
  30. provider (Union[ProviderType, str, None]): The provider to use, either as an object, a string identifier, or None.
  31. stream (bool): Indicates if the operation should be performed as a stream.
  32. ignored (list[str], optional): List of provider names to be ignored.
  33. ignore_working (bool, optional): If True, ignores the working status of the provider.
  34. ignore_stream (bool, optional): If True, ignores the streaming capability of the provider.
  35. Returns:
  36. tuple[str, ProviderType]: A tuple containing the model name and the provider type.
  37. Raises:
  38. ProviderNotFoundError: If the provider is not found.
  39. ModelNotFoundError: If the model is not found.
  40. ProviderNotWorkingError: If the provider is not working.
  41. StreamNotSupportedError: If streaming is not supported by the provider.
  42. """
  43. if debug.version_check:
  44. debug.version_check = False
  45. version.utils.check_version()
  46. if isinstance(provider, str):
  47. provider = convert_to_provider(provider)
  48. if isinstance(model, str):
  49. if model in ModelUtils.convert:
  50. model = ModelUtils.convert[model]
  51. if not provider:
  52. if not model:
  53. model = default
  54. elif isinstance(model, str):
  55. raise ModelNotFoundError(f'Model not found: {model}')
  56. provider = model.best_provider
  57. if not provider:
  58. raise ProviderNotFoundError(f'No provider found for model: {model}')
  59. if isinstance(model, Model):
  60. model = model.name
  61. if not ignore_working and not provider.working:
  62. raise ProviderNotWorkingError(f'{provider.__name__} is not working')
  63. if isinstance(provider, BaseRetryProvider):
  64. if not ignore_working:
  65. provider.providers = [p for p in provider.providers if p.working]
  66. if ignored:
  67. provider.providers = [p for p in provider.providers if p.__name__ not in ignored]
  68. if not ignore_stream and not provider.supports_stream and stream:
  69. raise StreamNotSupportedError(f'{provider.__name__} does not support "stream" argument')
  70. if model:
  71. debug.log(f'Using {provider.__name__} provider and {model} model')
  72. else:
  73. debug.log(f'Using {provider.__name__} provider')
  74. debug.last_provider = provider
  75. debug.last_model = model
  76. return model, provider
  77. def get_last_provider(as_dict: bool = False) -> Union[ProviderType, dict[str, str], None]:
  78. """
  79. Retrieves the last used provider.
  80. Args:
  81. as_dict (bool, optional): If True, returns the provider information as a dictionary.
  82. Returns:
  83. Union[ProviderType, dict[str, str]]: The last used provider, either as an object or a dictionary.
  84. """
  85. last = debug.last_provider
  86. if isinstance(last, BaseRetryProvider):
  87. last = last.last_provider
  88. if as_dict:
  89. if last:
  90. return {
  91. "name": last.__name__,
  92. "url": last.url,
  93. "model": debug.last_model,
  94. "label": getattr(last, "label", None) if hasattr(last, "label") else None
  95. }
  96. else:
  97. return {}
  98. return last