(
self,
create_dict: Dict,
**kwargs,
)
| 19 | |
| 20 | class ChunkModelOperator(PostgresModelOperator): |
| 21 | async def create( |
| 22 | self, |
| 23 | create_dict: Dict, |
| 24 | **kwargs, |
| 25 | ) -> ModelEntity: |
| 26 | from app.services.retrieval.embedding import embed_documents |
| 27 | |
| 28 | # handle kwargs |
| 29 | self._check_kwargs(object_id_required=None, **kwargs) |
| 30 | collection_id = kwargs["collection_id"] |
| 31 | |
| 32 | request = ChunkCreateRequest(**create_dict) |
| 33 | content = request.content |
| 34 | metadata = request.metadata |
| 35 | |
| 36 | # Get collection |
| 37 | collection: Collection = await collection_ops.get(collection_id=collection_id) |
| 38 | |
| 39 | # Get model |
| 40 | embedding_model = await model_ops.get(model_id=collection.embedding_model_id) |
| 41 | |
| 42 | # check if collection has available capacity |
| 43 | if not collection.has_available_capacity(1): |
| 44 | raise_http_error( |
| 45 | ErrorCode.RESOURCE_LIMIT_REACHED, |
| 46 | message=f"Collection {collection_id} has no available capacity to store new chunks.", |
| 47 | ) |
| 48 | |
| 49 | # embed the document |
| 50 | embeddings = await embed_documents( |
| 51 | documents=[content], |
| 52 | embedding_model=embedding_model, |
| 53 | embedding_size=collection.embedding_size, |
| 54 | ) |
| 55 | embedding = embeddings[0] |
| 56 | |
| 57 | # create chunk |
| 58 | num_tokens = default_tokenizer.count_tokens(content) |
| 59 | new_chunk_id = Chunk.generate_random_id() |
| 60 | await db_chunk.create_chunk( |
| 61 | chunk_id=new_chunk_id, |
| 62 | collection=collection, |
| 63 | content=content, |
| 64 | embedding=embedding, |
| 65 | metadata=metadata, |
| 66 | num_tokens=num_tokens, |
| 67 | ) |
| 68 | |
| 69 | # get the created chunk |
| 70 | chunk = await self.get(collection_id=collection_id, chunk_id=new_chunk_id) |
| 71 | |
| 72 | # pop collection redis |
| 73 | await collection_ops.redis.pop(collection) |
| 74 | return chunk |
| 75 | |
| 76 | async def update( |
| 77 | self, |
nothing calls this directly
no test coverage detected