From 92cea724b2b3b2eb9615dd2dbcc85c85eecd5d9d Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Wed, 24 Apr 2024 18:59:07 +0530 Subject: [PATCH] add finish_reason to copilot output --- .../0067_alter_botintegration_user_language.py | 18 ++++++++++++++++++ bots/models.py | 3 ++- daras_ai_v2/bots.py | 2 +- daras_ai_v2/facebook_bots.py | 9 +++++---- recipes/VideoBots.py | 8 +++++--- 5 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 bots/migrations/0067_alter_botintegration_user_language.py diff --git a/bots/migrations/0067_alter_botintegration_user_language.py b/bots/migrations/0067_alter_botintegration_user_language.py new file mode 100644 index 000000000..98deea790 --- /dev/null +++ b/bots/migrations/0067_alter_botintegration_user_language.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2024-04-24 13:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('bots', '0066_workflowmetadata_price_multiplier'), + ] + + operations = [ + migrations.AlterField( + model_name='botintegration', + name='user_language', + field=models.TextField(blank=True, default='', help_text='The response language (same as user language in video bots)'), + ), + ] diff --git a/bots/models.py b/bots/models.py index 1902730b4..ddd104313 100644 --- a/bots/models.py +++ b/bots/models.py @@ -437,8 +437,9 @@ class BotIntegration(models.Model): db_index=True, ) user_language = models.TextField( - default="en", + default="", help_text="The response language (same as user language in video bots)", + blank=True, ) show_feedback_buttons = models.BooleanField( default=False, diff --git a/daras_ai_v2/bots.py b/daras_ai_v2/bots.py index fd69c5a18..096df9f06 100644 --- a/daras_ai_v2/bots.py +++ b/daras_ai_v2/bots.py @@ -363,7 +363,7 @@ def _process_and_send_msg( # if no text, send the run status as text update_msg_id = bot.send_run_status(update_msg_id=update_msg_id) continue # no text, wait for the next update - streaming_done = bot.run_status.lower().startswith("completed") + streaming_done = state.get("finish_reason") # send the response to the user if bot.can_update_message: update_msg_id = bot.send_msg( diff --git a/daras_ai_v2/facebook_bots.py b/daras_ai_v2/facebook_bots.py index 23b32b113..4a8e1036c 100644 --- a/daras_ai_v2/facebook_bots.py +++ b/daras_ai_v2/facebook_bots.py @@ -220,15 +220,16 @@ def send_msg_to( if audio and not video: # video already has audio # simple audio msg - messages.append( + messages.insert( + 0, { "type": "audio", "audio": {"link": audio}, - } + }, ) if documents: - messages += [ + messages = [ # simple document msg { "type": "document", @@ -238,7 +239,7 @@ def send_msg_to( }, } for link in documents - ] + ] + messages return send_wa_msgs_raw( bot_number=bot_number, diff --git a/recipes/VideoBots.py b/recipes/VideoBots.py index 6de7f2cdb..618ac0c2d 100644 --- a/recipes/VideoBots.py +++ b/recipes/VideoBots.py @@ -300,6 +300,8 @@ class ResponseModel(BaseModel): output_documents: list[str] | None reply_buttons: list[ReplyButton] | None + finish_reason: list[str] | None + def preview_image(self, state: dict) -> str | None: return DEFAULT_COPILOT_META_IMG @@ -1002,13 +1004,13 @@ def run_v2( if asr_msg: output_text = [asr_msg + "\n\n" + text for text in output_text] response.output_text = output_text - if all(entry.get("finish_reason") for entry in entries): + finish_reasons = [entry.get("finish_reason") for entry in entries] + if all(finish_reasons): if all_refs_list: apply_response_formattings_suffix( all_refs_list, response.output_text, citation_style ) - finish_reason = entries[0]["finish_reason"] - yield f"Completed with {finish_reason=}" # avoid changing this message since it's used to detect end of stream + response.finish_reason = finish_reasons else: yield f"Streaming{str(i + 1).translate(SUPERSCRIPT)} {model.value}..."