Skip to content

django_ai_assistant.models

Thread

Bases: Model

Thread model. A thread is a collection of messages between a user and the AI assistant. Also called conversation or session.

Source code in django_ai_assistant/models.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Thread(models.Model):
    """Thread model. A thread is a collection of messages between a user and the AI assistant.
    Also called conversation or session."""

    messages: Manager["Message"]
    name = models.CharField(max_length=255, blank=True)
    """Name of the thread. Can be blank."""
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        related_name="ai_assistant_threads",
        null=True,
    )
    """User who created the thread. Can be null. Set to null/None when user is deleted."""
    created_at = models.DateTimeField(auto_now_add=True)
    """Date and time when the thread was created.
    Automatically set when the thread is created."""
    updated_at = models.DateTimeField(auto_now=True)
    """Date and time when the thread was last updated.
    Automatically set when the thread is updated."""

    class Meta:
        verbose_name = "Thread"
        verbose_name_plural = "Threads"
        ordering = ("-created_at",)
        indexes = (Index(F("created_at").desc(), name="thread_created_at_desc"),)

    def __str__(self) -> str:
        """Return the name of the thread as the string representation of the thread."""
        return self.name

    def __repr__(self) -> str:
        """Return the string representation of the thread like '<Thread name>'"""
        return f"<Thread {self.name}>"

name = models.CharField(max_length=255, blank=True) class-attribute instance-attribute

Name of the thread. Can be blank.

created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, related_name='ai_assistant_threads', null=True) class-attribute instance-attribute

User who created the thread. Can be null. Set to null/None when user is deleted.

created_at = models.DateTimeField(auto_now_add=True) class-attribute instance-attribute

Date and time when the thread was created. Automatically set when the thread is created.

updated_at = models.DateTimeField(auto_now=True) class-attribute instance-attribute

Date and time when the thread was last updated. Automatically set when the thread is updated.

__str__()

Return the name of the thread as the string representation of the thread.

Source code in django_ai_assistant/models.py
36
37
38
def __str__(self) -> str:
    """Return the name of the thread as the string representation of the thread."""
    return self.name

__repr__()

Return the string representation of the thread like ''

Source code in django_ai_assistant/models.py
40
41
42
def __repr__(self) -> str:
    """Return the string representation of the thread like '<Thread name>'"""
    return f"<Thread {self.name}>"

Message

Bases: Model

Message model. A message is a text that is part of a thread. A message can be sent by a user or the AI assistant.

The message data is stored as a JSON field called message.

Source code in django_ai_assistant/models.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Message(models.Model):
    """Message model. A message is a text that is part of a thread.
    A message can be sent by a user or the AI assistant.\n
    The message data is stored as a JSON field called `message`."""

    thread = models.ForeignKey(Thread, on_delete=models.CASCADE, related_name="messages")
    """Thread to which the message belongs."""
    thread_id: Any  # noqa: A003
    message = models.JSONField()
    """Message content. This is a serialized Langchain `BaseMessage` that was serialized
    with `message_to_dict` and can be deserialized with `messages_from_dict`."""
    created_at = models.DateTimeField(auto_now_add=True)
    """Date and time when the message was created.
    Automatically set when the message is created."""
    # TODO: add created_by field

    class Meta:
        verbose_name = "Message"
        verbose_name_plural = "Messages"
        ordering = ("created_at",)
        indexes = (Index(F("created_at"), name="message_created_at"),)

    def __str__(self) -> str:
        """Return internal message data from `message` attribute
        as the string representation of the message."""
        return json.dumps(self.message)

    def __repr__(self) -> str:
        """Return the string representation of the message like '<Message id at thread_id>'"""
        return f"<Message {self.id} at {self.thread_id}>"

thread = models.ForeignKey(Thread, on_delete=models.CASCADE, related_name='messages') class-attribute instance-attribute

Thread to which the message belongs.

message = models.JSONField() class-attribute instance-attribute

Message content. This is a serialized Langchain BaseMessage that was serialized with message_to_dict and can be deserialized with messages_from_dict.

created_at = models.DateTimeField(auto_now_add=True) class-attribute instance-attribute

Date and time when the message was created. Automatically set when the message is created.

__str__()

Return internal message data from message attribute as the string representation of the message.

Source code in django_ai_assistant/models.py
67
68
69
70
def __str__(self) -> str:
    """Return internal message data from `message` attribute
    as the string representation of the message."""
    return json.dumps(self.message)

__repr__()

Return the string representation of the message like ''

Source code in django_ai_assistant/models.py
72
73
74
def __repr__(self) -> str:
    """Return the string representation of the message like '<Message id at thread_id>'"""
    return f"<Message {self.id} at {self.thread_id}>"