§
    æ˜ºhÌ  ã                   ó`   — d dl Z d dl mZmZ d dlmZmZ ddlmZ  G d„ dej        ¦  «        ZdS )é    N)ÚnnÚTensor)ÚIterableÚDicté   )ÚSentenceTransformerc                   óŽ   ‡ — e Zd ZdZ ej        ¦   «          ej        ¦   «         fdefˆ fd„Zde	e
eef                  defd„Zˆ xZS )ÚCosineSimilarityLossa_  
    CosineSimilarityLoss expects, that the InputExamples consists of two texts and a float label.

    It computes the vectors u = model(input_text[0]) and v = model(input_text[1]) and measures the cosine-similarity between the two.
    By default, it minimizes the following loss: ||input_label - cos_score_transformation(cosine_sim(u,v))||_2.

    :param model: SentenceTransformer model
    :param loss_fct: Which pytorch loss function should be used to compare the cosine_similartiy(u,v) with the input_label? By default, MSE:  ||input_label - cosine_sim(u,v)||_2
    :param cos_score_transformation: The cos_score_transformation function is applied on top of cosine_similarity. By default, the identify function is used (i.e. no change).

    Example::

            from sentence_transformers import SentenceTransformer, SentencesDataset, InputExample, losses

            model = SentenceTransformer('distilbert-base-nli-mean-tokens')
            train_examples = [InputExample(texts=['My first sentence', 'My second sentence'], label=0.8),
                InputExample(texts=['Another pair', 'Unrelated sentence'], label=0.3)]
            train_dataset = SentencesDataset(train_examples, model)
            train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=train_batch_size)
            train_loss = losses.CosineSimilarityLoss(model=model)


    Úmodelc                 ó€   •— t          t          | ¦  «                             ¦   «          || _        || _        || _        d S )N)Úsuperr
   Ú__init__r   Úloss_fctÚcos_score_transformation)Úselfr   r   r   Ú	__class__s       €úy/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/sentence_transformers/losses/CosineSimilarityLoss.pyr   zCosineSimilarityLoss.__init__    s<   ø€ ÝÕ" DÑ)Ô)×2Ò2Ñ4Ô4Ð4ØˆŒ
Ø ˆŒØ(@ˆÔ%Ð%Ð%ó    Úsentence_featuresÚlabelsc                 óÚ   ‡ — ˆ fd„|D ¦   «         }‰                       t          j        |d         |d         ¦  «        ¦  «        }‰                      ||                     d¦  «        ¦  «        S )Nc                 óF   •— g | ]}‰                      |¦  «        d          ‘ŒS )Úsentence_embedding)r   )Ú.0Úsentence_featurer   s     €r   ú
<listcomp>z0CosineSimilarityLoss.forward.<locals>.<listcomp>'   s-   ø€ ÐsÐsÐsÐM]�d—j’jÐ!1Ñ2Ô2Ð3GÔHÐsÐsÐsr   r   é   éÿÿÿÿ)r   ÚtorchÚcosine_similarityr   Úview)r   r   r   Ú
embeddingsÚoutputs   `    r   ÚforwardzCosineSimilarityLoss.forward&   sg   ø€ ØsÐsÐsÐsÐarÐsÑsÔsˆ
Ø×.Ò.­uÔ/FÀzÐRSÄ}ÐV`ÐabÔVcÑ/dÔ/dÑeÔeˆØ�}Š}˜V V§[¢[°¡_¤_Ñ5Ô5Ð5r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚMSELossÚIdentityr   r   r   r   Ústrr   r$   Ú__classcell__)r   s   @r   r
   r
      s©   ø€ € € € € ðð ð0 =G¸B¼J¹L¼LÐcnÐceÔcnÑcpÔcpð Að AÐ1ð Að Að Að Að Að Að6¨°$°s¸F°{Ô2CÔ)Dð 6Èfð 6ð 6ð 6ð 6ð 6ð 6ð 6ð 6r   r
   )	r   r   r   Útypingr   r   r   ÚModuler
   © r   r   ú<module>r0      s†   ðØ €€€Ø Ð Ð Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ð !Ð !Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5ð"6ð "6ð "6ð "6ð "6˜2œ9ñ "6ô "6ð "6ð "6ð "6r   