Skip to content

Commit

Permalink
Merge pull request #1004 from dimagi/cs/ui_feedback
Browse files Browse the repository at this point in the history
👍👎 for UI feedback
  • Loading branch information
SmittieC authored Dec 19, 2024
2 parents 4b40a94 + 85d5c5d commit 3794faf
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 13 deletions.
18 changes: 18 additions & 0 deletions apps/annotations/migrations/0007_alter_tag_category.py
Original file line number Diff line number Diff line change
@@ -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=''),
),
]
30 changes: 20 additions & 10 deletions apps/annotations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions apps/annotations/views/tag_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
],
},
)

Expand Down
13 changes: 13 additions & 0 deletions apps/chat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).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"""
if self.message_type != ChatMessageType.AI:
Expand Down
5 changes: 5 additions & 0 deletions apps/experiments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@
views.verify_public_chat_token,
name="verify_public_chat_token",
),
path(
"messages/<int:message_id>/rate/<str:rating>/",
views.rate_message,
name="rate_message",
),
]

urlpatterns.extend(make_crud_urls(views, "SafetyLayer", "safety"))
Expand Down
1 change: 1 addition & 0 deletions apps/experiments/views/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
21 changes: 21 additions & 0 deletions apps/experiments/views/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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)
message.add_rating(rating)

return render(
request,
template_name="experiments/chat/components/message_rating.html",
context={
"team_slug": team_slug,
"message": message,
},
)
1 change: 0 additions & 1 deletion templates/annotations/tag_ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}</div>
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions templates/experiments/chat/ai_message.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<p>{{ message.content|render_markdown }}</p>
</div>
</div>
{% include 'experiments/chat/components/message_rating.html' with team_slug=experiment.team.slug %}
</div>
</div>
22 changes: 22 additions & 0 deletions templates/experiments/chat/components/message_rating.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="flex flex-row gap-1" id="rating-{{message.id}}">
{% if message.rating %}
<div class="text-sm">{{ message.rating }}</div>
{% else %}
<button
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"
title="Good response">
👍
</button>
<button
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"
title="Bad response">
👎
</button>
{% endif %}
</div>

0 comments on commit 3794faf

Please sign in to comment.