DeepInfra.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from __future__ import annotations
  2. import requests
  3. from ...typing import AsyncResult, Messages
  4. from .OpenaiAPI import OpenaiAPI
  5. class DeepInfra(OpenaiAPI):
  6. label = "DeepInfra"
  7. url = "https://deepinfra.com"
  8. working = True
  9. needs_auth = True
  10. supports_stream = True
  11. supports_message_history = True
  12. default_model = "meta-llama/Meta-Llama-3.1-70B-Instruct"
  13. @classmethod
  14. def get_models(cls):
  15. if not cls.models:
  16. url = 'https://api.deepinfra.com/models/featured'
  17. models = requests.get(url).json()
  18. cls.models = [model['model_name'] for model in models if model["type"] == "text-generation"]
  19. return cls.models
  20. @classmethod
  21. def create_async_generator(
  22. cls,
  23. model: str,
  24. messages: Messages,
  25. stream: bool,
  26. api_base: str = "https://api.deepinfra.com/v1/openai",
  27. temperature: float = 0.7,
  28. max_tokens: int = 1028,
  29. **kwargs
  30. ) -> AsyncResult:
  31. headers = {
  32. 'Accept-Encoding': 'gzip, deflate, br',
  33. 'Accept-Language': 'en-US',
  34. 'Connection': 'keep-alive',
  35. 'Origin': 'https://deepinfra.com',
  36. 'Referer': 'https://deepinfra.com/',
  37. 'Sec-Fetch-Dest': 'empty',
  38. 'Sec-Fetch-Mode': 'cors',
  39. 'Sec-Fetch-Site': 'same-site',
  40. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
  41. 'X-Deepinfra-Source': 'web-embed',
  42. 'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
  43. 'sec-ch-ua-mobile': '?0',
  44. 'sec-ch-ua-platform': '"macOS"',
  45. }
  46. return super().create_async_generator(
  47. model, messages,
  48. stream=stream,
  49. api_base=api_base,
  50. temperature=temperature,
  51. max_tokens=max_tokens,
  52. headers=headers,
  53. **kwargs
  54. )