DeepInfraChat.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from __future__ import annotations
  2. from ..typing import AsyncResult, Messages, ImagesType
  3. from .template import OpenaiTemplate
  4. from ..image import to_data_uri
  5. class DeepInfraChat(OpenaiTemplate):
  6. url = "https://deepinfra.com/chat"
  7. api_base = "https://api.deepinfra.com/v1/openai"
  8. working = True
  9. default_model = 'meta-llama/Llama-3.3-70B-Instruct-Turbo'
  10. default_vision_model = 'meta-llama/Llama-3.2-90B-Vision-Instruct'
  11. vision_models = [default_vision_model, 'openbmb/MiniCPM-Llama3-V-2_5']
  12. models = [
  13. 'meta-llama/Meta-Llama-3.1-8B-Instruct',
  14. default_model,
  15. 'meta-llama/Llama-3.3-70B-Instruct',
  16. 'deepseek-ai/DeepSeek-V3',
  17. 'mistralai/Mistral-Small-24B-Instruct-2501',
  18. 'deepseek-ai/DeepSeek-R1',
  19. 'deepseek-ai/DeepSeek-R1-Distill-Llama-70B',
  20. 'deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',
  21. 'microsoft/phi-4',
  22. 'microsoft/WizardLM-2-8x22B',
  23. 'Qwen/Qwen2.5-72B-Instruct',
  24. '01-ai/Yi-34B-Chat',
  25. 'Qwen/Qwen2-72B-Instruct',
  26. 'cognitivecomputations/dolphin-2.6-mixtral-8x7b',
  27. 'cognitivecomputations/dolphin-2.9.1-llama-3-70b',
  28. 'databricks/dbrx-instruct',
  29. 'deepinfra/airoboros-70b',
  30. 'lizpreciatior/lzlv_70b_fp16_hf',
  31. 'microsoft/WizardLM-2-7B',
  32. 'mistralai/Mixtral-8x22B-Instruct-v0.1',
  33. ] + vision_models
  34. model_aliases = {
  35. "llama-3.1-8b": "meta-llama/Meta-Llama-3.1-8B-Instruct",
  36. "llama-3.2-90b": "meta-llama/Llama-3.2-90B-Vision-Instruct",
  37. "llama-3.3-70b": "meta-llama/Llama-3.3-70B-Instruct-Turbo",
  38. "llama-3.3-70b": "meta-llama/Llama-3.3-70B-Instruct",
  39. "deepseek-v3": "deepseek-ai/DeepSeek-V3",
  40. "mixtral-small-28b": "mistralai/Mistral-Small-24B-Instruct-2501",
  41. "deepseek-r1": "deepseek-ai/DeepSeek-R1",
  42. "deepseek-r1-distill-llama": "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
  43. "deepseek-r1-distill-qwen": "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
  44. "phi-4": "microsoft/phi-4",
  45. "wizardlm-2-8x22b": "microsoft/WizardLM-2-8x22B",
  46. "yi-34b": "01-ai/Yi-34B-Chat",
  47. "qwen-2-72b": "Qwen/Qwen2-72B-Instruct",
  48. "dolphin-2.6": "cognitivecomputations/dolphin-2.6-mixtral-8x7b",
  49. "dolphin-2.9": "cognitivecomputations/dolphin-2.9.1-llama-3-70b",
  50. "dbrx-instruct": "databricks/dbrx-instruct",
  51. "airoboros-70b": "deepinfra/airoboros-70b",
  52. "lzlv-70b": "lizpreciatior/lzlv_70b_fp16_hf",
  53. "wizardlm-2-7b": "microsoft/WizardLM-2-7B",
  54. "mixtral-8x22b": "mistralai/Mixtral-8x22B-Instruct-v0.1",
  55. "minicpm-2.5": "openbmb/MiniCPM-Llama3-V-2_5",
  56. }
  57. @classmethod
  58. async def create_async_generator(
  59. cls,
  60. model: str,
  61. messages: Messages,
  62. stream: bool = True,
  63. top_p: float = 0.9,
  64. temperature: float = 0.7,
  65. max_tokens: int = None,
  66. headers: dict = {},
  67. images: ImagesType = None,
  68. **kwargs
  69. ) -> AsyncResult:
  70. headers = {
  71. 'Accept-Language': 'en-US,en;q=0.9',
  72. 'Origin': 'https://deepinfra.com',
  73. 'Referer': 'https://deepinfra.com/',
  74. 'X-Deepinfra-Source': 'web-page',
  75. 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
  76. **headers
  77. }
  78. if images is not None:
  79. if not model or model not in cls.models:
  80. model = cls.default_vision_model
  81. if messages:
  82. last_message = messages[-1].copy()
  83. last_message["content"] = [
  84. *[{
  85. "type": "image_url",
  86. "image_url": {"url": to_data_uri(image)}
  87. } for image, _ in images],
  88. {
  89. "type": "text",
  90. "text": last_message["content"]
  91. }
  92. ]
  93. messages[-1] = last_message
  94. async for chunk in super().create_async_generator(
  95. model,
  96. messages,
  97. headers=headers,
  98. stream=stream,
  99. top_p=top_p,
  100. temperature=temperature,
  101. max_tokens=max_tokens,
  102. **kwargs
  103. ):
  104. yield chunk