t.me/xtekky
commited on
Commit
·
b2459a5
1
Parent(s):
b31d053
phind major improvement ( stream )
Browse filesremoved timeout error, added data streaming. Soon integration into gpt clone
- README.md +15 -4
- phind/__init__.py +110 -23
- testing/phind_test.py +16 -1
README.md
CHANGED
@@ -102,16 +102,27 @@ print(response.completion.choices[0].text)
|
|
102 |
### Example: `phind` (use like openai pypi package) <a name="example-phind"></a>
|
103 |
|
104 |
```python
|
105 |
-
# HELP WANTED: tls_client does not accept stream and timeout gets hit with long responses
|
106 |
-
|
107 |
import phind
|
108 |
|
109 |
-
prompt = '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
|
|
111 |
result = phind.Completion.create(
|
112 |
model = 'gpt-4',
|
113 |
prompt = prompt,
|
114 |
-
results = phind.Search.create(prompt, actualSearch =
|
115 |
creative = False,
|
116 |
detailed = False,
|
117 |
codeContext = '') # up to 3000 chars of code
|
|
|
102 |
### Example: `phind` (use like openai pypi package) <a name="example-phind"></a>
|
103 |
|
104 |
```python
|
|
|
|
|
105 |
import phind
|
106 |
|
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)
|
120 |
|
121 |
+
# normal completion
|
122 |
result = phind.Completion.create(
|
123 |
model = 'gpt-4',
|
124 |
prompt = prompt,
|
125 |
+
results = phind.Search.create(prompt, actualSearch = True), # create search (set actualSearch to False to disable internet)
|
126 |
creative = False,
|
127 |
detailed = False,
|
128 |
codeContext = '') # up to 3000 chars of code
|
phind/__init__.py
CHANGED
@@ -1,24 +1,11 @@
|
|
1 |
from urllib.parse import quote
|
2 |
-
from tls_client import Session
|
3 |
from time import time
|
4 |
from datetime import datetime
|
|
|
|
|
|
|
5 |
|
6 |
-
|
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 |
-
}
|
22 |
|
23 |
class PhindResponse:
|
24 |
|
@@ -81,11 +68,19 @@ class Search:
|
|
81 |
}
|
82 |
}
|
83 |
|
84 |
-
|
85 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
'userRankList': {},
|
87 |
'browserLanguage': language}).json()['rawBingResults']
|
88 |
|
|
|
89 |
class Completion:
|
90 |
def create(
|
91 |
model = 'gpt-4',
|
@@ -121,12 +116,19 @@ class Completion:
|
|
121 |
}
|
122 |
}
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
completion = ''
|
125 |
-
response =
|
126 |
for line in response.text.split('\r\n\r\n'):
|
127 |
completion += (line.replace('data: ', ''))
|
128 |
-
|
129 |
-
return
|
130 |
'id' : f'cmpl-1337-{int(time())}',
|
131 |
'object' : 'text_completion',
|
132 |
'created': int(time()),
|
@@ -142,4 +144,89 @@ class Completion:
|
|
142 |
'completion_tokens' : len(completion),
|
143 |
'total_tokens' : len(prompt) + len(completion)
|
144 |
}
|
145 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from urllib.parse import quote
|
|
|
2 |
from time import time
|
3 |
from datetime import datetime
|
4 |
+
from queue import Queue, Empty
|
5 |
+
from threading import Thread
|
6 |
+
from re import findall
|
7 |
|
8 |
+
from curl_cffi.requests import post
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
class PhindResponse:
|
11 |
|
|
|
68 |
}
|
69 |
}
|
70 |
|
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,
|
80 |
'userRankList': {},
|
81 |
'browserLanguage': language}).json()['rawBingResults']
|
82 |
|
83 |
+
|
84 |
class Completion:
|
85 |
def create(
|
86 |
model = 'gpt-4',
|
|
|
116 |
}
|
117 |
}
|
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 |
+
|
126 |
completion = ''
|
127 |
+
response = post('https://www.phind.com/api/infer/answer', headers = headers, json = json_data, timeout=99999)
|
128 |
for line in response.text.split('\r\n\r\n'):
|
129 |
completion += (line.replace('data: ', ''))
|
130 |
+
|
131 |
+
return PhindResponse({
|
132 |
'id' : f'cmpl-1337-{int(time())}',
|
133 |
'object' : 'text_completion',
|
134 |
'created': int(time()),
|
|
|
144 |
'completion_tokens' : len(completion),
|
145 |
'total_tokens' : len(prompt) + len(completion)
|
146 |
}
|
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)
|
testing/phind_test.py
CHANGED
@@ -2,6 +2,7 @@ import phind
|
|
2 |
|
3 |
prompt = 'hello world'
|
4 |
|
|
|
5 |
result = phind.Completion.create(
|
6 |
model = 'gpt-4',
|
7 |
prompt = prompt,
|
@@ -10,4 +11,18 @@ result = phind.Completion.create(
|
|
10 |
detailed = False,
|
11 |
codeContext = '') # up to 3000 chars of code
|
12 |
|
13 |
-
print(result.completion.choices[0].text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
prompt = 'hello world'
|
4 |
|
5 |
+
# normal completion
|
6 |
result = phind.Completion.create(
|
7 |
model = 'gpt-4',
|
8 |
prompt = prompt,
|
|
|
11 |
detailed = False,
|
12 |
codeContext = '') # up to 3000 chars of code
|
13 |
|
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)
|