File size: 1,720 Bytes
9a0f230 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
"""
freeGPT's gpt4 module
"""
from uuid import uuid4
from re import findall
from curl_cffi.requests import get, RequestsError
class Completion:
"""
This class provides methods for generating completions based on prompts.
"""
async def create(self, prompt):
"""
Generate a completion based on the provided prompt.
Args:
prompt (str): The input prompt to generate a completion from.
Returns:
str: The generated completion as a text string.
Raises:
Exception: If the response does not contain the expected "youChatToken".
"""
resp = get(
"https://you.com/api/streamingSearch",
headers={
"cache-control": "no-cache",
"referer": "https://you.com/search?q=gpt4&tbm=youchat",
"cookie": f"safesearch_guest=Off; uuid_guest={str(uuid4())}",
},
params={
"q": prompt,
"page": 1,
"count": 10,
"safeSearch": "Off",
"onShoppingPage": False,
"mkt": "",
"responseFilter": "WebPages,Translations,TimeZone,Computation,RelatedSearches",
"domain": "youchat",
"queryTraceId": str(uuid4()),
"chat": [],
},
impersonate="chrome107",
)
if "youChatToken" not in resp.text:
raise RequestsError("Unable to fetch response.")
return (
"".join(findall(r"{\"youChatToken\": \"(.*?)\"}", resp.text))
.replace("\\n", "\n")
.replace("\\\\", "\\")
.replace('\\"', '"')
) |