summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authort.me/xtekky <98614666+xtekky@users.noreply.github.com>2023-05-06 23:44:18 +0200
committerGitHub <noreply@github.com>2023-05-06 23:44:18 +0200
commit48c0841d8f570da9b1624b7394a3d8bd18ca2f0f (patch)
treee6638430cd66a37fe5514ac9bd9d9eb2e69bdf86
parentMerge pull request #475 from AlephZero255/fix_for_forefront.git (diff)
parentadded retry for you (diff)
downloadgpt4free-48c0841d8f570da9b1624b7394a3d8bd18ca2f0f.tar
gpt4free-48c0841d8f570da9b1624b7394a3d8bd18ca2f0f.tar.gz
gpt4free-48c0841d8f570da9b1624b7394a3d8bd18ca2f0f.tar.bz2
gpt4free-48c0841d8f570da9b1624b7394a3d8bd18ca2f0f.tar.lz
gpt4free-48c0841d8f570da9b1624b7394a3d8bd18ca2f0f.tar.xz
gpt4free-48c0841d8f570da9b1624b7394a3d8bd18ca2f0f.tar.zst
gpt4free-48c0841d8f570da9b1624b7394a3d8bd18ca2f0f.zip
Diffstat (limited to '')
-rw-r--r--gpt4free/you/__init__.py61
-rw-r--r--requirements.txt1
2 files changed, 39 insertions, 23 deletions
diff --git a/gpt4free/you/__init__.py b/gpt4free/you/__init__.py
index da22d05e..11847fb5 100644
--- a/gpt4free/you/__init__.py
+++ b/gpt4free/you/__init__.py
@@ -5,10 +5,13 @@ from uuid import uuid4
from fake_useragent import UserAgent
from pydantic import BaseModel
+from requests import RequestException
+from retrying import retry
from tls_client import Session
+from tls_client.response import Response
-class PoeResponse(BaseModel):
+class YouResponse(BaseModel):
text: Optional[str] = None
links: List[str] = []
extra: Dict[str, Any] = {}
@@ -31,7 +34,7 @@ class Completion:
detailed: bool = False,
debug: bool = False,
proxy: Optional[str] = None,
- ) -> PoeResponse:
+ ) -> YouResponse:
if chat is None:
chat = []
@@ -41,30 +44,29 @@ class Completion:
client.headers = Completion.__get_headers()
client.proxies = proxies
- response = client.get(
- f'https://you.com/api/streamingSearch',
- params={
- 'q': prompt,
- 'page': page,
- 'count': count,
- 'safeSearch': safe_search,
- 'onShoppingPage': on_shopping_page,
- 'mkt': mkt,
- 'responseFilter': response_filter,
- 'domain': domain,
- 'queryTraceId': str(uuid4()) if query_trace_id is None else query_trace_id,
- 'chat': str(chat), # {'question':'','answer':' ''}
- },
- )
+ params = {
+ 'q': prompt,
+ 'page': page,
+ 'count': count,
+ 'safeSearch': safe_search,
+ 'onShoppingPage': on_shopping_page,
+ 'mkt': mkt,
+ 'responseFilter': response_filter,
+ 'domain': domain,
+ 'queryTraceId': str(uuid4()) if query_trace_id is None else query_trace_id,
+ 'chat': str(chat), # {'question':'','answer':' ''}
+ }
+
+ try:
+ response = Completion.__make_request(client, params)
+ except Exception:
+ return Completion.__get_failure_response()
if debug:
print('\n\n------------------\n\n')
print(response.text)
print('\n\n------------------\n\n')
- if 'youChatToken' not in response.text:
- return Completion.__get_failure_response()
-
you_chat_serp_results = re.search(
r'(?<=event: youChatSerpResults\ndata:)(.*\n)*?(?=event: )', response.text
).group()
@@ -80,7 +82,7 @@ class Completion:
# 'slots' : loads(slots)
}
- response = PoeResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"'))
+ response = YouResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"'))
if include_links:
response.links = json.loads(third_party_search_results)['search']['third_party_search_results']
@@ -108,5 +110,18 @@ class Completion:
}
@staticmethod
- def __get_failure_response() -> PoeResponse:
- return PoeResponse(text='Unable to fetch the response, Please try again.')
+ def __get_failure_response() -> YouResponse:
+ return YouResponse(text='Unable to fetch the response, Please try again.')
+
+ @staticmethod
+ @retry(
+ wait_fixed=5000,
+ stop_max_attempt_number=5,
+ retry_on_exception=lambda e: isinstance(e, RequestException),
+ )
+ def __make_request(client: Session, params: dict) -> Response:
+ response = client.get(f'https://you.com/api/streamingSearch', params=params)
+ if 'youChatToken' not in response.text:
+ print('retry')
+ raise RequestException('Unable to get the response from server')
+ return response
diff --git a/requirements.txt b/requirements.txt
index 17d5d4b5..a9b36b0a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -15,3 +15,4 @@ pymailtm
Levenshtein
xtempmail
faker
+retrying \ No newline at end of file