Skip to content

Commit

Permalink
Merge pull request xtekky#474 from sudouser777/feature/added_retry
Browse files Browse the repository at this point in the history
added retry for you
  • Loading branch information
xtekky authored May 6, 2023
2 parents 69f260e + 28597ed commit 48c0841
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
61 changes: 38 additions & 23 deletions gpt4free/you/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

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] = {}
Expand All @@ -31,7 +34,7 @@ def create(
detailed: bool = False,
debug: bool = False,
proxy: Optional[str] = None,
) -> PoeResponse:
) -> YouResponse:
if chat is None:
chat = []

Expand All @@ -41,30 +44,29 @@ def create(
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()
Expand All @@ -80,7 +82,7 @@ def create(
# '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']

Expand Down Expand Up @@ -108,5 +110,18 @@ def __get_headers() -> dict:
}

@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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pymailtm
Levenshtein
xtempmail
faker
retrying

0 comments on commit 48c0841

Please sign in to comment.