daren/apps/rlhf/models.py
2025-06-11 16:39:39 +08:00

218 lines
8.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.db import models
import uuid
from django.utils import timezone
from apps.user.models import User
from apps.chat.models import ChatHistory, NegotiationChat
from apps.daren_detail.models import CreatorProfile
from apps.brands.models import Product
# 使用NegotiationChat替代Conversation
# class Conversation(models.Model):
# id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
# user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='conversations')
# is_submitted = models.BooleanField(default=False)
# created_at = models.DateTimeField(default=timezone.now)
#
# class Meta:
# verbose_name = '对话'
# verbose_name_plural = '对话'
#
# def __str__(self):
# return f"Conversation {self.id[:8]}"
# 为NegotiationChat添加RLHF所需字段的代理模型
class RLHFConversation(models.Model):
"""对NegotiationChat的扩展添加RLHF所需字段"""
negotiation_chat = models.OneToOneField(NegotiationChat, on_delete=models.CASCADE, primary_key=True, related_name='rlhf_extension')
is_submitted = models.BooleanField(default=False)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='rlhf_conversations')
class Meta:
verbose_name = 'RLHF对话'
verbose_name_plural = 'RLHF对话'
def __str__(self):
return f"RLHF Conversation {self.negotiation_chat.conversation_id[:8]}"
@property
def id(self):
return self.negotiation_chat.conversation_id
@property
def created_at(self):
return self.negotiation_chat.created_at
# 使用ChatHistory替代Message
# class Message(models.Model):
# ROLE_CHOICES = (
# ('user', '用户'),
# ('assistant', '助手'),
# ('system', '系统'),
# )
#
# id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
# conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE, related_name='messages')
# role = models.CharField(max_length=20, choices=ROLE_CHOICES)
# content = models.TextField()
# timestamp = models.DateTimeField(default=timezone.now)
#
# class Meta:
#
# verbose_name = '消息'
# verbose_name_plural = '消息'
# ordering = ['timestamp']
#
# def __str__(self):
# return f"{self.role}: {self.content[:50]}..."
class Feedback(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
message = models.ForeignKey(ChatHistory, on_delete=models.CASCADE, related_name='rlhf_feedback')
conversation_id = models.CharField(max_length=100) # 存储NegotiationChat的conversation_id
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='feedback')
feedback_value = models.IntegerField()
timestamp = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name = '反馈'
verbose_name_plural = '反馈'
def __str__(self):
return f"Feedback on {self.message.id}"
class FeedbackTag(models.Model):
TAG_TYPE_CHOICES = (
('positive', '正面'),
('negative', '负面'),
)
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
tag_name = models.CharField(max_length=50, unique=True)
tag_type = models.CharField(max_length=20, choices=TAG_TYPE_CHOICES)
description = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name = '反馈标签'
verbose_name_plural = '反馈标签'
def __str__(self):
return f"{self.tag_name} ({self.tag_type})"
class DetailedFeedback(models.Model):
FEEDBACK_TYPE_CHOICES = (
('positive', '正面'),
('negative', '负面'),
('neutral', '中性'),
)
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
message = models.ForeignKey(ChatHistory, on_delete=models.CASCADE, related_name='rlhf_detailed_feedback')
conversation_id = models.CharField(max_length=100) # 存储NegotiationChat的conversation_id
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='detailed_feedback')
feedback_type = models.CharField(max_length=20, choices=FEEDBACK_TYPE_CHOICES)
feedback_tags = models.TextField(blank=True, null=True) # JSON格式存储多个标签
custom_tags = models.TextField(blank=True, null=True)
custom_content = models.TextField(blank=True, null=True)
is_inline = models.BooleanField(default=True)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name = '详细反馈'
verbose_name_plural = '详细反馈'
def __str__(self):
return f"{self.feedback_type} feedback on {self.message.id}"
class ConversationSubmission(models.Model):
STATUS_CHOICES = (
('submitted', '已提交'),
('reviewed', '已审核'),
('accepted', '已接受'),
('rejected', '已拒绝'),
)
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
conversation_id = models.CharField(max_length=100) # 存储NegotiationChat的conversation_id
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='submissions')
title = models.CharField(max_length=255, blank=True, null=True)
description = models.TextField(blank=True, null=True)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='submitted')
quality_score = models.IntegerField(null=True, blank=True)
reviewer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='reviewed_submissions')
reviewer_notes = models.TextField(blank=True, null=True)
submitted_at = models.DateTimeField(default=timezone.now)
reviewed_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name = '对话提交'
verbose_name_plural = '对话提交'
def __str__(self):
return f"Submission for {self.conversation_id[:8]}"
class ConversationEvaluation(models.Model):
LOGICAL_CHOICES = (
('yes', ''),
('no', ''),
('unsure', '不确定'),
)
NEEDS_CHOICES = (
('yes', ''),
('no', ''),
('partially', '部分'),
)
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
conversation_id = models.CharField(max_length=100) # 存储NegotiationChat的conversation_id
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='evaluations')
overall_feeling = models.TextField(blank=True, null=True)
has_logical_issues = models.CharField(max_length=10, choices=LOGICAL_CHOICES)
needs_satisfied = models.CharField(max_length=10, choices=NEEDS_CHOICES)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name = '对话评估'
verbose_name_plural = '对话评估'
unique_together = ('conversation_id', 'user')
def __str__(self):
return f"Evaluation for {self.conversation_id[:8]}"
class SystemConfig(models.Model):
CONFIG_TYPE_CHOICES = (
('string', '字符串'),
('integer', '整数'),
('float', '浮点数'),
('boolean', '布尔值'),
('json', 'JSON'),
)
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False)
config_key = models.CharField(max_length=50, unique=True)
config_value = models.TextField(blank=True, null=True)
config_type = models.CharField(max_length=20, choices=CONFIG_TYPE_CHOICES, default='string')
description = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name = '系统配置'
verbose_name_plural = '系统配置'
def __str__(self):
return self.config_key