TeachAnything.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from __future__ import annotations
  2. from typing import Any, Dict
  3. from aiohttp import ClientSession, ClientTimeout
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  6. from .helper import format_prompt
  7. class TeachAnything(AsyncGeneratorProvider, ProviderModelMixin):
  8. url = "https://www.teach-anything.com"
  9. api_endpoint = "/api/generate"
  10. working = True
  11. default_model = "llama-3.1-70b"
  12. @classmethod
  13. async def create_async_generator(
  14. cls,
  15. model: str,
  16. messages: Messages,
  17. proxy: str | None = None,
  18. **kwargs: Any
  19. ) -> AsyncResult:
  20. headers = cls._get_headers()
  21. async with ClientSession(headers=headers) as session:
  22. prompt = format_prompt(messages)
  23. data = {"prompt": prompt}
  24. timeout = ClientTimeout(total=60)
  25. async with session.post(
  26. f"{cls.url}{cls.api_endpoint}",
  27. json=data,
  28. proxy=proxy,
  29. timeout=timeout
  30. ) as response:
  31. response.raise_for_status()
  32. buffer = b""
  33. async for chunk in response.content.iter_any():
  34. buffer += chunk
  35. try:
  36. decoded = buffer.decode('utf-8')
  37. yield decoded
  38. buffer = b""
  39. except UnicodeDecodeError:
  40. # If we can't decode, we'll wait for more data
  41. continue
  42. # Handle any remaining data in the buffer
  43. if buffer:
  44. try:
  45. yield buffer.decode('utf-8', errors='replace')
  46. except Exception as e:
  47. print(f"Error decoding final buffer: {e}")
  48. @staticmethod
  49. def _get_headers() -> Dict[str, str]:
  50. return {
  51. "accept": "*/*",
  52. "accept-language": "en-US,en;q=0.9",
  53. "content-type": "application/json",
  54. "dnt": "1",
  55. "origin": "https://www.teach-anything.com",
  56. "priority": "u=1, i",
  57. "referer": "https://www.teach-anything.com/",
  58. "sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
  59. "sec-ch-ua-mobile": "?0",
  60. "sec-ch-ua-platform": '"Linux"',
  61. "sec-fetch-dest": "empty",
  62. "sec-fetch-mode": "cors",
  63. "sec-fetch-site": "same-origin",
  64. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
  65. }