Skip to content

Commit 48c0841

Browse files
authored
Merge pull request xtekky#474 from sudouser777/feature/added_retry
added retry for you
2 parents 69f260e + 28597ed commit 48c0841

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

gpt4free/you/__init__.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
from fake_useragent import UserAgent
77
from pydantic import BaseModel
8+
from requests import RequestException
9+
from retrying import retry
810
from tls_client import Session
11+
from tls_client.response import Response
912

1013

11-
class PoeResponse(BaseModel):
14+
class YouResponse(BaseModel):
1215
text: Optional[str] = None
1316
links: List[str] = []
1417
extra: Dict[str, Any] = {}
@@ -31,7 +34,7 @@ def create(
3134
detailed: bool = False,
3235
debug: bool = False,
3336
proxy: Optional[str] = None,
34-
) -> PoeResponse:
37+
) -> YouResponse:
3538
if chat is None:
3639
chat = []
3740

@@ -41,30 +44,29 @@ def create(
4144
client.headers = Completion.__get_headers()
4245
client.proxies = proxies
4346

44-
response = client.get(
45-
f'https://you.com/api/streamingSearch',
46-
params={
47-
'q': prompt,
48-
'page': page,
49-
'count': count,
50-
'safeSearch': safe_search,
51-
'onShoppingPage': on_shopping_page,
52-
'mkt': mkt,
53-
'responseFilter': response_filter,
54-
'domain': domain,
55-
'queryTraceId': str(uuid4()) if query_trace_id is None else query_trace_id,
56-
'chat': str(chat), # {'question':'','answer':' ''}
57-
},
58-
)
47+
params = {
48+
'q': prompt,
49+
'page': page,
50+
'count': count,
51+
'safeSearch': safe_search,
52+
'onShoppingPage': on_shopping_page,
53+
'mkt': mkt,
54+
'responseFilter': response_filter,
55+
'domain': domain,
56+
'queryTraceId': str(uuid4()) if query_trace_id is None else query_trace_id,
57+
'chat': str(chat), # {'question':'','answer':' ''}
58+
}
59+
60+
try:
61+
response = Completion.__make_request(client, params)
62+
except Exception:
63+
return Completion.__get_failure_response()
5964

6065
if debug:
6166
print('\n\n------------------\n\n')
6267
print(response.text)
6368
print('\n\n------------------\n\n')
6469

65-
if 'youChatToken' not in response.text:
66-
return Completion.__get_failure_response()
67-
6870
you_chat_serp_results = re.search(
6971
r'(?<=event: youChatSerpResults\ndata:)(.*\n)*?(?=event: )', response.text
7072
).group()
@@ -80,7 +82,7 @@ def create(
8082
# 'slots' : loads(slots)
8183
}
8284

83-
response = PoeResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"'))
85+
response = YouResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"'))
8486
if include_links:
8587
response.links = json.loads(third_party_search_results)['search']['third_party_search_results']
8688

@@ -108,5 +110,18 @@ def __get_headers() -> dict:
108110
}
109111

110112
@staticmethod
111-
def __get_failure_response() -> PoeResponse:
112-
return PoeResponse(text='Unable to fetch the response, Please try again.')
113+
def __get_failure_response() -> YouResponse:
114+
return YouResponse(text='Unable to fetch the response, Please try again.')
115+
116+
@staticmethod
117+
@retry(
118+
wait_fixed=5000,
119+
stop_max_attempt_number=5,
120+
retry_on_exception=lambda e: isinstance(e, RequestException),
121+
)
122+
def __make_request(client: Session, params: dict) -> Response:
123+
response = client.get(f'https://you.com/api/streamingSearch', params=params)
124+
if 'youChatToken' not in response.text:
125+
print('retry')
126+
raise RequestException('Unable to get the response from server')
127+
return response

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ pymailtm
1515
Levenshtein
1616
xtempmail
1717
faker
18+
retrying

0 commit comments

Comments
 (0)