Airforce.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from __future__ import annotations
  2. from typing import Any, Dict
  3. import inspect
  4. from aiohttp import ClientSession
  5. from ..typing import AsyncResult, Messages
  6. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  7. from .helper import format_prompt
  8. from .airforce.AirforceChat import AirforceChat
  9. from .airforce.AirforceImage import AirforceImage
  10. class Airforce(AsyncGeneratorProvider, ProviderModelMixin):
  11. url = "https://api.airforce"
  12. api_endpoint_completions = AirforceChat.api_endpoint
  13. api_endpoint_imagine2 = AirforceImage.api_endpoint
  14. working = True
  15. supports_stream = AirforceChat.supports_stream
  16. supports_system_message = AirforceChat.supports_system_message
  17. supports_message_history = AirforceChat.supports_message_history
  18. default_model = AirforceChat.default_model
  19. models = [*AirforceChat.models, *AirforceImage.models]
  20. model_aliases = {
  21. **AirforceChat.model_aliases,
  22. **AirforceImage.model_aliases
  23. }
  24. @classmethod
  25. def get_model(cls, model: str) -> str:
  26. if model in cls.models:
  27. return model
  28. elif model in cls.model_aliases:
  29. return cls.model_aliases[model]
  30. else:
  31. return cls.default_model
  32. @classmethod
  33. async def create_async_generator(cls, model: str, messages: Messages, **kwargs) -> AsyncResult:
  34. model = cls.get_model(model)
  35. provider = AirforceChat if model in AirforceChat.text_models else AirforceImage
  36. if model not in provider.models:
  37. raise ValueError(f"Unsupported model: {model}")
  38. # Get the signature of the provider's create_async_generator method
  39. sig = inspect.signature(provider.create_async_generator)
  40. # Filter kwargs to only include parameters that the provider's method accepts
  41. filtered_kwargs = {k: v for k, v in kwargs.items() if k in sig.parameters}
  42. # Add model and messages to filtered_kwargs
  43. filtered_kwargs['model'] = model
  44. filtered_kwargs['messages'] = messages
  45. async for result in provider.create_async_generator(**filtered_kwargs):
  46. yield result