FastGpt.py 2.9 KB

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