FastGpt.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from __future__ import annotations
  2. import json
  3. import random
  4. import requests
  5. from ...typing import Any, CreateResult
  6. from ..base_provider import AbstractProvider
  7. class FastGpt(AbstractProvider):
  8. url: str = 'https://chat9.fastgpt.me/'
  9. working = False
  10. needs_auth = False
  11. supports_stream = True
  12. supports_gpt_35_turbo = True
  13. supports_gpt_4 = False
  14. @staticmethod
  15. def create_completion(
  16. model: str,
  17. messages: list[dict[str, str]],
  18. stream: bool, **kwargs: Any) -> CreateResult:
  19. headers = {
  20. 'authority' : 'chat9.fastgpt.me',
  21. 'accept' : 'text/event-stream',
  22. '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',
  23. 'cache-control' : 'no-cache',
  24. 'content-type' : 'application/json',
  25. 'origin' : 'https://chat9.fastgpt.me',
  26. 'plugins' : '0',
  27. 'pragma' : 'no-cache',
  28. 'referer' : 'https://chat9.fastgpt.me/',
  29. 'sec-ch-ua' : '"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
  30. 'sec-ch-ua-mobile' : '?0',
  31. 'sec-ch-ua-platform': '"macOS"',
  32. 'sec-fetch-dest' : 'empty',
  33. 'sec-fetch-mode' : 'cors',
  34. 'sec-fetch-site' : 'same-origin',
  35. 'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
  36. 'usesearch' : 'false',
  37. 'x-requested-with' : 'XMLHttpRequest',
  38. }
  39. json_data = {
  40. 'messages' : messages,
  41. 'stream' : stream,
  42. 'model' : model,
  43. 'temperature' : kwargs.get('temperature', 0.5),
  44. 'presence_penalty' : kwargs.get('presence_penalty', 0),
  45. 'frequency_penalty' : kwargs.get('frequency_penalty', 0),
  46. 'top_p' : kwargs.get('top_p', 1),
  47. }
  48. subdomain = random.choice([
  49. 'jdaen979ew',
  50. 'chat9'
  51. ])
  52. response = requests.post(f'https://{subdomain}.fastgpt.me/api/openai/v1/chat/completions',
  53. headers=headers, json=json_data, stream=stream)
  54. for line in response.iter_lines():
  55. if line:
  56. try:
  57. if b'content' in line:
  58. line_json = json.loads(line.decode('utf-8').split('data: ')[1])
  59. token = line_json['choices'][0]['delta'].get(
  60. 'content'
  61. )
  62. if token:
  63. yield token
  64. except:
  65. continue