11"""AI app chunk model."""
22
3- from django .contrib .contenttypes .fields import GenericForeignKey
4- from django .contrib .contenttypes .models import ContentType
53from django .db import models
64from langchain .text_splitter import RecursiveCharacterTextSplitter
75from pgvector .django import VectorField
86
7+ from apps .ai .models .context import Context
98from apps .common .models import BulkSaveModel , TimestampedModel
109from apps .common .utils import truncate
1110
@@ -16,25 +15,18 @@ class Chunk(TimestampedModel):
1615 class Meta :
1716 db_table = "ai_chunks"
1817 verbose_name = "Chunk"
19- unique_together = ("content_type" , "object_id " , "text" )
18+ unique_together = ("context " , "text" )
2019
21- content_object = GenericForeignKey ("content_type" , "object_id" )
22- content_type = models .ForeignKey (ContentType , on_delete = models .CASCADE , blank = True , null = True )
20+ context = models .ForeignKey (
21+ Context , on_delete = models .CASCADE , related_name = "chunks" , null = True , blank = True
22+ )
2323 embedding = VectorField (verbose_name = "Embedding" , dimensions = 1536 )
24- object_id = models .PositiveIntegerField (default = 0 )
2524 text = models .TextField (verbose_name = "Text" )
2625
2726 def __str__ (self ):
2827 """Human readable representation."""
29- content_name = (
30- getattr (self .content_object , "name" , None )
31- or getattr (self .content_object , "key" , None )
32- or str (self .content_object )
33- )
34- return (
35- f"Chunk { self .id } for { self .content_type .model } { content_name } : "
36- f"{ truncate (self .text , 50 )} "
37- )
28+ context_str = str (self .context ) if self .context else "No Context"
29+ return f"Chunk { self .id } for { context_str } : { truncate (self .text , 50 )} "
3830
3931 @staticmethod
4032 def bulk_save (chunks , fields = None ):
@@ -54,7 +46,7 @@ def split_text(text: str) -> list[str]:
5446 @staticmethod
5547 def update_data (
5648 text : str ,
57- content_object ,
49+ context : Context ,
5850 embedding ,
5951 * ,
6052 save : bool = True ,
@@ -63,24 +55,18 @@ def update_data(
6355
6456 Args:
6557 text (str): The text content of the chunk.
66- content_object : The object this chunk belongs to (Message, Chapter, etc.) .
58+ context (Context) : The context this chunk belongs to.
6759 embedding (list): The embedding vector for the chunk.
6860 save (bool): Whether to save the chunk to the database.
6961
7062 Returns:
7163 Chunk: The updated chunk instance or None if it already exists.
7264
7365 """
74- content_type = ContentType .objects .get_for_model (content_object )
75-
76- if Chunk .objects .filter (
77- content_type = content_type , object_id = content_object .id , text = text
78- ).exists ():
66+ if Chunk .objects .filter (context = context , text = text ).exists ():
7967 return None
8068
81- chunk = Chunk (
82- content_type = content_type , object_id = content_object .id , text = text , embedding = embedding
83- )
69+ chunk = Chunk (context = context , text = text , embedding = embedding )
8470
8571 if save :
8672 chunk .save ()
0 commit comments