XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

added retry for you

28597ed2
Raju Komati <komatiraju032@gmail.com>
提交于

代码差异

2 个文件 +39 -23
Modified gpt4free/you/__init__.py +38 -23
@@ -5,10 +5,13 @@ from uuid import uuid4
5 5
6 6 from fake_useragent import UserAgent
7 7 from pydantic import BaseModel
8 from requests import RequestException
9 from retrying import retry
8 10 from tls_client import Session
11 from tls_client.response import Response
9 12
10 13
11 class PoeResponse(BaseModel):
14 class YouResponse(BaseModel):
12 15 text: Optional[str] = None
13 16 links: List[str] = []
14 17 extra: Dict[str, Any] = {}
@@ -31,7 +34,7 @@ class Completion:
31 34 detailed: bool = False,
32 35 debug: bool = False,
33 36 proxy: Optional[str] = None,
34 ) -> PoeResponse:
37 ) -> YouResponse:
35 38 if chat is None:
36 39 chat = []
37 40
@@ -41,30 +44,29 @@ class Completion:
41 44 client.headers = Completion.__get_headers()
42 45 client.proxies = proxies
43 46
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()
59 64
60 65 if debug:
61 66 print('\n\n------------------\n\n')
62 67 print(response.text)
63 68 print('\n\n------------------\n\n')
64 69
65 if 'youChatToken' not in response.text:
66 return Completion.__get_failure_response()
67
68 70 you_chat_serp_results = re.search(
69 71 r'(?<=event: youChatSerpResults\ndata:)(.*\n)*?(?=event: )', response.text
70 72 ).group()
@@ -80,7 +82,7 @@ class Completion:
80 82 # 'slots' : loads(slots)
81 83 }
82 84
83 response = PoeResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"'))
85 response = YouResponse(text=text.replace('\\n', '\n').replace('\\\\', '\\').replace('\\"', '"'))
84 86 if include_links:
85 87 response.links = json.loads(third_party_search_results)['search']['third_party_search_results']
86 88
@@ -108,5 +110,18 @@ class Completion:
108 110 }
109 111
110 112 @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
Modified requirements.txt +1 -0
@@ -15,3 +15,4 @@ pymailtm
15 15 Levenshtein
16 16 xtempmail
17 17 faker
18 retrying