Hashnode.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. from ...typing import AsyncResult, Messages
  4. from ..base_provider import AsyncGeneratorProvider
  5. from ..helper import get_random_hex
  6. class SearchTypes():
  7. quick = "quick"
  8. code = "code"
  9. websearch = "websearch"
  10. class Hashnode(AsyncGeneratorProvider):
  11. url = "https://hashnode.com"
  12. working = False
  13. supports_message_history = True
  14. supports_gpt_35_turbo = True
  15. _sources = []
  16. @classmethod
  17. async def create_async_generator(
  18. cls,
  19. model: str,
  20. messages: Messages,
  21. search_type: str = SearchTypes.websearch,
  22. proxy: str = None,
  23. **kwargs
  24. ) -> AsyncResult:
  25. headers = {
  26. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
  27. "Accept": "*/*",
  28. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  29. "Accept-Encoding": "gzip, deflate, br",
  30. "Referer": f"{cls.url}/rix",
  31. "Content-Type": "application/json",
  32. "Origin": cls.url,
  33. "Connection": "keep-alive",
  34. "Sec-Fetch-Dest": "empty",
  35. "Sec-Fetch-Mode": "cors",
  36. "Sec-Fetch-Site": "same-origin",
  37. "Pragma": "no-cache",
  38. "Cache-Control": "no-cache",
  39. "TE": "trailers",
  40. }
  41. async with ClientSession(headers=headers) as session:
  42. prompt = messages[-1]["content"]
  43. cls._sources = []
  44. if search_type == "websearch":
  45. async with session.post(
  46. f"{cls.url}/api/ai/rix/search",
  47. json={"prompt": prompt},
  48. proxy=proxy,
  49. ) as response:
  50. response.raise_for_status()
  51. cls._sources = (await response.json())["result"]
  52. data = {
  53. "chatId": get_random_hex(),
  54. "history": messages,
  55. "prompt": prompt,
  56. "searchType": search_type,
  57. "urlToScan": None,
  58. "searchResults": cls._sources,
  59. }
  60. async with session.post(
  61. f"{cls.url}/api/ai/rix/completion",
  62. json=data,
  63. proxy=proxy,
  64. ) as response:
  65. response.raise_for_status()
  66. async for chunk in response.content.iter_any():
  67. if chunk:
  68. yield chunk.decode()
  69. @classmethod
  70. def get_sources(cls) -> list:
  71. return [{
  72. "title": source["name"],
  73. "url": source["url"]
  74. } for source in cls._sources]