FlowGpt.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import annotations
  2. import json
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  6. class FlowGpt(AsyncGeneratorProvider, ProviderModelMixin):
  7. url = "https://flowgpt.com/chat"
  8. working = True
  9. supports_gpt_35_turbo = True
  10. supports_message_history = True
  11. default_model = "gpt-3.5-turbo"
  12. models = [
  13. "gpt-3.5-turbo",
  14. "gpt-3.5-long",
  15. "google-gemini",
  16. "claude-v2",
  17. "llama2-13b"
  18. ]
  19. model_aliases = {
  20. "gemini": "google-gemini",
  21. "gemini-pro": "google-gemini"
  22. }
  23. @classmethod
  24. async def create_async_generator(
  25. cls,
  26. model: str,
  27. messages: Messages,
  28. proxy: str = None,
  29. **kwargs
  30. ) -> AsyncResult:
  31. model = cls.get_model(model)
  32. headers = {
  33. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0",
  34. "Accept": "*/*",
  35. "Accept-Language": "en-US;q=0.7,en;q=0.3",
  36. "Accept-Encoding": "gzip, deflate, br",
  37. "Referer": "https://flowgpt.com/",
  38. "Content-Type": "application/json",
  39. "Authorization": "Bearer null",
  40. "Origin": "https://flowgpt.com",
  41. "Connection": "keep-alive",
  42. "Sec-Fetch-Dest": "empty",
  43. "Sec-Fetch-Mode": "cors",
  44. "Sec-Fetch-Site": "same-site",
  45. "TE": "trailers"
  46. }
  47. async with ClientSession(headers=headers) as session:
  48. history = [message for message in messages[:-1] if message["role"] != "system"]
  49. system_message = "\n".join([message["content"] for message in messages if message["role"] == "system"])
  50. if not system_message:
  51. system_message = "You are helpful assistant. Follow the user's instructions carefully."
  52. data = {
  53. "model": model,
  54. "nsfw": False,
  55. "question": messages[-1]["content"],
  56. "history": [{"role": "assistant", "content": "Hello, how can I help you today?"}, *history],
  57. "system": system_message,
  58. "temperature": kwargs.get("temperature", 0.7),
  59. "promptId": f"model-{model}",
  60. "documentIds": [],
  61. "chatFileDocumentIds": [],
  62. "generateImage": False,
  63. "generateAudio": False
  64. }
  65. async with session.post("https://backend-k8s.flowgpt.com/v2/chat-anonymous", json=data, proxy=proxy) as response:
  66. response.raise_for_status()
  67. async for chunk in response.content:
  68. if chunk.strip():
  69. message = json.loads(chunk)
  70. if "event" not in message:
  71. continue
  72. if message["event"] == "text":
  73. yield message["data"]