Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
SECRET_KEY: ${{ secrets.SECRET_KEY }}
run: |
poetry run ./manage.py migrate
PLAYWRIGHT_BROWSERS_PATH=.venv scripts/run_integration_tests.sh
PLAYWRIGHT_BROWSERS_PATH=.venv scripts/tests/run_integration_tests.sh
- name: Create trace.zip
if: always()
run: |
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ RUN export NODE_VERSION=$(cat /tmp/.nvmrc) && cd /tmp/ && curl -fsSLO --compress
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs


# This is done to copy only the source code from HEAD into the image
# This is done to copy only the source code from HEAD into the image
RUN --mount=type=bind,source=.git/,target=/tmp/app/.git/ \
git clone /tmp/app/.git/ /app/

Expand All @@ -69,5 +69,5 @@ EXPOSE 3000
ARG ENTRY_SCRIPT_ARG="scripts/prod/startapp.sh"
ENV ENTRY_SCRIPT=${ENTRY_SCRIPT_ARG}

CMD ["sh","-c","${ENTRY_SCRIPT}"]
CMD ["sh", "-c", "${ENTRY_SCRIPT}"]

Empty file added fab_credits/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions fab_credits/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib import admin

from . import models


@admin.register(models.UserUsage)
class UserUsageAdmin(admin.ModelAdmin):
autocomplete_fields = ("user",)
search_fields = ["user__username", "user__email"]
readonly_fields = ["input_tokens_used", "output_tokens_used"]
6 changes: 6 additions & 0 deletions fab_credits/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class FabCreditsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "fab_credits"
70 changes: 70 additions & 0 deletions fab_credits/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Generated by Django 4.2.11 on 2024-07-18 10:43

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="UserUsage",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"input_tokens_used",
models.IntegerField(
default=0,
help_text="Keeps track of input token used. Filled in automatically",
),
),
(
"output_tokens_used",
models.IntegerField(
default=0,
help_text="Keeps track of output token used. Filled in automatically",
),
),
(
"total_allowed_tokens",
models.IntegerField(
default=0,
help_text="Used by admins to set the total number of input&output tokens the user can use.",
),
),
(
"platform",
models.CharField(
choices=[("OPENAI", "Openai"), ("ANTHROPIC", "Anthropic")],
default="OPENAI",
max_length=50,
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"unique_together": {("user", "platform")},
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 4.2.11 on 2024-07-18 12:44

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("fab_credits", "0001_initial"),
]

operations = [
migrations.AlterUniqueTogether(
name="userusage",
unique_together=set(),
),
migrations.AddField(
model_name="userusage",
name="model_name",
field=models.CharField(
choices=[
("gpt-4o", "gpt-4o"),
("gpt-4o-2024-05-13", "gpt-4o-2024-05-13"),
("gpt-3.5-turbo-0125", "gpt-3.5-turbo-0125"),
("gpt-3.5-turbo-instruct", "gpt-3.5-turbo-instruct"),
("claude-3-5-sonnet-20240620", "claude-3-5-sonnet-20240620"),
("claude-3-opus-20240229", "claude-3-opus-20240229"),
("claude-3-sonnet-20240229", "claude-3-sonnet-20240229"),
("claude-3-haiku-20240307", "claude-3-haiku-20240307"),
],
default="gpt-4o",
max_length=50,
),
),
migrations.AlterUniqueTogether(
name="userusage",
unique_together={("user", "platform", "model_name")},
),
]
Empty file.
45 changes: 45 additions & 0 deletions fab_credits/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.db import models

from users.models import User

available_models = [
("gpt-4o", "gpt-4o"),
("gpt-4o-2024-05-13", "gpt-4o-2024-05-13"),
("gpt-3.5-turbo-0125", "gpt-3.5-turbo-0125"),
("gpt-3.5-turbo-instruct", "gpt-3.5-turbo-instruct"),
("claude-3-5-sonnet-20240620", "claude-3-5-sonnet-20240620"),
("claude-3-opus-20240229", "claude-3-opus-20240229"),
("claude-3-sonnet-20240229", "claude-3-sonnet-20240229"),
("claude-3-haiku-20240307", "claude-3-haiku-20240307"),
]


# Create your models here.
class UserUsage(models.Model):
class UsagePlatform(models.TextChoices):
OpenAI = "OPENAI"
Anthropic = "ANTHROPIC"

user = models.ForeignKey(User, on_delete=models.CASCADE)
input_tokens_used = models.IntegerField(
default=0, help_text="Keeps track of input token used. Filled in automatically"
)
output_tokens_used = models.IntegerField(
default=0, help_text="Keeps track of output token used. Filled in automatically"
)
total_allowed_tokens = models.IntegerField(
default=0,
help_text="Used by admins to set the total number of input&output tokens the user can use.",
)
platform = models.CharField(
max_length=50, choices=UsagePlatform.choices, default=UsagePlatform.OpenAI
)
model_name = models.CharField(
max_length=50, default="gpt-4o", choices=available_models
)

class Meta:
unique_together = ("user", "platform", "model_name")

def __str__(self):
return f"{self.user.username}({self.user.email}) - {self.platform}/{self.model_name}"
1 change: 1 addition & 0 deletions fab_credits/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
16 changes: 16 additions & 0 deletions fab_credits/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.urls import path

from . import views

urlpatterns = [
path(
"anthropic/v1/messages",
views.anthropic_v1_messages,
name="anthropic_v1_messages",
),
path(
"openai/v1/chat/completions",
views.openai_v1_chat_completions,
name="anthropic_v1_messages",
),
]
Loading