Spaces:
Sleeping
Sleeping
feat(app): Optimize message handling for video and URL references
Browse files- Enhanced the `message_handler` function to format video references synchronously and URL references asynchronously, improving concurrency and reducing perceived latency for users.
- Streamlined the process of sending messages by collecting video reference messages early and scheduling URL reference tasks for background processing.
app.py
CHANGED
|
@@ -404,9 +404,23 @@ async def message_handler(input_message: cl.Message):
|
|
| 404 |
|
| 405 |
response = cast(TutorialState, raw_response)
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
for msg in response["messages"]:
|
| 408 |
if isinstance(msg, FinalAnswer):
|
| 409 |
-
# Stream the final answer token-by-token for a typing effect
|
| 410 |
final_msg = cl.Message(content="", author=msg.type)
|
| 411 |
await final_msg.send()
|
| 412 |
tokens = list(msg.content)
|
|
@@ -416,22 +430,17 @@ async def message_handler(input_message: cl.Message):
|
|
| 416 |
if final_msg:
|
| 417 |
await final_msg.update()
|
| 418 |
|
|
|
|
| 419 |
await cl.Message(
|
| 420 |
content=f"Formatting {len(response['video_references'])} video references."
|
| 421 |
).send()
|
|
|
|
|
|
|
| 422 |
|
| 423 |
-
#
|
| 424 |
-
for v in get_unique(response["video_references"]):
|
| 425 |
-
await format_video_reference(v).send()
|
| 426 |
-
|
| 427 |
await cl.Message(
|
| 428 |
content=f"Formatting {len(response['url_references'])} website references."
|
| 429 |
).send()
|
| 430 |
-
|
| 431 |
-
# Send all unique URL references as separate messages (with screenshots if available)
|
| 432 |
-
url_reference_tasks = [
|
| 433 |
-
format_url_reference(u) for u in get_unique(response["url_references"])
|
| 434 |
-
]
|
| 435 |
url_reference_messages = await asyncio.gather(*url_reference_tasks)
|
| 436 |
for msg in url_reference_messages:
|
| 437 |
await msg.send()
|
|
|
|
| 404 |
|
| 405 |
response = cast(TutorialState, raw_response)
|
| 406 |
|
| 407 |
+
# Start formatting tasks early to maximize concurrency.
|
| 408 |
+
# Video reference formatting is synchronous, so we just collect the messages.
|
| 409 |
+
# URL reference formatting is asynchronous (may involve network I/O), so we schedule those as tasks.
|
| 410 |
+
# By starting the async tasks before streaming the answer, we allow them to run in the background while the answer is being streamed,
|
| 411 |
+
# reducing the total perceived latency for the user.
|
| 412 |
+
video_reference_messages = [
|
| 413 |
+
format_video_reference(v)
|
| 414 |
+
for v in get_unique(response["video_references"])
|
| 415 |
+
]
|
| 416 |
+
url_reference_tasks = [
|
| 417 |
+
asyncio.create_task(format_url_reference(u))
|
| 418 |
+
for u in get_unique(response["url_references"])
|
| 419 |
+
]
|
| 420 |
+
|
| 421 |
+
# Stream the final answer token-by-token for a typing effect
|
| 422 |
for msg in response["messages"]:
|
| 423 |
if isinstance(msg, FinalAnswer):
|
|
|
|
| 424 |
final_msg = cl.Message(content="", author=msg.type)
|
| 425 |
await final_msg.send()
|
| 426 |
tokens = list(msg.content)
|
|
|
|
| 430 |
if final_msg:
|
| 431 |
await final_msg.update()
|
| 432 |
|
| 433 |
+
# After streaming the answer, display video references (synchronous)
|
| 434 |
await cl.Message(
|
| 435 |
content=f"Formatting {len(response['video_references'])} video references."
|
| 436 |
).send()
|
| 437 |
+
for msg in video_reference_messages:
|
| 438 |
+
await msg.send()
|
| 439 |
|
| 440 |
+
# Await and display URL references (asynchronous)
|
|
|
|
|
|
|
|
|
|
| 441 |
await cl.Message(
|
| 442 |
content=f"Formatting {len(response['url_references'])} website references."
|
| 443 |
).send()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
url_reference_messages = await asyncio.gather(*url_reference_tasks)
|
| 445 |
for msg in url_reference_messages:
|
| 446 |
await msg.send()
|