Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dara-network/ddgai into copilot_s…
Browse files Browse the repository at this point in the history
…ettings_reordering

# Conflicts:
#	daras_ai_v2/prompt_vars.py
#	recipes/VideoBots.py
  • Loading branch information
devxpy committed Feb 29, 2024
2 parents d9d3de1 + 2cdd158 commit 9e8c7ef
Show file tree
Hide file tree
Showing 113 changed files with 3,584 additions and 1,150 deletions.
10 changes: 10 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 @@ -145,6 +151,9 @@ pg_restore --no-privileges --no-owner -d $PGDATABASE $fname
cid=$(docker ps | grep gooey-api-prod | cut -d " " -f 1 | head -1)
# exec the script to create the fixture
docker exec -it $cid poetry run ./manage.py runscript create_fixture
```

```bash
# copy the fixture outside container
docker cp $cid:/app/fixture.json .
# print the absolute path
Expand Down Expand Up @@ -178,3 +187,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
```

1 change: 1 addition & 0 deletions app_users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AppUserAdmin(admin.ModelAdmin):
"view_transactions",
"open_in_firebase",
"open_in_stripe",
"low_balance_email_sent_at",
]

@admin.display(description="User Runs")
Expand Down
18 changes: 18 additions & 0 deletions app_users/migrations/0012_appuser_low_balance_email_sent_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-02-14 07:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app_users', '0011_appusertransaction_charged_amount_and_more'),
]

operations = [
migrations.AddField(
model_name='appuser',
name='low_balance_email_sent_at',
field=models.DateTimeField(blank=True, null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.7 on 2024-02-28 14:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app_users', '0012_appuser_low_balance_email_sent_at'),
]

operations = [
migrations.AddIndex(
model_name='appusertransaction',
index=models.Index(fields=['user', 'amount', '-created_at'], name='app_users_a_user_id_9b2e8d_idx'),
),
migrations.AddIndex(
model_name='appusertransaction',
index=models.Index(fields=['-created_at'], name='app_users_a_created_3c27fe_idx'),
),
]
12 changes: 11 additions & 1 deletion app_users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class AppUser(models.Model):
stripe_customer_id = models.CharField(max_length=255, default="", blank=True)
is_paying = models.BooleanField("paid", default=False)

low_balance_email_sent_at = models.DateTimeField(null=True, blank=True)

created_at = models.DateTimeField(
"created", editable=False, blank=True, default=timezone.now
)
Expand Down Expand Up @@ -207,7 +209,11 @@ def search_stripe_customer(self) -> stripe.Customer | None:
if not self.uid:
return None
if self.stripe_customer_id:
return stripe.Customer.retrieve(self.stripe_customer_id)
try:
return stripe.Customer.retrieve(self.stripe_customer_id)
except stripe.error.InvalidRequestError as e:
if e.http_status != 404:
raise
try:
customer = stripe.Customer.search(
query=f'metadata["uid"]:"{self.uid}"'
Expand Down Expand Up @@ -263,6 +269,10 @@ class AppUserTransaction(models.Model):

class Meta:
verbose_name = "Transaction"
indexes = [
models.Index(fields=["user", "amount", "-created_at"]),
models.Index(fields=["-created_at"]),
]

def __str__(self):
return f"{self.invoice_id} ({self.amount})"
41 changes: 33 additions & 8 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 @@ -168,6 +166,7 @@ class BotIntegrationAdmin(admin.ModelAdmin):
"Settings",
{
"fields": [
"streaming_enabled",
"show_feedback_buttons",
"analysis_run",
"view_analysis_results",
Expand Down Expand Up @@ -265,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 @@ -277,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 All @@ -289,16 +294,25 @@ class SavedRunAdmin(admin.ModelAdmin):
django.db.models.JSONField: {"widget": JSONEditorWidget},
}

def get_queryset(self, request):
return (
super()
.get_queryset(request)
.prefetch_related(
"parent_version",
"parent_version__published_run",
"parent_version__published_run__saved_run",
)
)

def lookup_allowed(self, key, value):
if key in ["parent_version__published_run__id__exact"]:
return True
return super().lookup_allowed(key, value)

def view_user(self, saved_run: SavedRun):
return change_obj_url(
AppUser.objects.get(uid=saved_run.uid),
label=f"{saved_run.uid}",
)
user = AppUser.objects.get(uid=saved_run.uid)
return change_obj_url(user)

view_user.short_description = "View User"

Expand All @@ -312,6 +326,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 @@ -491,6 +514,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 +574,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
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)'),
),
]
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),
),
]
18 changes: 18 additions & 0 deletions bots/migrations/0060_conversation_reset_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-02-20 16:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bots', '0059_savedrun_is_api_call'),
]

operations = [
migrations.AddField(
model_name='conversation',
name='reset_at',
field=models.DateTimeField(blank=True, default=None, null=True),
),
]
Loading

0 comments on commit 9e8c7ef

Please sign in to comment.