Pizzagpt.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. from ..typing import AsyncResult, Messages
  4. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  5. from .helper import format_prompt
  6. from ..providers.response import FinishReason
  7. class Pizzagpt(AsyncGeneratorProvider, ProviderModelMixin):
  8. url = "https://www.pizzagpt.it"
  9. api_endpoint = "/api/chatx-completion"
  10. working = False
  11. default_model = 'gpt-4o-mini'
  12. models = [default_model]
  13. @classmethod
  14. async def create_async_generator(
  15. cls,
  16. model: str,
  17. messages: Messages,
  18. proxy: str = None,
  19. **kwargs
  20. ) -> AsyncResult:
  21. headers = {
  22. "accept": "application/json",
  23. "accept-language": "en-US,en;q=0.9",
  24. "content-type": "application/json",
  25. "origin": cls.url,
  26. "referer": f"{cls.url}/en",
  27. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
  28. "x-secret": "Marinara"
  29. }
  30. async with ClientSession(headers=headers) as session:
  31. prompt = format_prompt(messages)
  32. data = {
  33. "question": prompt
  34. }
  35. async with session.post(f"{cls.url}{cls.api_endpoint}", json=data, proxy=proxy) as response:
  36. response.raise_for_status()
  37. response_json = await response.json()
  38. content = response_json.get("answer", response_json).get("content")
  39. if content:
  40. if "Misuse detected. please get in touch" in content:
  41. raise ValueError(content)
  42. yield content
  43. yield FinishReason("stop")