Multimodal: Fix memory leak with MMEmbeddings

On a basic python class, class attributes are handled by reference,
meaning that every instance of embeddings would attach to that reference
and allocate more memory.

Switch to a Pydantic class and factory methods when instantiating.

Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>
This commit is contained in:
kingbri 2025-02-02 12:21:19 -05:00
parent bd16681825
commit 96e8375ec8

View file

@ -1,20 +1,20 @@
from typing import List
from backends.exllamav2.vision import get_image_embedding
from common import model
from loguru import logger
from pydantic import BaseModel, Field
from typing import List
from common.optional_dependencies import dependencies
if dependencies.exllamav2:
from exllamav2 import ExLlamaV2VisionTower
class MultimodalEmbeddingWrapper:
class MultimodalEmbeddingWrapper(BaseModel):
"""Common multimodal embedding wrapper"""
type: str = None
content: List = []
text_alias: List[str] = []
content: list = Field(default_factory=list)
text_alias: List[str] = Field(default_factory=list)
async def add(self, url: str):
# Determine the type of vision embedding to use