|
| 1 | +from datetime import datetime |
| 2 | +from enum import Enum |
| 3 | +from typing import Any, Literal |
| 4 | +from uuid import UUID, uuid4 |
| 5 | + |
| 6 | +from pydantic import HttpUrl, model_validator |
| 7 | +from sqlmodel import Field, Relationship, SQLModel |
| 8 | + |
| 9 | +from app.core.util import now |
| 10 | +from app.models.document import DocumentPublic |
| 11 | +from .project import Project |
| 12 | + |
| 13 | + |
| 14 | +class ProviderType(str, Enum): |
| 15 | + """Supported LLM providers for collections.""" |
| 16 | + |
| 17 | + OPENAI = "OPENAI" |
| 18 | + # BEDROCK = "bedrock" |
| 19 | + # GEMINI = "gemini" |
| 20 | + |
| 21 | + |
| 22 | +class Collection(SQLModel, table=True): |
| 23 | + """Database model for Collection operations.""" |
| 24 | + |
| 25 | + id: UUID = Field( |
| 26 | + default_factory=uuid4, |
| 27 | + primary_key=True, |
| 28 | + description="Unique identifier for the collection", |
| 29 | + sa_column_kwargs={"comment": "Unique identifier for the collection"}, |
| 30 | + ) |
| 31 | + provider: ProviderType = ( |
| 32 | + Field( |
| 33 | + nullable=False, |
| 34 | + description="LLM provider used for this collection (e.g., 'openai', 'bedrock', 'gemini', etc)", |
| 35 | + sa_column_kwargs={"LLM provider used for this collection"}, |
| 36 | + ), |
| 37 | + ) |
| 38 | + llm_service_id: str = Field( |
| 39 | + nullable=False, |
| 40 | + description="External LLM service identifier (e.g., OpenAI vector store ID)", |
| 41 | + sa_column_kwargs={ |
| 42 | + "comment": "External LLM service identifier (e.g., OpenAI vector store ID)" |
| 43 | + }, |
| 44 | + ) |
| 45 | + llm_service_name: str = Field( |
| 46 | + nullable=False, |
| 47 | + description="Name of the LLM service", |
| 48 | + sa_column_kwargs={"comment": "Name of the LLM service"}, |
| 49 | + ) |
| 50 | + name: str = Field( |
| 51 | + nullable=True, |
| 52 | + unique=True, |
| 53 | + description="Name of the collection", |
| 54 | + sa_column_kwargs={"comment": "Name of the collection"}, |
| 55 | + ) |
| 56 | + description: str = Field( |
| 57 | + nullable=True, |
| 58 | + description="Description of the collection", |
| 59 | + sa_column_kwargs={"comment": "Description of the collection"}, |
| 60 | + ) |
| 61 | + project_id: int = Field( |
| 62 | + foreign_key="project.id", |
| 63 | + nullable=False, |
| 64 | + ondelete="CASCADE", |
| 65 | + description="Project the collection belongs to", |
| 66 | + sa_column_kwargs={"comment": "Reference to the project"}, |
| 67 | + ) |
| 68 | + inserted_at: datetime = Field( |
| 69 | + default_factory=now, |
| 70 | + description="Timestamp when the collection was created", |
| 71 | + sa_column_kwargs={"comment": "Timestamp when the collection was created"}, |
| 72 | + ) |
| 73 | + updated_at: datetime = Field( |
| 74 | + default_factory=now, |
| 75 | + description="Timestamp when the collection was updated", |
| 76 | + sa_column_kwargs={"comment": "Timestamp when the collection was last updated"}, |
| 77 | + ) |
| 78 | + deleted_at: datetime | None = Field( |
| 79 | + default=None, |
| 80 | + description="Timestamp when the collection was deleted", |
| 81 | + sa_column_kwargs={"comment": "Timestamp when the collection was deleted"}, |
| 82 | + ) |
| 83 | + project: Project = Relationship(back_populates="collections") |
| 84 | + |
| 85 | + |
| 86 | +# Request models |
| 87 | +class CollectionOptions(SQLModel): |
| 88 | + name: str | None = Field(default=None, description="Name of the collection") |
| 89 | + description: str | None = Field( |
| 90 | + default=None, description="Description of the collection" |
| 91 | + ) |
| 92 | + documents: list[UUID] = Field( |
| 93 | + description="List of document IDs", |
| 94 | + ) |
| 95 | + batch_size: int = Field( |
| 96 | + default=1, |
| 97 | + description=( |
| 98 | + "Number of documents to send to OpenAI in a single " |
| 99 | + "transaction. See the `file_ids` parameter in the " |
| 100 | + "vector store [create batch](https://platform.openai.com/docs/api-reference/vector-stores-file-batches/createBatch)." |
| 101 | + ), |
| 102 | + ) |
| 103 | + |
| 104 | + def model_post_init(self, __context: Any): |
| 105 | + self.documents = list(set(self.documents)) |
| 106 | + |
| 107 | + |
| 108 | +class AssistantOptions(SQLModel): |
| 109 | + # Fields to be passed along to OpenAI. They must be a subset of |
| 110 | + # parameters accepted by the OpenAI.clien.beta.assistants.create |
| 111 | + # API. |
| 112 | + model: str | None = Field( |
| 113 | + default=None, |
| 114 | + description=( |
| 115 | + "**[Deprecated]** " |
| 116 | + "OpenAI model to attach to this assistant. The model " |
| 117 | + "must be compatable with the assistants API; see the " |
| 118 | + "OpenAI [model documentation](https://platform.openai.com/docs/models/compare) for more." |
| 119 | + ), |
| 120 | + ) |
| 121 | + |
| 122 | + instructions: str | None = Field( |
| 123 | + default=None, |
| 124 | + description=( |
| 125 | + "**[Deprecated]** " |
| 126 | + "Assistant instruction. Sometimes referred to as the " |
| 127 | + '"system" prompt.' |
| 128 | + ), |
| 129 | + ) |
| 130 | + temperature: float = Field( |
| 131 | + default=1e-6, |
| 132 | + description=( |
| 133 | + "**[Deprecated]** " |
| 134 | + "Model temperature. The default is slightly " |
| 135 | + "greater-than zero because it is [unknown how OpenAI " |
| 136 | + "handles zero](https://community.openai.com/t/clarifications-on-setting-temperature-0/886447/5)." |
| 137 | + ), |
| 138 | + ) |
| 139 | + |
| 140 | + @model_validator(mode="before") |
| 141 | + def _assistant_fields_all_or_none(cls, values: dict[str, Any]) -> dict[str, Any]: |
| 142 | + def norm(x: Any) -> Any: |
| 143 | + if x is None: |
| 144 | + return None |
| 145 | + if isinstance(x, str): |
| 146 | + s = x.strip() |
| 147 | + return s if s else None |
| 148 | + return x # let Pydantic handle non-strings |
| 149 | + |
| 150 | + model = norm(values.get("model")) |
| 151 | + instructions = norm(values.get("instructions")) |
| 152 | + |
| 153 | + if (model is None) ^ (instructions is None): |
| 154 | + raise ValueError( |
| 155 | + "To create an Assistant, provide BOTH 'model' and 'instructions'. " |
| 156 | + "If you only want a vector store, remove both fields." |
| 157 | + ) |
| 158 | + |
| 159 | + values["model"] = model |
| 160 | + values["instructions"] = instructions |
| 161 | + return values |
| 162 | + |
| 163 | + |
| 164 | +class CallbackRequest(SQLModel): |
| 165 | + callback_url: HttpUrl | None = Field( |
| 166 | + default=None, |
| 167 | + description="URL to call to report endpoint status", |
| 168 | + ) |
| 169 | + |
| 170 | + |
| 171 | +class ProviderOptions(SQLModel): |
| 172 | + """LLM provider configuration.""" |
| 173 | + |
| 174 | + provider: Literal["openai"] = Field( |
| 175 | + default="openai", description="LLM provider to use for this collection" |
| 176 | + ) |
| 177 | + |
| 178 | + |
| 179 | +class CreationRequest( |
| 180 | + AssistantOptions, |
| 181 | + CollectionOptions, |
| 182 | + ProviderOptions, |
| 183 | + CallbackRequest, |
| 184 | +): |
| 185 | + def extract_super_type(self, cls: "CreationRequest"): |
| 186 | + for field_name in cls.model_fields.keys(): |
| 187 | + field_value = getattr(self, field_name) |
| 188 | + yield (field_name, field_value) |
| 189 | + |
| 190 | + |
| 191 | +class DeletionRequest(CallbackRequest): |
| 192 | + collection_id: UUID = Field(description="Collection to delete") |
| 193 | + |
| 194 | + |
| 195 | +# Response models |
| 196 | + |
| 197 | + |
| 198 | +class CollectionIDPublic(SQLModel): |
| 199 | + id: UUID |
| 200 | + |
| 201 | + |
| 202 | +class CollectionPublic(SQLModel): |
| 203 | + id: UUID |
| 204 | + llm_service_id: str |
| 205 | + llm_service_name: str |
| 206 | + project_id: int |
| 207 | + |
| 208 | + inserted_at: datetime |
| 209 | + updated_at: datetime |
| 210 | + deleted_at: datetime | None = None |
| 211 | + |
| 212 | + |
| 213 | +class CollectionWithDocsPublic(CollectionPublic): |
| 214 | + documents: list[DocumentPublic] | None = None |
0 commit comments