Skip to content

models

django_spire.ai.chat.models

Chat

Bases: HistoryModelMixin

user = models.ForeignKey(User, blank=True, null=True, on_delete=(models.SET_NULL), related_name='chats', related_query_name='chat') class-attribute instance-attribute

name = models.CharField(max_length=128) class-attribute instance-attribute

last_message_datetime = models.DateTimeField(default=now, editable=False) class-attribute instance-attribute

has_unread_messages = models.BooleanField(default=False) class-attribute instance-attribute

objects = ChatQuerySet.as_manager() class-attribute instance-attribute

is_empty property

name_shortened property

Meta

db_table = 'django_spire_ai_chat' class-attribute instance-attribute
verbose_name = 'Chat' class-attribute instance-attribute
verbose_name_plural = 'Chats' class-attribute instance-attribute
ordering = ('-last_message_datetime', 'name') class-attribute instance-attribute

__str__

Source code in django_spire/ai/chat/models.py
def __str__(self):
    if len(self.name) < 48:
        return self.name

    return self.name[:48] + '...'

add_message_response

Source code in django_spire/ai/chat/models.py
def add_message_response(self, message_response: MessageResponse) -> None:
    self.messages.create(
        response_type=message_response.type.value,
        sender=message_response.sender,
        _intel_data=message_response.message_intel.model_dump(),
        _intel_class_name=get_class_name_from_class(message_response.message_intel.__class__),
        is_processed=True,
        is_viewed=True
    )
    self.last_message_datetime = now()
    self.save()

generate_message_history

Source code in django_spire/ai/chat/models.py
def generate_message_history(
        self,
        message_count: int = 20,
        exclude_last_message: bool = True
) -> MessageHistory:
    message_history = MessageHistory()

    messages = self.messages.newest_by_count(message_count)

    if exclude_last_message:
        messages = messages[1:]

    messages = list(reversed(messages))

    for message in messages:
        message_history.add_message(
            role=message.role,
            content=message.intel.content_to_str()
        )

    return message_history

ChatMessage

Bases: HistoryModelMixin

chat = models.ForeignKey(Chat, on_delete=(models.CASCADE), related_name='messages', related_query_name='message') class-attribute instance-attribute

response_type = models.CharField(max_length=32, choices=MessageResponseType) class-attribute instance-attribute

sender = models.CharField(max_length=128) class-attribute instance-attribute

is_processed = models.BooleanField(default=False) class-attribute instance-attribute

is_viewed = models.BooleanField(default=False) class-attribute instance-attribute

objects = ChatMessageQuerySet.as_manager() class-attribute instance-attribute

intel property writable

role property

Meta

db_table = 'django_spire_ai_chat_message' class-attribute instance-attribute
verbose_name = 'Chat Message' class-attribute instance-attribute
verbose_name_plural = 'Chat Messages' class-attribute instance-attribute

__str__

Source code in django_spire/ai/chat/models.py
def __str__(self):
    content = self.intel.content_to_str()

    if len(content) < 64:
        return content

    return content[:64] + '...'

to_message_response

Source code in django_spire/ai/chat/models.py
def to_message_response(self) -> MessageResponse:
    return MessageResponse(
        type=MessageResponseType(self.response_type),
        sender=self.sender,
        message_intel=self.intel
    )