Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dara-network/ddgai into all-tabs-…
Browse files Browse the repository at this point in the history
…meta
  • Loading branch information
devxpy committed Feb 12, 2024
2 parents c62883d + be8f58e commit 09d02c6
Show file tree
Hide file tree
Showing 70 changed files with 1,806 additions and 391 deletions.
23 changes: 19 additions & 4 deletions bots/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django import forms
from django.conf import settings
from django.contrib import admin
from django.db.models import Max, Count, F
from django.db.models import Max, Count, F, Sum
from django.template import loader
from django.utils import dateformat
from django.utils.safestring import mark_safe
Expand All @@ -28,8 +28,6 @@
WorkflowMetadata,
)
from bots.tasks import create_personal_channels_for_all_members
from daras_ai.image_input import truncate_text_words
from daras_ai_v2.base import BasePage
from gooeysite.custom_actions import export_to_excel, export_to_csv
from gooeysite.custom_filters import (
related_json_field_summary,
Expand Down Expand Up @@ -266,10 +264,14 @@ class SavedRunAdmin(admin.ModelAdmin):
"view_parent_published_run",
"run_time",
"price",
"is_api_call",
"created_at",
"updated_at",
]
list_filter = ["workflow"]
list_filter = [
"workflow",
"is_api_call",
]
search_fields = ["workflow", "example_id", "run_id", "uid"]
autocomplete_fields = ["parent_version"]

Expand All @@ -278,10 +280,12 @@ class SavedRunAdmin(admin.ModelAdmin):
"parent",
"view_bots",
"price",
"view_usage_cost",
"transaction",
"created_at",
"updated_at",
"run_time",
"is_api_call",
]

actions = [export_to_csv, export_to_excel]
Expand Down Expand Up @@ -313,6 +317,15 @@ def view_parent_published_run(self, saved_run: SavedRun):
pr = saved_run.parent_published_run()
return pr and change_obj_url(pr)

@admin.display(description="Usage Costs")
def view_usage_cost(self, saved_run: SavedRun):
total_cost = saved_run.usage_costs.aggregate(total_cost=Sum("dollar_amount"))[
"total_cost"
]
return list_related_html_url(
saved_run.usage_costs, extra_label=f"${total_cost.normalize()}"
)


@admin.register(PublishedRunVersion)
class PublishedRunVersionAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -492,6 +505,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 @@ -551,6 +565,7 @@ def get_fieldsets(self, request, msg: Message = None):
"Analysis",
{
"fields": [
"response_time",
"analysis_result",
"analysis_run",
"question_answered",
Expand Down
3 changes: 3 additions & 0 deletions bots/admin_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def list_related_html_url(
query_param: str = None,
instance_id: int = None,
show_add: bool = True,
extra_label: str = None,
) -> typing.Optional[str]:
num = manager.all().count()

Expand All @@ -60,6 +61,8 @@ def list_related_html_url(
).url

label = f"{num} {meta.verbose_name if num == 1 else meta.verbose_name_plural}"
if extra_label:
label = f"{label} ({extra_label})"

if show_add:
add_related_url = furl(
Expand Down
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)'),
),
]
21 changes: 21 additions & 0 deletions bots/migrations/0058_alter_savedrun_unique_together_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.7 on 2024-02-06 18:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bots', '0057_message_response_time_and_more'),
]

operations = [
migrations.AlterUniqueTogether(
name='savedrun',
unique_together={('run_id', 'uid'), ('workflow', 'example_id')},
),
migrations.AddIndex(
model_name='savedrun',
index=models.Index(fields=['run_id', 'uid'], name='bots_savedr_run_id_7b0b34_idx'),
),
]
18 changes: 18 additions & 0 deletions bots/migrations/0059_savedrun_is_api_call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-02-12 07:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bots', '0058_alter_savedrun_unique_together_and_more'),
]

operations = [
migrations.AddField(
model_name='savedrun',
name='is_api_call',
field=models.BooleanField(default=False),
),
]
97 changes: 79 additions & 18 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,15 @@ class SavedRun(models.Model):
page_title = models.TextField(default="", blank=True, help_text="(Deprecated)")
page_notes = models.TextField(default="", blank=True, help_text="(Deprecated)")

is_api_call = models.BooleanField(default=False)

objects = SavedRunQuerySet.as_manager()

class Meta:
ordering = ["-updated_at"]
unique_together = [
["workflow", "example_id"],
["workflow", "run_id", "uid"],
["run_id", "uid"],
]
constraints = [
models.CheckConstraint(
Expand All @@ -273,6 +275,7 @@ class Meta:
models.Index(fields=["-created_at"]),
models.Index(fields=["-updated_at"]),
models.Index(fields=["workflow"]),
models.Index(fields=["run_id", "uid"]),
models.Index(fields=["workflow", "run_id", "uid"]),
models.Index(fields=["workflow", "example_id", "run_id", "uid"]),
models.Index(fields=["workflow", "example_id", "hidden"]),
Expand Down Expand Up @@ -699,8 +702,8 @@ def to_df_format(
.replace(tzinfo=None)
)
row |= {
"Last Sent": last_time.strftime("%b %d, %Y %I:%M %p"),
"First Sent": first_time.strftime("%b %d, %Y %I:%M %p"),
"Last Sent": last_time.strftime(settings.SHORT_DATETIME_FORMAT),
"First Sent": first_time.strftime(settings.SHORT_DATETIME_FORMAT),
"A7": not convo.d7(),
"A30": not convo.d30(),
"R1": last_time - first_time < datetime.timedelta(days=1),
Expand All @@ -717,7 +720,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 @@ -906,36 +928,56 @@ def to_df_format(
"Message (EN)": message.content,
"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
.strftime(settings.SHORT_DATETIME_FORMAT),
"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"),
.strftime(settings.SHORT_DATETIME_FORMAT),
"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 @@ -1017,6 +1059,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 @@ -1107,20 +1155,33 @@ def to_df_format(
"Question Sent": feedback.message.get_previous_by_created_at()
.created_at.astimezone(tz)
.replace(tzinfo=None)
.strftime("%b %d, %Y %I:%M %p"),
.strftime(settings.SHORT_DATETIME_FORMAT),
"Answer (EN)": feedback.message.content,
"Answer Sent": feedback.message.created_at.astimezone(tz)
.replace(tzinfo=None)
.strftime("%b %d, %Y %I:%M %p"),
.strftime(settings.SHORT_DATETIME_FORMAT),
"Rating": Feedback.Rating(feedback.rating).label,
"Feedback (EN)": feedback.text_english,
"Feedback Sent": feedback.created_at.astimezone(tz)
.replace(tzinfo=None)
.strftime("%b %d, %Y %I:%M %p"),
.strftime(settings.SHORT_DATETIME_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
Loading

0 comments on commit 09d02c6

Please sign in to comment.