ChatgptDuo.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import annotations
  2. from ...typing import Messages
  3. from curl_cffi.requests import AsyncSession
  4. from ..base_provider import AsyncProvider, format_prompt
  5. class ChatgptDuo(AsyncProvider):
  6. url = "https://chatgptduo.com"
  7. supports_gpt_35_turbo = True
  8. working = False
  9. @classmethod
  10. async def create_async(
  11. cls,
  12. model: str,
  13. messages: Messages,
  14. proxy: str = None,
  15. timeout: int = 120,
  16. **kwargs
  17. ) -> str:
  18. async with AsyncSession(
  19. impersonate="chrome107",
  20. proxies={"https": proxy},
  21. timeout=timeout
  22. ) as session:
  23. prompt = format_prompt(messages),
  24. data = {
  25. "prompt": prompt,
  26. "search": prompt,
  27. "purpose": "ask",
  28. }
  29. response = await session.post(f"{cls.url}/", data=data)
  30. response.raise_for_status()
  31. data = response.json()
  32. cls._sources = [{
  33. "title": source["title"],
  34. "url": source["link"],
  35. "snippet": source["snippet"]
  36. } for source in data["results"]]
  37. return data["answer"]
  38. @classmethod
  39. def get_sources(cls):
  40. return cls._sources