Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import json
|
3 |
import uuid
|
4 |
from datetime import datetime
|
5 |
-
from flask import Flask, request, Response, jsonify
|
6 |
import socketio
|
7 |
import requests
|
8 |
import logging
|
@@ -100,13 +100,11 @@ def messages():
|
|
100 |
"usage": {"input_tokens": 0, "output_tokens": 0}
|
101 |
})
|
102 |
|
103 |
-
@stream_with_context
|
104 |
def generate():
|
105 |
previous_messages = "\n\n".join([msg['content'] for msg in json_body['messages']])
|
106 |
msg_id = str(uuid.uuid4())
|
107 |
response_event = Event()
|
108 |
response_text = []
|
109 |
-
client_disconnected = False
|
110 |
|
111 |
yield create_event("message_start", {
|
112 |
"type": "message_start",
|
@@ -150,9 +148,16 @@ def messages():
|
|
150 |
text = json.loads(data['text'])
|
151 |
chunks = text.get('chunks', [])
|
152 |
for chunk in chunks:
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
def on_disconnect():
|
158 |
logging.info("Disconnected from Perplexity AI")
|
@@ -160,31 +165,22 @@ def messages():
|
|
160 |
|
161 |
def on_connect_error(data):
|
162 |
logging.error(f"Connection error: {data}")
|
163 |
-
|
|
|
|
|
|
|
|
|
164 |
response_event.set()
|
165 |
|
166 |
sio.on('connect', on_connect)
|
167 |
sio.on('query_progress', on_query_progress)
|
|
|
168 |
sio.on('disconnect', on_disconnect)
|
169 |
sio.on('connect_error', on_connect_error)
|
170 |
|
171 |
try:
|
172 |
sio.connect('wss://www.perplexity.ai/', **connect_opts, headers=sio_opts['extraHeaders'])
|
173 |
-
|
174 |
-
while not response_event.is_set() and not client_disconnected:
|
175 |
-
sio.sleep(0.1)
|
176 |
-
while response_text:
|
177 |
-
chunk = response_text.pop(0)
|
178 |
-
try:
|
179 |
-
yield create_event("content_block_delta", {
|
180 |
-
"type": "content_block_delta",
|
181 |
-
"index": 0,
|
182 |
-
"delta": {"type": "text_delta", "text": chunk},
|
183 |
-
})
|
184 |
-
except GeneratorExit:
|
185 |
-
client_disconnected = True
|
186 |
-
break
|
187 |
-
|
188 |
except Exception as e:
|
189 |
logging.error(f"Error during socket connection: {str(e)}")
|
190 |
yield create_event("content_block_delta", {
|
@@ -196,21 +192,16 @@ def messages():
|
|
196 |
if sio.connected:
|
197 |
sio.disconnect()
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
yield create_event("message_stop", {"type": "message_stop"})
|
207 |
-
|
208 |
log_request(request.remote_addr, request.path, 200)
|
209 |
|
210 |
-
return Response(generate(), content_type='text/event-stream'
|
211 |
-
'Cache-Control': 'no-cache',
|
212 |
-
'X-Accel-Buffering': 'no'
|
213 |
-
})
|
214 |
|
215 |
except Exception as e:
|
216 |
logging.error(f"Request error: {str(e)}")
|
@@ -230,8 +221,8 @@ def server_error(error):
|
|
230 |
|
231 |
def create_event(event, data):
|
232 |
if isinstance(data, dict):
|
233 |
-
data = json.dumps(data
|
234 |
-
return f"event: {event}"
|
235 |
|
236 |
if __name__ == '__main__':
|
237 |
port = int(os.environ.get('PORT', 8081))
|
|
|
2 |
import json
|
3 |
import uuid
|
4 |
from datetime import datetime
|
5 |
+
from flask import Flask, request, Response, jsonify
|
6 |
import socketio
|
7 |
import requests
|
8 |
import logging
|
|
|
100 |
"usage": {"input_tokens": 0, "output_tokens": 0}
|
101 |
})
|
102 |
|
|
|
103 |
def generate():
|
104 |
previous_messages = "\n\n".join([msg['content'] for msg in json_body['messages']])
|
105 |
msg_id = str(uuid.uuid4())
|
106 |
response_event = Event()
|
107 |
response_text = []
|
|
|
108 |
|
109 |
yield create_event("message_start", {
|
110 |
"type": "message_start",
|
|
|
148 |
text = json.loads(data['text'])
|
149 |
chunks = text.get('chunks', [])
|
150 |
for chunk in chunks:
|
151 |
+
decoded_chunk = chunk.encode('utf-8').decode('unicode_escape')
|
152 |
+
response_text.append(decoded_chunk)
|
153 |
+
yield create_event("content_block_delta", {
|
154 |
+
"type": "content_block_delta",
|
155 |
+
"index": 0,
|
156 |
+
"delta": {"type": "text_delta", "text": decoded_chunk},
|
157 |
+
})
|
158 |
+
|
159 |
+
def on_query_complete(data):
|
160 |
+
response_event.set()
|
161 |
|
162 |
def on_disconnect():
|
163 |
logging.info("Disconnected from Perplexity AI")
|
|
|
165 |
|
166 |
def on_connect_error(data):
|
167 |
logging.error(f"Connection error: {data}")
|
168 |
+
yield create_event("content_block_delta", {
|
169 |
+
"type": "content_block_delta",
|
170 |
+
"index": 0,
|
171 |
+
"delta": {"type": "text_delta", "text": f"Error connecting to Perplexity AI: {data}"},
|
172 |
+
})
|
173 |
response_event.set()
|
174 |
|
175 |
sio.on('connect', on_connect)
|
176 |
sio.on('query_progress', on_query_progress)
|
177 |
+
sio.on('query_complete', on_query_complete)
|
178 |
sio.on('disconnect', on_disconnect)
|
179 |
sio.on('connect_error', on_connect_error)
|
180 |
|
181 |
try:
|
182 |
sio.connect('wss://www.perplexity.ai/', **connect_opts, headers=sio_opts['extraHeaders'])
|
183 |
+
response_event.wait(timeout=30) # 等待响应,最多30秒
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
except Exception as e:
|
185 |
logging.error(f"Error during socket connection: {str(e)}")
|
186 |
yield create_event("content_block_delta", {
|
|
|
192 |
if sio.connected:
|
193 |
sio.disconnect()
|
194 |
|
195 |
+
yield create_event("content_block_stop", {"type": "content_block_stop", "index": 0})
|
196 |
+
yield create_event("message_delta", {
|
197 |
+
"type": "message_delta",
|
198 |
+
"delta": {"stop_reason": "end_turn", "stop_sequence": None},
|
199 |
+
"usage": {"output_tokens": len(''.join(response_text))},
|
200 |
+
})
|
201 |
+
yield create_event("message_stop", {"type": "message_stop"})
|
|
|
|
|
202 |
log_request(request.remote_addr, request.path, 200)
|
203 |
|
204 |
+
return Response(generate(), content_type='text/event-stream')
|
|
|
|
|
|
|
205 |
|
206 |
except Exception as e:
|
207 |
logging.error(f"Request error: {str(e)}")
|
|
|
221 |
|
222 |
def create_event(event, data):
|
223 |
if isinstance(data, dict):
|
224 |
+
data = json.dumps(data)
|
225 |
+
return f"event: {event}\ndata: {data}\n\n"
|
226 |
|
227 |
if __name__ == '__main__':
|
228 |
port = int(os.environ.get('PORT', 8081))
|