Skip to content

Commit

Permalink
Merge branch 'master' into update-support-email
Browse files Browse the repository at this point in the history
  • Loading branch information
clr-li committed Feb 6, 2024
2 parents ee0dcb1 + 4aa53e7 commit 9985147
Show file tree
Hide file tree
Showing 40 changed files with 1,026 additions and 394 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ ngrok http 8080
5. Copy the temporary access token there and set env var `WHATSAPP_ACCESS_TOKEN = XXXX`


**(Optional) Use the test script to send yourself messages**

```bash
python manage.py runscript test_wa_msg_send --script-args 104696745926402 +918764022384
```
Replace `+918764022384` with your number and `104696745926402` with the test number ID

## Dangerous postgres commands

Expand Down Expand Up @@ -178,3 +184,4 @@ rsync -P -a <username>@captain.us-1.gooey.ai:/home/<username>/fixture.json .
createdb -T template0 $PGDATABASE
pg_dump $SOURCE_DATABASE | psql -q $PGDATABASE
```

3 changes: 3 additions & 0 deletions bots/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class BotIntegrationAdmin(admin.ModelAdmin):
"Settings",
{
"fields": [
"streaming_enabled",
"show_feedback_buttons",
"analysis_run",
"view_analysis_results",
Expand Down Expand Up @@ -491,6 +492,7 @@ class MessageAdmin(admin.ModelAdmin):
"prev_msg_content",
"prev_msg_display_content",
"prev_msg_saved_run",
"response_time",
]
ordering = ["created_at"]
actions = [export_to_csv, export_to_excel]
Expand Down Expand Up @@ -550,6 +552,7 @@ def get_fieldsets(self, request, msg: Message = None):
"Analysis",
{
"fields": [
"response_time",
"analysis_result",
"analysis_run",
"question_answered",
Expand Down
20 changes: 20 additions & 0 deletions bots/migrations/0056_botintegration_streaming_enabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.7 on 2024-01-31 19:14

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("bots", "0055_workflowmetadata"),
]

operations = [
migrations.AddField(
model_name="botintegration",
name="streaming_enabled",
field=models.BooleanField(
default=False,
help_text="If set, the bot will stream messages to the frontend",
),
),
]
23 changes: 23 additions & 0 deletions bots/migrations/0057_message_response_time_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.7 on 2024-02-05 15:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bots', '0056_botintegration_streaming_enabled'),
]

operations = [
migrations.AddField(
model_name='message',
name='response_time',
field=models.DurationField(default=None, help_text='The time it took for the bot to respond to the corresponding user message', null=True),
),
migrations.AlterField(
model_name='botintegration',
name='streaming_enabled',
field=models.BooleanField(default=False, help_text='If set, the bot will stream messages to the frontend (Slack only)'),
),
]
83 changes: 73 additions & 10 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ class BotIntegration(models.Model):
help_text="If provided, the message content will be analyzed for this bot using this saved run",
)

streaming_enabled = models.BooleanField(
default=False,
help_text="If set, the bot will stream messages to the frontend (Slack only)",
)

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

Expand Down Expand Up @@ -712,7 +717,26 @@ def to_df_format(
"Bot": str(convo.bot_integration),
}
rows.append(row)
df = pd.DataFrame.from_records(rows)
df = pd.DataFrame.from_records(
rows,
columns=[
"Name",
"Messages",
"Correct Answers",
"Thumbs up",
"Thumbs down",
"Last Sent",
"First Sent",
"A7",
"A30",
"R1",
"R7",
"R30",
"Delta Hours",
"Created At",
"Bot",
],
)
return df


Expand Down Expand Up @@ -902,35 +926,55 @@ def to_df_format(
"Sent": message.created_at.astimezone(tz)
.replace(tzinfo=None)
.strftime("%b %d, %Y %I:%M %p"),
"Feedback": message.feedbacks.first().get_display_text()
if message.feedbacks.first()
else None, # only show first feedback as per Sean's request
"Feedback": (
message.feedbacks.first().get_display_text()
if message.feedbacks.first()
else None
), # only show first feedback as per Sean's request
"Analysis JSON": message.analysis_result,
"Response Time": (
message.response_time
and round(message.response_time.total_seconds(), 1)
),
}
rows.append(row)
df = pd.DataFrame.from_records(rows)
df = pd.DataFrame.from_records(
rows,
columns=[
"Name",
"Role",
"Message (EN)",
"Sent",
"Feedback",
"Analysis JSON",
"Response Time",
],
)
return df

def to_df_analysis_format(
self, tz=pytz.timezone(settings.TIME_ZONE), row_limit=10000
) -> "pd.DataFrame":
import pandas as pd

qs = self.filter(role=CHATML_ROLE_USER).prefetch_related("feedbacks")
qs = self.filter(role=CHATML_ROLE_ASSISSTANT).prefetch_related("feedbacks")
rows = []
for message in qs[:row_limit]:
message: Message
row = {
"Name": message.conversation.get_display_name(),
"Question (EN)": message.content,
"Answer (EN)": message.get_next_by_created_at().content,
"Question (EN)": message.get_previous_by_created_at().content,
"Answer (EN)": message.content,
"Sent": message.created_at.astimezone(tz)
.replace(tzinfo=None)
.strftime("%b %d, %Y %I:%M %p"),
"Analysis JSON": message.analysis_result,
}
rows.append(row)
df = pd.DataFrame.from_records(rows)
df = pd.DataFrame.from_records(
rows,
columns=["Name", "Question (EN)", "Answer (EN)", "Sent", "Analysis JSON"],
)
return df

def as_llm_context(self, limit: int = 100) -> list["ConversationEntry"]:
Expand Down Expand Up @@ -1012,6 +1056,12 @@ class Message(models.Model):
help_text="Subject of given question (DEPRECATED)",
)

response_time = models.DurationField(
default=None,
null=True,
help_text="The time it took for the bot to respond to the corresponding user message",
)

_analysis_started = False

objects = MessageQuerySet.as_manager()
Expand Down Expand Up @@ -1115,7 +1165,20 @@ def to_df_format(
"Question Answered": feedback.message.question_answered,
}
rows.append(row)
df = pd.DataFrame.from_records(rows)
df = pd.DataFrame.from_records(
rows,
columns=[
"Name",
"Question (EN)",
"Question Sent",
"Answer (EN)",
"Answer Sent",
"Rating",
"Feedback (EN)",
"Feedback Sent",
"Question Answered",
],
)
return df


Expand Down
2 changes: 1 addition & 1 deletion bots/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def send_broadcast_msg(
channel_is_personal=convo.slack_channel_is_personal,
username=bi.name,
token=bi.slack_access_token,
)
)[0]
case _:
raise NotImplementedError(
f"Platform {bi.platform} doesn't support broadcasts yet"
Expand Down
63 changes: 63 additions & 0 deletions bots/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,66 @@ def test_create_bot_integration_conversation_message(transactional_db):
assert message_b.role == CHATML_ROLE_ASSISSTANT
assert message_b.content == "Red, green, and yellow grow the best."
assert message_b.display_content == "Red, green, and yellow grow the best."


def test_stats_get_tabular_data_invalid_sorting_options(transactional_db):
from recipes.VideoBotsStats import VideoBotsStatsPage

page = VideoBotsStatsPage()

# setup
run_url = "https://my_run_url"
bi = BotIntegration.objects.create(
name="My Bot Integration",
saved_run=None,
billing_account_uid="fdnacsFSBQNKVW8z6tzhBLHKpAm1", # digital green's account id
user_language="en",
show_feedback_buttons=True,
platform=Platform.WHATSAPP,
wa_phone_number="my_whatsapp_number",
wa_phone_number_id="my_whatsapp_number_id",
)
convos = Conversation.objects.filter(bot_integration=bi)
msgs = Message.objects.filter(conversation__in=convos)

# valid option but no data
df = page.get_tabular_data(
bi, run_url, convos, msgs, "Answered Successfully", "Name"
)
assert df.shape[0] == 0
assert "Name" in df.columns

# valid option and data
convo = Conversation.objects.create(
bot_integration=bi,
state=ConvoState.INITIAL,
wa_phone_number="+919876543210",
)
Message.objects.create(
conversation=convo,
role=CHATML_ROLE_USER,
content="What types of chilies can be grown in Mumbai?",
display_content="What types of chilies can be grown in Mumbai?",
)
Message.objects.create(
conversation=convo,
role=CHATML_ROLE_ASSISSTANT,
content="Red, green, and yellow grow the best.",
display_content="Red, green, and yellow grow the best.",
analysis_result={"Answered": True},
)
convos = Conversation.objects.filter(bot_integration=bi)
msgs = Message.objects.filter(conversation__in=convos)
assert msgs.count() == 2
df = page.get_tabular_data(
bi, run_url, convos, msgs, "Answered Successfully", "Name"
)
assert df.shape[0] == 1
assert "Name" in df.columns

# invalid sort option should be ignored
df = page.get_tabular_data(
bi, run_url, convos, msgs, "Answered Successfully", "Invalid"
)
assert df.shape[0] == 1
assert "Name" in df.columns
20 changes: 12 additions & 8 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ def setup_render(self):

def refresh_state(self):
_, run_id, uid = extract_query_params(gooey_get_query_params())
channel = f"gooey-outputs/{self.slug_versions[0]}/{uid}/{run_id}"
channel = self.realtime_channel_name(run_id, uid)
output = realtime_pull([channel])[0]
if output:
st.session_state.update(output)

def render(self):
self.setup_render()

if self.get_run_state() == RecipeRunState.running:
if self.get_run_state(st.session_state) == RecipeRunState.running:
self.refresh_state()
else:
realtime_clear_subs()
Expand Down Expand Up @@ -1307,12 +1307,13 @@ def _render_input_col(self):
)
return submitted

def get_run_state(self) -> RecipeRunState:
if st.session_state.get(StateKeys.run_status):
@classmethod
def get_run_state(cls, state: dict[str, typing.Any]) -> RecipeRunState:
if state.get(StateKeys.run_status):
return RecipeRunState.running
elif st.session_state.get(StateKeys.error_msg):
elif state.get(StateKeys.error_msg):
return RecipeRunState.failed
elif st.session_state.get(StateKeys.run_time):
elif state.get(StateKeys.run_time):
return RecipeRunState.completed
else:
# when user is at a recipe root, and not running anything
Expand All @@ -1331,7 +1332,7 @@ def _render_output_col(self, submitted: bool):

self._render_before_output()

run_state = self.get_run_state()
run_state = self.get_run_state(st.session_state)
match run_state:
case RecipeRunState.completed:
self._render_completed_output()
Expand Down Expand Up @@ -1458,13 +1459,16 @@ def call_runner_task(self, example_id, run_id, uid, is_api_call=False):
run_id=run_id,
uid=uid,
state=st.session_state,
channel=f"gooey-outputs/{self.slug_versions[0]}/{uid}/{run_id}",
channel=self.realtime_channel_name(run_id, uid),
query_params=self.clean_query_params(
example_id=example_id, run_id=run_id, uid=uid
),
is_api_call=is_api_call,
)

def realtime_channel_name(self, run_id, uid):
return f"gooey-outputs/{self.slug_versions[0]}/{uid}/{run_id}"

def generate_credit_error_message(self, example_id, run_id, uid) -> str:
account_url = furl(settings.APP_BASE_URL) / "account/"
if self.request.user.is_anonymous:
Expand Down
11 changes: 8 additions & 3 deletions daras_ai_v2/bot_integration_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ def general_integration_settings(bi: BotIntegration):
st.session_state[f"_bi_user_language_{bi.id}"] = BotIntegration._meta.get_field(
"user_language"
).default
st.session_state[
f"_bi_show_feedback_buttons_{bi.id}"
] = BotIntegration._meta.get_field("show_feedback_buttons").default
st.session_state[f"_bi_show_feedback_buttons_{bi.id}"] = (
BotIntegration._meta.get_field("show_feedback_buttons").default
)
st.session_state[f"_bi_analysis_url_{bi.id}"] = None

bi.streaming_enabled = st.checkbox(
"**📡 Streaming Enabled**",
value=bi.streaming_enabled,
key=f"_bi_streaming_enabled_{bi.id}",
)
bi.show_feedback_buttons = st.checkbox(
"**👍🏾 👎🏽 Show Feedback Buttons**",
value=bi.show_feedback_buttons,
Expand Down
Loading

0 comments on commit 9985147

Please sign in to comment.