RAG
Retrieval-augmented knowledge lives in Django app agency.corpus.
Shape
Tenant → Workspace → Document → (extraction) → Chunk → Chroma collection
Workspaces carry embedding / chunk defaults and deterministic collection names (per tenant + workspace). Agents can query private knowledge without mixing that infra into ThreadRunner itself.
In this reference stack, vectors are stored in ChromaDB. That is a host deployment choice; the package talks to the configured store.
API surface (today)
REST viewsets and actions under /api/ include workspace and document
resources (search / ask / reindex-style actions as implemented on the viewsets).
See API and OpenAPI at /api/.
Related: miner (Scrap / Block) is scrape ingest, not the RAG index —
Miner.
Module reference
Django app agency.corpus: document indexing and retrieval for RAG.
Models, vector store wiring, retrieval/generation helpers, and Celery tasks for indexing live under this package.
Corpus domain models: tenants, workspaces, documents, chunks, extractions.
- agency.corpus.models.slugify_for_collection(value)[source]
Slug simple y estable para nombres de colección. Chroma suele tolerar bastante, pero conviene restringir a [a-z0-9_:-].
- class agency.corpus.models.Tenant(*args, **kwargs)[source]
Bases:
ModelDatabase table:
corpus_tenantOrganización / cliente. Límite duro de aislamiento de datos.
- Parameters:
id (UUIDField) – Primary key: Id
name (CharField) – Name
slug (SlugField) – Slug
is_active (BooleanField) – Is active
created_at (DateTimeField) – Created at
Reverse relationships:
- Parameters:
memberships (Reverse
ForeignKeyfromTenantMembership) – All memberships of this tenant (related name oftenant)workspaces (Reverse
ForeignKeyfromWorkspace) – All workspaces of this tenant (related name oftenant)
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- is_active
Type:
BooleanFieldIs active
A wrapper for a deferred-loading field. When the value is read from this
- created_at
Type:
DateTimeFieldCreated at
A wrapper for a deferred-loading field. When the value is read from this
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- get_next_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=True, **kwargs)
Finds next instance based on
created_at. Seeget_next_by_FOO()for more information.
- get_previous_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=False, **kwargs)
Finds previous instance based on
created_at. Seeget_previous_by_FOO()for more information.
- memberships
Type: Reverse
ForeignKeyfromTenantMembershipAll memberships of this tenant (related name of
tenant)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- objects = <django.db.models.Manager object>
- workspaces
Type: Reverse
ForeignKeyfromWorkspaceAll workspaces of this tenant (related name of
tenant)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- class agency.corpus.models.TenantMembership(*args, **kwargs)[source]
Bases:
ModelDatabase table:
corpus_tenantmembershipLinks an operator to a tenant for corpus / workspace access.
- Parameters:
id (UUIDField) – Primary key: Id
created_at (DateTimeField) – Created at
Relationship fields:
- Parameters:
tenant (
ForeignKeytoTenant) – Tenant (related name:memberships)user (
ForeignKeytoUser) – User (related name:tenant_memberships)
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- tenant
Type:
ForeignKeytoTenantTenant (related name:
memberships)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- user
Type:
ForeignKeytoUserUser (related name:
tenant_memberships)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- created_at
Type:
DateTimeFieldCreated at
A wrapper for a deferred-loading field. When the value is read from this
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- get_next_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=True, **kwargs)
Finds next instance based on
created_at. Seeget_next_by_FOO()for more information.
- get_previous_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=False, **kwargs)
Finds previous instance based on
created_at. Seeget_previous_by_FOO()for more information.
- objects = <django.db.models.Manager object>
- class agency.corpus.models.Workspace(*args, **kwargs)[source]
Bases:
ModelDatabase table:
corpus_workspaceEspacio dentro de un tenant (proyecto/departamento). Normalmente: 1 colección por workspace (recomendado) o por tenant+workspace.
- Parameters:
id (UUIDField) – Primary key: Id
name (CharField) – Name
slug (SlugField) – Slug
description (TextField) – Description
default_embedding_model (CharField) – Default embedding model
default_chunk_size (PositiveIntegerField) – Default chunk size
default_chunk_overlap (PositiveIntegerField) – Default chunk overlap
created_at (DateTimeField) – Created at
Relationship fields:
- Parameters:
tenant (
ForeignKeytoTenant) – Tenant (related name:workspaces)
Reverse relationships:
- Parameters:
ui_panels (Reverse
ForeignKeyfromUiPanel) – All ui panels of this workspace (related name ofworkspace)documents (Reverse
ForeignKeyfromDocument) – All documents of this workspace (related name ofworkspace)display_assignments (Reverse
ForeignKeyfromDisplayAssignment) – All display assignments of this workspace (related name ofworkspace)display_views (Reverse
ForeignKeyfromDisplayView) – All display views of this workspace (related name ofworkspace)displays (Reverse
ForeignKeyfromDisplay) – All displays of this workspace (related name ofworkspace)display_scopes (Reverse
ForeignKeyfromDisplayScope) – All display scopes of this workspace (related name ofworkspace)
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- tenant
Type:
ForeignKeytoTenantTenant (related name:
workspaces)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- description
Type:
TextFieldDescription
A wrapper for a deferred-loading field. When the value is read from this
- default_embedding_model
Type:
CharFieldDefault embedding model
A wrapper for a deferred-loading field. When the value is read from this
- default_chunk_size
Type:
PositiveIntegerFieldDefault chunk size
A wrapper for a deferred-loading field. When the value is read from this
- default_chunk_overlap
Type:
PositiveIntegerFieldDefault chunk overlap
A wrapper for a deferred-loading field. When the value is read from this
- created_at
Type:
DateTimeFieldCreated at
A wrapper for a deferred-loading field. When the value is read from this
- collection_name(env='prod', schema_version=1)[source]
Nombre determinista de colección. - env: dev/prod/staging - schema_version: súbelo si cambias metadatos/estrategia de ids
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- display_assignments
Type: Reverse
ForeignKeyfromDisplayAssignmentAll display assignments of this workspace (related name of
workspace)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- display_scopes
Type: Reverse
ForeignKeyfromDisplayScopeAll display scopes of this workspace (related name of
workspace)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- display_views
Type: Reverse
ForeignKeyfromDisplayViewAll display views of this workspace (related name of
workspace)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- displays
Type: Reverse
ForeignKeyfromDisplayAll displays of this workspace (related name of
workspace)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- documents
Type: Reverse
ForeignKeyfromDocumentAll documents of this workspace (related name of
workspace)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- get_next_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=True, **kwargs)
Finds next instance based on
created_at. Seeget_next_by_FOO()for more information.
- get_previous_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=False, **kwargs)
Finds previous instance based on
created_at. Seeget_previous_by_FOO()for more information.
- objects = <django.db.models.Manager object>
- ui_panels
Type: Reverse
ForeignKeyfromUiPanelAll ui panels of this workspace (related name of
workspace)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- class agency.corpus.models.Document(*args, **kwargs)[source]
Bases:
ModelDatabase table:
corpus_documentA document is a file or a URL that is indexed by the system.
- Parameters:
id (UUIDField) – Primary key: Id
title (CharField) – Title
file (FileField) – File
source_url (URLField) – Source url
summary (TextField) – Summary
checksum_sha256 (CharField) – Checksum sha256
status (CharField) – Status
error_message (TextField) – Error message
page_count (PositiveIntegerField) – Page count
chunk_count (PositiveIntegerField) – Chunk count
indexed_at (DateTimeField) – Indexed at
created_at (DateTimeField) – Created at
updated_at (DateTimeField) – Updated at
Relationship fields:
- Parameters:
workspace (
ForeignKeytoWorkspace) – Workspace (related name:documents)
Reverse relationships:
- Parameters:
extractions (Reverse
ForeignKeyfromExtraction) – All extractions of this document (related name ofdocument)assistants (Reverse
ManyToManyFieldfromAssistant) – All assistants of this document (related name ofdocuments)
- class Status(value)[source]
Bases:
TextChoices- UPLOADED = 'UPLOADED'
- PARSING = 'PARSING'
- INDEXING = 'INDEXING'
- INDEXED = 'INDEXED'
- FAILED = 'FAILED'
- DELETED = 'DELETED'
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- workspace
Type:
ForeignKeytoWorkspaceWorkspace (related name:
documents)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- title
Type:
CharFieldTitle
A wrapper for a deferred-loading field. When the value is read from this
- file
Type:
FileFieldFile
The descriptor for the file attribute on the model instance. Return a FieldFile when accessed so you can write code like:
>>> from myapp.models import MyModel >>> instance = MyModel.objects.get(pk=1) >>> instance.file.size
Assign a file object on assignment so you can do:
>>> with open('/path/to/hello.world') as f:
- source_url
Type:
URLFieldSource url
A wrapper for a deferred-loading field. When the value is read from this
- summary
Type:
TextFieldSummary
A wrapper for a deferred-loading field. When the value is read from this
- checksum_sha256
Type:
CharFieldChecksum sha256
A wrapper for a deferred-loading field. When the value is read from this
- status
Type:
CharFieldStatus
Choices:
UPLOADEDPARSINGINDEXINGINDEXEDFAILEDDELETED
A wrapper for a deferred-loading field. When the value is read from this
- error_message
Type:
TextFieldError message
A wrapper for a deferred-loading field. When the value is read from this
- page_count
Type:
PositiveIntegerFieldPage count
A wrapper for a deferred-loading field. When the value is read from this
- chunk_count
Type:
PositiveIntegerFieldChunk count
A wrapper for a deferred-loading field. When the value is read from this
- indexed_at
Type:
DateTimeFieldIndexed at
A wrapper for a deferred-loading field. When the value is read from this
- created_at
Type:
DateTimeFieldCreated at
A wrapper for a deferred-loading field. When the value is read from this
- updated_at
Type:
DateTimeFieldUpdated at
A wrapper for a deferred-loading field. When the value is read from this
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- assistants
Type: Reverse
ManyToManyFieldfromAssistantAll assistants of this document (related name of
documents)Accessor to the related objects manager on the forward and reverse sides of a many-to-many relation.
In the example:
class Pizza(Model): toppings = ManyToManyField(Topping, related_name='pizzas')
Pizza.toppingsandTopping.pizzasareManyToManyDescriptorinstances.Most of the implementation is delegated to a dynamically defined manager
- extractions
Type: Reverse
ForeignKeyfromExtractionAll extractions of this document (related name of
document)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- get_next_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=True, **kwargs)
Finds next instance based on
created_at. Seeget_next_by_FOO()for more information.
- get_next_by_updated_at(*, field=<django.db.models.DateTimeField: updated_at>, is_next=True, **kwargs)
Finds next instance based on
updated_at. Seeget_next_by_FOO()for more information.
- get_previous_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=False, **kwargs)
Finds previous instance based on
created_at. Seeget_previous_by_FOO()for more information.
- get_previous_by_updated_at(*, field=<django.db.models.DateTimeField: updated_at>, is_next=False, **kwargs)
Finds previous instance based on
updated_at. Seeget_previous_by_FOO()for more information.
- get_status_display(*, field=<django.db.models.CharField: status>)
Shows the label of the
status. Seeget_FOO_display()for more information.
- objects = <django.db.models.Manager object>
- class agency.corpus.models.Extraction(id, document, created_at, method)[source]
Bases:
ModelDatabase table:
corpus_extraction- Parameters:
id (UUIDField) – Primary key: Id
created_at (DateTimeField) – Created at
method (CharField) – Method
Relationship fields:
- Parameters:
document (
ForeignKeytoDocument) – Document (related name:extractions)
Reverse relationships:
- Parameters:
extracts (Reverse
ForeignKeyfromExtract) – All extracts of this extraction (related name ofextraction)sections (Reverse
ForeignKeyfromSection) – All sections of this extraction (related name ofextraction)
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- document
Type:
ForeignKeytoDocumentDocument (related name:
extractions)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- created_at
Type:
DateTimeFieldCreated at
A wrapper for a deferred-loading field. When the value is read from this
- method
Type:
CharFieldMethod
A wrapper for a deferred-loading field. When the value is read from this
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- extracts
Type: Reverse
ForeignKeyfromExtractAll extracts of this extraction (related name of
extraction)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- get_next_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=True, **kwargs)
Finds next instance based on
created_at. Seeget_next_by_FOO()for more information.
- get_previous_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=False, **kwargs)
Finds previous instance based on
created_at. Seeget_previous_by_FOO()for more information.
- objects = <django.db.models.Manager object>
- sections
Type: Reverse
ForeignKeyfromSectionAll sections of this extraction (related name of
extraction)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- class agency.corpus.models.Extract(id, extraction, text, page)[source]
Bases:
ModelDatabase table:
corpus_extract- Parameters:
id (UUIDField) – Primary key: Id
text (TextField) – Text
page (PositiveIntegerField) – Page
Relationship fields:
- Parameters:
extraction (
ForeignKeytoExtraction) – Extraction (related name:extracts)
Reverse relationships:
- Parameters:
chunks (Reverse
ForeignKeyfromChunk) – All chunks of this extract (related name ofextract)
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- extraction
Type:
ForeignKeytoExtractionExtraction (related name:
extracts)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- page
Type:
PositiveIntegerFieldPage
A wrapper for a deferred-loading field. When the value is read from this
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- chunks
Type: Reverse
ForeignKeyfromChunkAll chunks of this extract (related name of
extract)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- extraction_id
Internal field, use
extractioninstead.
- objects = <django.db.models.Manager object>
- class agency.corpus.models.Section(*args, **kwargs)[source]
Bases:
ModelDatabase table:
corpus_sectionOne section can be se
Relationship fields:
- Parameters:
extraction (
ForeignKeytoExtraction) – Extraction (related name:sections)
Reverse relationships:
- Parameters:
chunks (Reverse
ForeignKeyfromChunk) – All chunks of this section (related name ofsection)
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- extraction
Type:
ForeignKeytoExtractionExtraction (related name:
sections)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- title
Type:
CharFieldTitle
A wrapper for a deferred-loading field. When the value is read from this
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- chunks
Type: Reverse
ForeignKeyfromChunkAll chunks of this section (related name of
section)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- extraction_id
Internal field, use
extractioninstead.
- objects = <django.db.models.Manager object>
- class agency.corpus.models.Chunk(id, extract, section, metadata, text, clear)[source]
Bases:
ModelDatabase table:
corpus_chunk- Parameters:
id (UUIDField) – Primary key: Id
metadata (JSONField) – Metadata
text (TextField) – Text
clear (BooleanField) – Clear
Relationship fields:
- Parameters:
extract (
ForeignKeytoExtract) – Extract (related name:chunks)section (
ForeignKeytoSection) – Section (related name:chunks)
- id
Type:
UUIDFieldPrimary key: Id
A wrapper for a deferred-loading field. When the value is read from this
- extract
Type:
ForeignKeytoExtractExtract (related name:
chunks)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- section
Type:
ForeignKeytoSectionSection (related name:
chunks)Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
- metadata
Type:
JSONFieldMetadata
A wrapper for a deferred-loading field. When the value is read from this
- clear
Type:
BooleanFieldClear
A wrapper for a deferred-loading field. When the value is read from this
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- objects = <django.db.models.Manager object>