DeepInfra.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from __future__ import annotations
  2. import requests, json
  3. from ..typing import CreateResult, Messages
  4. from .base_provider import BaseProvider
  5. class DeepInfra(BaseProvider):
  6. url: str = "https://deepinfra.com"
  7. working: bool = True
  8. supports_stream: bool = True
  9. supports_message_history: bool = True
  10. @staticmethod
  11. def create_completion(model: str,
  12. messages: Messages,
  13. stream: bool,
  14. **kwargs) -> CreateResult:
  15. headers = {
  16. 'Accept-Language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
  17. 'Cache-Control': 'no-cache',
  18. 'Connection': 'keep-alive',
  19. 'Content-Type': 'application/json',
  20. 'Origin': 'https://deepinfra.com',
  21. 'Pragma': 'no-cache',
  22. 'Referer': 'https://deepinfra.com/',
  23. 'Sec-Fetch-Dest': 'empty',
  24. 'Sec-Fetch-Mode': 'cors',
  25. 'Sec-Fetch-Site': 'same-site',
  26. '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',
  27. 'X-Deepinfra-Source': 'web-embed',
  28. 'accept': 'text/event-stream',
  29. 'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
  30. 'sec-ch-ua-mobile': '?0',
  31. 'sec-ch-ua-platform': '"macOS"',
  32. }
  33. json_data = json.dumps({
  34. 'model' : 'meta-llama/Llama-2-70b-chat-hf',
  35. 'messages': messages,
  36. 'stream' : True}, separators=(',', ':'))
  37. response = requests.post('https://api.deepinfra.com/v1/openai/chat/completions',
  38. headers=headers, data=json_data, stream=True)
  39. response.raise_for_status()
  40. first = True
  41. for line in response.iter_content(chunk_size=1024):
  42. if line.startswith(b"data: [DONE]"):
  43. break
  44. elif line.startswith(b"data: "):
  45. chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
  46. if chunk:
  47. if first:
  48. chunk = chunk.lstrip()
  49. if chunk:
  50. first = False
  51. yield (chunk)