Update app.py
Browse files
app.py
CHANGED
@@ -114,6 +114,7 @@ def messages():
|
|
114 |
msg_id = str(uuid.uuid4())
|
115 |
response_event = Event()
|
116 |
response_text = []
|
|
|
117 |
|
118 |
if not stream:
|
119 |
return handle_non_stream(previous_messages, msg_id, model, input_tokens)
|
@@ -121,7 +122,7 @@ def messages():
|
|
121 |
log_request(request.remote_addr, request.path, 200)
|
122 |
|
123 |
def generate():
|
124 |
-
total_output_tokens
|
125 |
|
126 |
yield create_event("message_start", {
|
127 |
"type": "message_start",
|
@@ -139,6 +140,16 @@ def messages():
|
|
139 |
yield create_event("content_block_start", {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}})
|
140 |
yield create_event("ping", {"type": "ping"})
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
def on_connect():
|
143 |
logging.info("Connected to Perplexity AI")
|
144 |
emit_data = {
|
@@ -157,25 +168,6 @@ def messages():
|
|
157 |
}
|
158 |
sio.emit('perplexity_ask', (previous_messages, emit_data))
|
159 |
|
160 |
-
def on_query_progress(data):
|
161 |
-
nonlocal total_output_tokens
|
162 |
-
if 'text' in data:
|
163 |
-
text = json.loads(data['text'])
|
164 |
-
chunk = text['chunks'][-1] if text['chunks'] else None
|
165 |
-
if chunk:
|
166 |
-
response_text.append(chunk)
|
167 |
-
chunk_tokens = calculate_tokens(chunk)
|
168 |
-
total_output_tokens += chunk_tokens
|
169 |
-
|
170 |
-
yield create_event("content_block_delta", {
|
171 |
-
"type": "content_block_delta",
|
172 |
-
"index": 0,
|
173 |
-
"delta": {"type": "text_delta", "text": chunk},
|
174 |
-
})
|
175 |
-
|
176 |
-
if data.get('final', False):
|
177 |
-
response_event.set()
|
178 |
-
|
179 |
def on_disconnect():
|
180 |
logging.info("Disconnected from Perplexity AI")
|
181 |
response_event.set()
|
@@ -195,6 +187,13 @@ def messages():
|
|
195 |
|
196 |
while not response_event.is_set():
|
197 |
sio.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
except Exception as e:
|
200 |
logging.error(f"Error during socket connection: {str(e)}")
|
|
|
114 |
msg_id = str(uuid.uuid4())
|
115 |
response_event = Event()
|
116 |
response_text = []
|
117 |
+
total_output_tokens = 0
|
118 |
|
119 |
if not stream:
|
120 |
return handle_non_stream(previous_messages, msg_id, model, input_tokens)
|
|
|
122 |
log_request(request.remote_addr, request.path, 200)
|
123 |
|
124 |
def generate():
|
125 |
+
nonlocal total_output_tokens
|
126 |
|
127 |
yield create_event("message_start", {
|
128 |
"type": "message_start",
|
|
|
140 |
yield create_event("content_block_start", {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}})
|
141 |
yield create_event("ping", {"type": "ping"})
|
142 |
|
143 |
+
def on_query_progress(data):
|
144 |
+
nonlocal total_output_tokens
|
145 |
+
if 'text' in data:
|
146 |
+
text = json.loads(data['text'])
|
147 |
+
chunk = text['chunks'][-1] if text['chunks'] else None
|
148 |
+
if chunk:
|
149 |
+
response_text.append(chunk)
|
150 |
+
chunk_tokens = calculate_tokens(chunk)
|
151 |
+
total_output_tokens += chunk_tokens
|
152 |
+
|
153 |
def on_connect():
|
154 |
logging.info("Connected to Perplexity AI")
|
155 |
emit_data = {
|
|
|
168 |
}
|
169 |
sio.emit('perplexity_ask', (previous_messages, emit_data))
|
170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
def on_disconnect():
|
172 |
logging.info("Disconnected from Perplexity AI")
|
173 |
response_event.set()
|
|
|
187 |
|
188 |
while not response_event.is_set():
|
189 |
sio.sleep(0.1)
|
190 |
+
while response_text:
|
191 |
+
chunk = response_text.pop(0)
|
192 |
+
yield create_event("content_block_delta", {
|
193 |
+
"type": "content_block_delta",
|
194 |
+
"index": 0,
|
195 |
+
"delta": {"type": "text_delta", "text": chunk},
|
196 |
+
})
|
197 |
|
198 |
except Exception as e:
|
199 |
logging.error(f"Error during socket connection: {str(e)}")
|