
    9i'                       U d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZ d dlmZmZ d dlZd dlZd dlmZ d dlmZ d d	lmZ e
eef   Zi Zd
ed<    ej<                         ZddZ ddZ!ddZ"ddZ#ddZ$ddZ%dddZ&y)    )annotationsN)SentenceTransformermodels)Path)IterableUnionListSetAnyDict)AutoTokenizerPreTrainedTokenizerBase)QdrantClient)config_param)
CONFIG_DIRz"dict[str, PreTrainedTokenizerBase]_TOKENIZER_CACHEc                    t          d|  }t        t        |      d      xs i }t        |t              r|j                  di       ni }t        |t              st        d      |S )N/profiles)config_filefieldeqp_manualsz<Invalid 'profiles.eqp_manuals' structure in rag_config.yaml.)r   r   str
isinstancedictget
ValueError)configcfg_pathr   profs       /C:\Projects\mas-dev\aom\tools\rag\rag_helper.pyload_rag_profiler"      s]    Qvh'HHZHNBH.84.H8<<r*bDdD!WXXK    c                f    t        | d      sd t        _        t        | d      sd t        _        yy)z
    Ensure the model has:
      - encode_query(texts, **kw)
      - encode_document(texts, **kw)
    Both normalize embeddings (good for COSINE in Qdrant).
    encode_queryc                ,     | j                   |fddi|S Nnormalize_embeddingsTencodeselfxkws      r!   <lambda>z attach_helpers.<locals>.<lambda>%   s%    B
$(B
,.B
r#   encode_documentc                ,     | j                   |fddi|S r'   r)   r+   s      r!   r/   z attach_helpers.<locals>.<lambda>)   s%    KDKKE
$(E
,.E
r#   N)hasattrr   r%   r0   )sts    r!   attach_helpersr4      s6     2~&,
( 2()/
+ *r#   c                Z   	 t        | |      }t        |       ||_        |S # t        t        f$ rx}t        d|  d|        t	        j
                  | t        j                  ddd      }t	        j                  |j                         d      }t        ||g|      }Y d	}~d	}~ww xY w)
a  
    Loads a SentenceTransformer model instance from a local directory. If loading directly fails, a fallback
    mechanism is implemented to initialize the model using specific transformer and pooling components.

    This function also attaches helper methods to the SentenceTransformer instance and sets the maximum
    sequence length for the model.

    :param model_dir: The directory path where the model files are stored.
    :type model_dir: Str
    :param device: The computation device to be used (e.g., "cpu" or "cuda").
    :type device: Str
    :param max_seq_len: The maximum sequence length to be set for the model.
    :type max_seq_len: Int
    :return: Loaded and configured SentenceTransformer model.
    :rtype: SentenceTransformer
    )devicez-Error loading SentenceTransformer model from z: T)torch_dtypelocal_files_onlylow_cpu_mem_usage)
model_args)pooling_mode_mean_tokens)modulesr6   N)r   FileNotFoundErrorr   printr   Transformertorchfloat32Poolingget_word_embedding_dimensionr4   max_seq_length)	model_dirr6   max_seq_lenr3   ewordpools          r!   load_local_strJ   -   s    "	F 6: 2#BI z* F=i[1#NO!!',}}$eij
 ~~d??A\`a $fEFs   # B*A.B%%B*c                   t        | t              s2t        j                  j                  t        |       v sdt        |       v r#t        t        |       j                               }nt        |       }|t        v r	t        |   S t        5  |t        v rt        |   cd d d        S 	 t        j                  |dd      }|t        |<   |cd d d        S # t        $ r t        j                  |d      }Y 6w xY w# 1 sw Y   y xY w)Nr   T)use_fastr8   )rL   )r   r   ospathsepr   resolver   	_TOK_LOCKr   from_pretrained	Exception)rE   keytoks      r!   get_tokenizerrV   M   s    )T"bggkkS^&CscR[nG\$y/))+,)n
$$	""#C( 
	D//dUYZC !$ 

  	D//dCC	D 
s0   C6$C<
C6 C30C62C33C66C?c                D    t        |      }t         || d      d         S )NF)add_special_tokens	input_ids)rV   len)textrE   rU   s      r!   count_tokensr\   a   s#    
	
"Cs4E2;?@@r#   c                    t        |      } |t        |       ddd      }|d   D cg c]  }t        |       c}S c c}w )NF)rX   padding
truncationrY   )rV   listrZ   )textsrE   rU   encidss        r!   count_tokens_batchrd   f   sC    
	
"C
d5keUu
UC #K 01 0CH 0111s   =c                   t               }d}	 | j                  |d|dd|      \  }}|s	 t        |      S |D ]@  }|j                  xs i }|j                  d      }|s'|j	                  t        |             B |s	 t        |      S |)zTScroll the collection and return a sorted list of distinct payload['doc_id'] values.NTF)collection_namescroll_filterlimitwith_payloadwith_vectorsoffsetdoc_id)setscrollpayloadr   addr   sorted)	client
collectionbatchdoc_ids	next_pagepointspro   ds	            r!   list_doc_idsrz   l   s    GI
"MM& * 
	  '? A&'iio2GH%ACF#	 
 '?% r#   )r   r   returnr   )r3   r   r{   None)rE   r   r6   r   rF   intr{   r   )rE   	ModelPathr{   r   )r[   r   rE   r~   r{   r}   )ra   zIterable[str]rE   r~   r{   z	List[int])i  )rr   r   rs   r   rt   r}   r{   z	List[str])'
__future__r   r@   sentence_transformersr   r   pathlibr   typingr   r   r	   r
   r   r   transformersr   r   	threadingrM   qdrant_clientr   aom.utils.utilitiesr   aom.definitionsr   r   r~   r   __annotations__RLockrQ   r"   r4   rJ   rV   r\   rd   rz    r#   r!   <module>r      sw    #  =  8 8 ?  & , & #t)	79 4 9IOO	
 @(A
2r#   