gpt4free/g4f/Provider/Phind.py

76 lines
2.7 KiB
Python
Raw Normal View History

from __future__ import annotations
2023-11-22 14:22:36 +00:00
from datetime import datetime
2023-11-22 14:22:36 +00:00
from ..typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider
from ..requests import StreamSession
2023-11-22 14:22:36 +00:00
class Phind(AsyncGeneratorProvider):
url = "https://www.phind.com"
working = True
supports_gpt_4 = True
supports_stream = True
2023-11-22 14:22:36 +00:00
supports_message_history = True
@classmethod
2023-11-22 14:22:36 +00:00
async def create_async_generator(
cls,
model: str,
2023-10-10 07:49:29 +00:00
messages: Messages,
proxy: str = None,
2023-10-09 08:22:17 +00:00
timeout: int = 120,
2023-11-22 14:22:36 +00:00
creative_mode: bool = False,
**kwargs
2023-11-22 14:22:36 +00:00
) -> AsyncResult:
headers = {
"Accept": "*/*",
"Origin": cls.url,
"Referer": f"{cls.url}/search",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
}
2023-11-22 14:22:36 +00:00
async with StreamSession(
impersonate="chrome110",
proxies={"https": proxy},
timeout=timeout
) as session:
prompt = messages[-1]["content"]
data = {
"question": prompt,
"questionHistory": [
message["content"] for message in messages[:-1] if message["role"] == "user"
],
"answerHistory": [
message["content"] for message in messages if message["role"] == "assistant"
],
"webResults": [],
"options": {
"date": datetime.now().strftime("%d.%m.%Y"),
"language": "en-US",
"detailed": True,
"anonUserId": "",
"answerModel": "GPT-4" if model.startswith("gpt-4") else "Phind Model",
"creativeMode": creative_mode,
"customLinks": []
},
"context": "",
2023-12-26 21:41:19 +00:00
"rewrittenQuestion": prompt,
"challenge": 0.21132115912208504
2023-11-22 14:22:36 +00:00
}
async with session.post(f"{cls.url}/api/infer/followup/answer", headers=headers, json=data) as response:
new_line = False
async for line in response.iter_lines():
if line.startswith(b"data: "):
chunk = line[6:]
2023-12-26 21:41:19 +00:00
if chunk.startswith(b"<PHIND_METADATA>") or chunk.startswith(b"<PHIND_INDICATOR>"):
2023-11-22 14:22:36 +00:00
pass
elif chunk:
yield chunk.decode()
elif new_line:
yield "\n"
new_line = False
else:
2023-12-26 21:41:19 +00:00
new_line = True