2023-10-02 15:01:15 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-22 12:22:33 +00:00
|
|
|
import random, string
|
2023-10-02 15:01:15 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2023-10-10 07:49:29 +00:00
|
|
|
from ..typing import AsyncResult, Messages
|
2023-10-02 15:01:15 +00:00
|
|
|
from ..requests import StreamSession
|
|
|
|
from .base_provider import AsyncGeneratorProvider, format_prompt
|
|
|
|
|
|
|
|
|
|
|
|
class Phind(AsyncGeneratorProvider):
|
|
|
|
url = "https://www.phind.com"
|
|
|
|
working = True
|
|
|
|
supports_gpt_4 = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-10 07:49:29 +00:00
|
|
|
messages: Messages,
|
2023-10-02 15:01:15 +00:00
|
|
|
proxy: str = None,
|
2023-10-09 08:22:17 +00:00
|
|
|
timeout: int = 120,
|
2023-10-02 15:01:15 +00:00
|
|
|
**kwargs
|
2023-10-10 07:49:29 +00:00
|
|
|
) -> AsyncResult:
|
2023-10-22 12:22:33 +00:00
|
|
|
chars = string.ascii_lowercase + string.digits
|
2023-10-02 15:01:15 +00:00
|
|
|
user_id = ''.join(random.choice(chars) for _ in range(24))
|
|
|
|
data = {
|
|
|
|
"question": format_prompt(messages),
|
|
|
|
"webResults": [],
|
|
|
|
"options": {
|
|
|
|
"date": datetime.now().strftime("%d.%m.%Y"),
|
|
|
|
"language": "en",
|
|
|
|
"detailed": True,
|
|
|
|
"anonUserId": user_id,
|
|
|
|
"answerModel": "GPT-4",
|
|
|
|
"creativeMode": False,
|
|
|
|
"customLinks": []
|
|
|
|
},
|
|
|
|
"context":""
|
|
|
|
}
|
|
|
|
headers = {
|
|
|
|
"Authority": cls.url,
|
|
|
|
"Accept": "application/json, text/plain, */*",
|
|
|
|
"Origin": cls.url,
|
|
|
|
"Referer": f"{cls.url}/"
|
|
|
|
}
|
2023-10-09 08:22:17 +00:00
|
|
|
async with StreamSession(
|
|
|
|
headers=headers,
|
|
|
|
timeout=(5, timeout),
|
|
|
|
proxies={"https": proxy},
|
|
|
|
impersonate="chrome107"
|
|
|
|
) as session:
|
2023-10-02 15:01:15 +00:00
|
|
|
async with session.post(f"{cls.url}/api/infer/answer", json=data) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
new_lines = 0
|
|
|
|
async for line in response.iter_lines():
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line.startswith(b"data: "):
|
|
|
|
line = line[6:]
|
|
|
|
if line.startswith(b"<PHIND_METADATA>"):
|
|
|
|
continue
|
|
|
|
if line:
|
|
|
|
if new_lines:
|
|
|
|
yield "".join(["\n" for _ in range(int(new_lines / 2))])
|
|
|
|
new_lines = 0
|
|
|
|
yield line.decode()
|
|
|
|
else:
|
|
|
|
new_lines += 1
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def params(cls):
|
|
|
|
params = [
|
|
|
|
("model", "str"),
|
|
|
|
("messages", "list[dict[str, str]]"),
|
|
|
|
("stream", "bool"),
|
|
|
|
("proxy", "str"),
|
2023-10-09 08:22:17 +00:00
|
|
|
("timeout", "int"),
|
2023-10-02 15:01:15 +00:00
|
|
|
]
|
|
|
|
param = ", ".join([": ".join(p) for p in params])
|
|
|
|
return f"g4f.provider.{cls.__name__} supports: ({param})"
|