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

XFEstudio/gpt4free

phind major improvement ( stream )

removed timeout error, added data streaming. Soon integration into gpt clone

b2459a58
t.me/xtekky <98614666+xtekky@users.noreply.github.com>
提交于

代码差异

3 个文件 +141 -28
Modified README.md +15 -4
@@ -102,16 +102,27 @@ print(response.completion.choices[0].text)
102 102 ### Example: `phind` (use like openai pypi package) <a name="example-phind"></a>
103 103
104 104 ```python
105 # HELP WANTED: tls_client does not accept stream and timeout gets hit with long responses
106
107 105 import phind
108 106
109 prompt = 'hello world'
107 prompt = 'who won the quatar world cup'
108
109 # help needed: not getting newlines from the stream, please submit a PR if you know how to fix this
110 # stream completion
111 for result in phind.StreamingCompletion.create(
112 model = 'gpt-4',
113 prompt = prompt,
114 results = phind.Search.create(prompt, actualSearch = True), # create search (set actualSearch to False to disable internet)
115 creative = False,
116 detailed = False,
117 codeContext = ''): # up to 3000 chars of code
118
119 print(result.completion.choices[0].text, end='', flush=True)
110 120
121 # normal completion
111 122 result = phind.Completion.create(
112 123 model = 'gpt-4',
113 124 prompt = prompt,
114 results = phind.Search.create(prompt, actualSearch = False), # create search (set actualSearch to False to disable internet)
125 results = phind.Search.create(prompt, actualSearch = True), # create search (set actualSearch to False to disable internet)
115 126 creative = False,
116 127 detailed = False,
117 128 codeContext = '') # up to 3000 chars of code
Modified phind/__init__.py +110 -23
@@ -1,24 +1,11 @@
1 1 from urllib.parse import quote
2 from tls_client import Session
3 2 from time import time
4 3 from datetime import datetime
4 from queue import Queue, Empty
5 from threading import Thread
6 from re import findall
5 7
6 client = Session(client_identifier='chrome110')
7 client.headers = {
8 'authority': 'www.phind.com',
9 'accept': '*/*',
10 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
11 'content-type': 'application/json',
12 'origin': 'https://www.phind.com',
13 'referer': 'https://www.phind.com/search',
14 'sec-ch-ua': '"Chromium";v="110", "Google Chrome";v="110", "Not:A-Brand";v="99"',
15 'sec-ch-ua-mobile': '?0',
16 'sec-ch-ua-platform': '"macOS"',
17 'sec-fetch-dest': 'empty',
18 'sec-fetch-mode': 'cors',
19 'sec-fetch-site': 'same-origin',
20 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
21 }
8 from curl_cffi.requests import post
22 9
23 10 class PhindResponse:
24 11
@@ -81,11 +68,19 @@ class Search:
81 68 }
82 69 }
83 70
84 return client.post('https://www.phind.com/api/bing/search', json = {
85 'q' : prompt,
71 headers = {
72 'authority' : 'www.phind.com',
73 'origin' : 'https://www.phind.com',
74 'referer' : 'https://www.phind.com/search',
75 'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
76 }
77
78 return post('https://www.phind.com/api/bing/search', headers = headers, json = {
79 'q': prompt,
86 80 'userRankList': {},
87 81 'browserLanguage': language}).json()['rawBingResults']
88 82
83
89 84 class Completion:
90 85 def create(
91 86 model = 'gpt-4',
@@ -121,12 +116,19 @@ class Completion:
121 116 }
122 117 }
123 118
119 headers = {
120 'authority' : 'www.phind.com',
121 'origin' : 'https://www.phind.com',
122 'referer' : f'https://www.phind.com/search?q={quote(prompt)}&c=&source=searchbox&init=true',
123 'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
124 }
125
124 126 completion = ''
125 response = client.post('https://www.phind.com/api/infer/answer', json=json_data, timeout_seconds=200)
127 response = post('https://www.phind.com/api/infer/answer', headers = headers, json = json_data, timeout=99999)
126 128 for line in response.text.split('\r\n\r\n'):
127 129 completion += (line.replace('data: ', ''))
128
129 return PhindResponse({
130
131 return PhindResponse({
130 132 'id' : f'cmpl-1337-{int(time())}',
131 133 'object' : 'text_completion',
132 134 'created': int(time()),
@@ -142,4 +144,89 @@ class Completion:
142 144 'completion_tokens' : len(completion),
143 145 'total_tokens' : len(prompt) + len(completion)
144 146 }
145 })
147 })
148
149
150 class StreamingCompletion:
151 message_queue = Queue()
152 stream_completed = False
153
154 def request(model, prompt, results, creative, detailed, codeContext, language) -> None:
155
156 models = {
157 'gpt-4' : 'expert',
158 'gpt-3.5-turbo' : 'intermediate',
159 'gpt-3.5': 'intermediate',
160 }
161
162 json_data = {
163 'question' : prompt,
164 'bingResults' : results,
165 'codeContext' : codeContext,
166 'options': {
167 'skill' : models[model],
168 'date' : datetime.now().strftime("%d/%m/%Y"),
169 'language': language,
170 'detailed': detailed,
171 'creative': creative
172 }
173 }
174
175 stream_req = post('https://www.phind.com/api/infer/answer', json=json_data, timeout=99999,
176 content_callback = StreamingCompletion.handle_stream_response,
177 headers = {
178 'authority' : 'www.phind.com',
179 'origin' : 'https://www.phind.com',
180 'referer' : f'https://www.phind.com/search?q={quote(prompt)}&c=&source=searchbox&init=true',
181 'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
182 })
183
184 StreamingCompletion.stream_completed = True
185
186 @staticmethod
187 def create(
188 model : str = 'gpt-4',
189 prompt : str = '',
190 results : dict = None,
191 creative : bool = False,
192 detailed : bool = False,
193 codeContext : str = '',
194 language : str = 'en'):
195
196 if results is None:
197 results = Search.create(prompt, actualSearch = True)
198
199 if len(codeContext) > 2999:
200 raise ValueError('codeContext must be less than 3000 characters')
201
202 Thread(target = StreamingCompletion.request, args = [
203 model, prompt, results, creative, detailed, codeContext, language]).start()
204
205 while StreamingCompletion.stream_completed != True or not StreamingCompletion.message_queue.empty():
206 try:
207 message = StreamingCompletion.message_queue.get(timeout=0)
208 for token in findall(r'(?<=data: )(.+?)(?=\r\n\r\n)', message.decode()):
209 yield PhindResponse({
210 'id' : f'cmpl-1337-{int(time())}',
211 'object' : 'text_completion',
212 'created': int(time()),
213 'model' : model,
214 'choices': [{
215 'text' : token,
216 'index' : 0,
217 'logprobs' : None,
218 'finish_reason' : 'stop'
219 }],
220 'usage': {
221 'prompt_tokens' : len(prompt),
222 'completion_tokens' : len(token),
223 'total_tokens' : len(prompt) + len(token)
224 }
225 })
226
227 except Empty:
228 pass
229
230 @staticmethod
231 def handle_stream_response(response):
232 StreamingCompletion.message_queue.put(response)
Modified testing/phind_test.py +16 -1
@@ -2,6 +2,7 @@ import phind
2 2
3 3 prompt = 'hello world'
4 4
5 # normal completion
5 6 result = phind.Completion.create(
6 7 model = 'gpt-4',
7 8 prompt = prompt,
@@ -10,4 +11,18 @@ result = phind.Completion.create(
10 11 detailed = False,
11 12 codeContext = '') # up to 3000 chars of code
12 13
13 print(result.completion.choices[0].text)
14 print(result.completion.choices[0].text)
15
16 prompt = 'who won the quatar world cup'
17
18 # help needed: not getting newlines from the stream, please submit a PR if you know how to fix this
19 # stream completion
20 for result in phind.StreamingCompletion.create(
21 model = 'gpt-3.5',
22 prompt = prompt,
23 results = phind.Search.create(prompt, actualSearch = True), # create search (set actualSearch to False to disable internet)
24 creative = False,
25 detailed = False,
26 codeContext = ''): # up to 3000 chars of code
27
28 print(result.completion.choices[0].text, end='', flush=True)