Skip to content

Commit

Permalink
added cmd to back populate logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gecBurton committed Oct 17, 2024
1 parent 53f7c05 commit 1d36ae1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
21 changes: 2 additions & 19 deletions django_app/redbox_app/redbox_core/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from asyncio import CancelledError
from collections.abc import Mapping, Sequence
from typing import Any, ClassVar
from uuid import UUID, uuid4
from uuid import UUID

from channels.db import database_sync_to_async
from channels.generic.websocket import AsyncWebsocketConsumer
Expand Down Expand Up @@ -222,25 +222,8 @@ def save_message(
model_name=model,
token_count=token_count,
)
token_count = sum(metadata.output_tokens.values())
else:
token_count = 0

elastic_log_msg = {
"@timestamp": chat_message.created_at.isotime(),
"id": chat_message.id,
"chat_id": chat_message.chat.id,
"user_id": chat_message.chat.user.id,
"text": chat_message.text,
"route": chat_message.route,
"role": "ai",
"token_count": token_count,
"rating": chat_message.rating,
"rating_text": chat_message.rating_text,
"rating_chips": chat_message.rating_chips,
}
elasticsearch_client.create(env.elastic_chat_mesage_index, uuid4(), elastic_log_msg)

chat_message.log()
return chat_message

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.core.management import BaseCommand

from redbox_app.redbox_core.models import ChatMessage


class Command(BaseCommand):
help = """This is a one-off command to back populate elastic logs."""

def handle(self, *args, **kwargs): # noqa:ARG002
for chat_message in ChatMessage.objects.all():
chat_message.log()
19 changes: 18 additions & 1 deletion django_app/redbox_app/redbox_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.contrib.postgres.fields import ArrayField
from django.core import validators
from django.db import models
from django.db.models import Max, Min, Prefetch, UniqueConstraint
from django.db.models import Max, Min, Prefetch, Sum, UniqueConstraint
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django_use_email_as_username.models import BaseUser, BaseUserManager
Expand Down Expand Up @@ -786,6 +786,23 @@ def get_messages_ordered_by_citation_priority(cls, chat_id: uuid.UUID) -> Sequen
)
)

def log(self):
token_count = self.chatmessagetokenuse_set.annotate(token_count=Sum("token_count"))
elastic_log_msg = {
"@timestamp": self.created_at.isotime(),
"id": self.id,
"chat_id": self.chat.id,
"user_id": self.chat.user.id,
"text": self.text,
"route": self.route,
"role": "ai",
"token_count": token_count["token_count"],
"rating": self.rating,
"rating_text": self.rating_text,
"rating_chips": self.rating_chips,
}
es_client.create(env.elastic_chat_mesage_index, uuid.uuid4(), elastic_log_msg)


class ChatMessageTokenUse(UUIDPrimaryKeyBase, TimeStampedModel):
class UseTypeEnum(models.TextChoices):
Expand Down

0 comments on commit 1d36ae1

Please sign in to comment.