From 680ecd7106bc63cc93b762af9bceee2ded47d930 Mon Sep 17 00:00:00 2001 From: Chris Smit Date: Wed, 18 Dec 2024 14:38:52 +0200 Subject: [PATCH 1/7] Remove tag title. We have a tooltip --- templates/annotations/tag_ui.html | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/annotations/tag_ui.html b/templates/annotations/tag_ui.html index 7165c988a..d3109f49a 100644 --- a/templates/annotations/tag_ui.html +++ b/templates/annotations/tag_ui.html @@ -15,7 +15,6 @@ badge-warning {% endif %} tooltip" - title="{% if tag.is_system_tag %}System Tag{% else %}User Tag{% endif %}: {{ tag.name }}" data-tip="Added by {% if tag.added_by == request.user.email %}me{% else %}{{ tag.added_by }}{% endif %}" >{{ tag.name }} {% endfor %} From b213eeafd2403208c8ff83cafab309a4670e4e6e Mon Sep 17 00:00:00 2001 From: Chris Smit Date: Wed, 18 Dec 2024 14:40:06 +0200 Subject: [PATCH 2/7] =?UTF-8?q?View=20and=20template=20to=20add=20message?= =?UTF-8?q?=20tags=20for=20=F0=9F=91=8D=20and=20=F0=9F=91=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/experiments/urls.py | 5 +++++ apps/experiments/views/__init__.py | 1 + apps/experiments/views/chat.py | 22 +++++++++++++++++++ templates/experiments/chat/ai_message.html | 1 + .../chat/components/message_rating.html | 20 +++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 apps/experiments/views/chat.py create mode 100644 templates/experiments/chat/components/message_rating.html diff --git a/apps/experiments/urls.py b/apps/experiments/urls.py index 60d3e1207..bf6bb4471 100644 --- a/apps/experiments/urls.py +++ b/apps/experiments/urls.py @@ -173,6 +173,11 @@ views.verify_public_chat_token, name="verify_public_chat_token", ), + path( + "messages//rate//", + views.rate_message, + name="rate_message", + ), ] urlpatterns.extend(make_crud_urls(views, "SafetyLayer", "safety")) diff --git a/apps/experiments/views/__init__.py b/apps/experiments/views/__init__.py index 089bd1f64..057340dc9 100644 --- a/apps/experiments/views/__init__.py +++ b/apps/experiments/views/__init__.py @@ -1,5 +1,6 @@ from apps.annotations.views import CreateTag, DeleteTag, EditTag, TagHome, TagTableView # noqa: F401 +from .chat import rate_message # noqa: F401 from .consent import ( # noqa: F401 ConsentFormHome, ConsentFormTableView, diff --git a/apps/experiments/views/chat.py b/apps/experiments/views/chat.py new file mode 100644 index 000000000..f7e8c7b47 --- /dev/null +++ b/apps/experiments/views/chat.py @@ -0,0 +1,22 @@ +from django.http import Http404 +from django.shortcuts import get_object_or_404, render + +from apps.chat.models import ChatMessage, ChatMessageType + + +def rate_message(request, team_slug: str, message_id: int, rating: str): + if rating not in ["👍", "👎"]: + raise Http404() + + message = get_object_or_404(ChatMessage, id=message_id, message_type=ChatMessageType.AI) + # TODO + # message.add_rating(rating) + + return render( + request, + template_name="experiments/chat/components/message_rating.html", + context={ + "team_slug": team_slug, + "message": message, + }, + ) diff --git a/templates/experiments/chat/ai_message.html b/templates/experiments/chat/ai_message.html index 7a758e16e..588b151a7 100644 --- a/templates/experiments/chat/ai_message.html +++ b/templates/experiments/chat/ai_message.html @@ -24,5 +24,6 @@

{{ message.content|render_markdown }}

+ {% include 'experiments/chat/components/message_rating.html' with team_slug=experiment.team.slug %} diff --git a/templates/experiments/chat/components/message_rating.html b/templates/experiments/chat/components/message_rating.html new file mode 100644 index 000000000..f447e318e --- /dev/null +++ b/templates/experiments/chat/components/message_rating.html @@ -0,0 +1,20 @@ +
+ {% if message.rating %} +
{{ message.rating }}
+ {% else %} + + + {% endif %} +
\ No newline at end of file From 85041df4dcf5c2b833fcbb0e3ae05ef18cedc54b Mon Sep 17 00:00:00 2001 From: Chris Smit Date: Wed, 18 Dec 2024 14:41:15 +0200 Subject: [PATCH 3/7] Update models Add RESPONSE_RATING tag category and implement add_rating and rating methods --- apps/annotations/models.py | 30 ++++++++++++++++++++---------- apps/chat/models.py | 13 +++++++++++++ apps/experiments/views/chat.py | 3 +-- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/apps/annotations/models.py b/apps/annotations/models.py index 3ca11a6fc..33f38acd2 100644 --- a/apps/annotations/models.py +++ b/apps/annotations/models.py @@ -19,6 +19,7 @@ class TagCategories(models.TextChoices): BOT_RESPONSE = "bot_response", _("Bot Response") SAFETY_LAYER_RESPONSE = "safety_layer_response", _("Safety Layer Response") EXPERIMENT_VERSION = "experiment_version", _("Experiment Version") + RESPONSE_RATING = "response_rating", _("Response Rating") @audit_fields( @@ -111,16 +112,25 @@ def tags_json(self): tagged_items = CustomTaggedItem.objects.filter( content_type__model=self._meta.model_name, content_type__app_label=self._meta.app_label, object_id=self.id ).prefetch_related("tag", "user") - return [ - { - "name": tagged_item.tag.name, - "id": tagged_item.tag.id, - "is_system_tag": tagged_item.tag.is_system_tag, - "category": tagged_item.tag.category, - "added_by": tagged_item.user.email if tagged_item.user else "System", - } - for tagged_item in tagged_items - ] + tags = [] + for tagged_item in tagged_items: + if tagged_item.tag.is_system_tag: + added_by = "System" + elif tagged_item.user and tagged_item.user.email: + added_by = tagged_item.user.email + else: + added_by = "Participant" + + tags.append( + { + "name": tagged_item.tag.name, + "id": tagged_item.tag.id, + "is_system_tag": tagged_item.tag.is_system_tag, + "category": tagged_item.tag.category, + "added_by": added_by, + } + ) + return tags class UserComment(BaseTeamModel): diff --git a/apps/chat/models.py b/apps/chat/models.py index fa45e4cf4..44b6bba82 100644 --- a/apps/chat/models.py +++ b/apps/chat/models.py @@ -209,6 +209,19 @@ def add_version_tag(self, version_number: int, is_a_version: bool): tag = f"{tag}-unreleased" self.add_system_tag(tag=tag, tag_category=TagCategories.EXPERIMENT_VERSION) + def add_rating(self, tag: str): + tag, _ = Tag.objects.get_or_create( + name=tag, + team=self.chat.team, + is_system_tag=False, + category=TagCategories.RESPONSE_RATING, + ) + self.add_tag(tag, team=self.chat.team, added_by=None) + + def rating(self) -> str | None: + if rating := self.tags.filter(category=TagCategories.RESPONSE_RATING).first(): + return rating.name + def get_processor_bot_tag_name(self) -> str | None: """Returns the tag of the bot that generated this message""" if self.message_type != ChatMessageType.AI: diff --git a/apps/experiments/views/chat.py b/apps/experiments/views/chat.py index f7e8c7b47..b80aab6ea 100644 --- a/apps/experiments/views/chat.py +++ b/apps/experiments/views/chat.py @@ -9,8 +9,7 @@ def rate_message(request, team_slug: str, message_id: int, rating: str): raise Http404() message = get_object_or_404(ChatMessage, id=message_id, message_type=ChatMessageType.AI) - # TODO - # message.add_rating(rating) + message.add_rating(rating) return render( request, From 448f47832c40b87c70ef1ed9ae5f5f85185bfc9d Mon Sep 17 00:00:00 2001 From: Chris Smit Date: Wed, 18 Dec 2024 14:46:35 +0200 Subject: [PATCH 4/7] =?UTF-8?q?Disallow=20editing=20=F0=9F=91=8D=20and=20?= =?UTF-8?q?=F0=9F=91=8E=20tags=20from=20transcript=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/annotations/views/tag_views.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/annotations/views/tag_views.py b/apps/annotations/views/tag_views.py index a6e757f91..f00789dc1 100644 --- a/apps/annotations/views/tag_views.py +++ b/apps/annotations/views/tag_views.py @@ -11,7 +11,7 @@ from django_tables2 import SingleTableView from apps.annotations.forms import TagForm -from apps.annotations.models import Tag +from apps.annotations.models import Tag, TagCategories from apps.annotations.tables import TagTable from apps.teams.mixins import LoginAndTeamRequiredMixin @@ -126,7 +126,12 @@ def get(self, request, team_slug: str): "team_slug": team_slug, "object": obj, "edit_mode": request.GET.get("edit"), - "available_tags": [t.name for t in Tag.objects.filter(team__slug=team_slug, is_system_tag=False).all()], + "available_tags": [ + t.name + for t in Tag.objects.filter(team__slug=team_slug, is_system_tag=False) + .exclude(category=TagCategories.RESPONSE_RATING) + .all() + ], }, ) From 08884deedbb27bc422d660f3ea3ca02eb0138091 Mon Sep 17 00:00:00 2001 From: Chris Smit Date: Wed, 18 Dec 2024 14:56:24 +0200 Subject: [PATCH 5/7] Small query improvement --- apps/chat/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/chat/models.py b/apps/chat/models.py index 44b6bba82..baa6a1809 100644 --- a/apps/chat/models.py +++ b/apps/chat/models.py @@ -219,8 +219,8 @@ def add_rating(self, tag: str): self.add_tag(tag, team=self.chat.team, added_by=None) def rating(self) -> str | None: - if rating := self.tags.filter(category=TagCategories.RESPONSE_RATING).first(): - return rating.name + if rating := self.tags.filter(category=TagCategories.RESPONSE_RATING).values_list("name", flat=True).first(): + return rating def get_processor_bot_tag_name(self) -> str | None: """Returns the tag of the bot that generated this message""" From aa7d38268ed7ef054e4caf076b5205f6fb42d884 Mon Sep 17 00:00:00 2001 From: Chris Smit Date: Wed, 18 Dec 2024 15:05:41 +0200 Subject: [PATCH 6/7] Add migration for new category --- .../migrations/0007_alter_tag_category.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 apps/annotations/migrations/0007_alter_tag_category.py diff --git a/apps/annotations/migrations/0007_alter_tag_category.py b/apps/annotations/migrations/0007_alter_tag_category.py new file mode 100644 index 000000000..6e9b87215 --- /dev/null +++ b/apps/annotations/migrations/0007_alter_tag_category.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.2 on 2024-12-18 13:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('annotations', '0006_alter_tag_category'), + ] + + operations = [ + migrations.AlterField( + model_name='tag', + name='category', + field=models.CharField(blank=True, choices=[('bot_response', 'Bot Response'), ('safety_layer_response', 'Safety Layer Response'), ('experiment_version', 'Experiment Version'), ('response_rating', 'Response Rating')], default=''), + ), + ] From 85d5c5d498a3a2bf97bae7c489300d02d024b17b Mon Sep 17 00:00:00 2001 From: Chris Smit Date: Thu, 19 Dec 2024 09:18:05 +0200 Subject: [PATCH 7/7] Add button title to show what the buttons mean --- templates/experiments/chat/components/message_rating.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/templates/experiments/chat/components/message_rating.html b/templates/experiments/chat/components/message_rating.html index f447e318e..97030d2d5 100644 --- a/templates/experiments/chat/components/message_rating.html +++ b/templates/experiments/chat/components/message_rating.html @@ -6,14 +6,16 @@ hx-post="{% url 'experiments:rate_message' team_slug=team_slug message_id=message.id rating='👍' %}" hx-swap="outerHTML" hx-target="#rating-{{message.id}}" - class="text-sm p-1 rounded-md hover:bg-gray-50"> + class="text-sm p-1 rounded-md hover:bg-gray-50" + title="Good response"> 👍 {% endif %}