
    ^hY:                     b    S r SSKrSSKrSSKrSSKJr  SSKrSSKJr  SSK	J
r
   " S S\
5      rg)	z
Qdrant Semantic Cache implementation

Has 4 methods:
    - set_cache
    - get_cache
    - async_set_cache
    - async_get_cache
    N)Any)print_verbose   )	BaseCachec                   ^    \ rS rSr       SS jrS\4S jrS rS rS r	S	 r
S
 rS rSrg)QdrantSemanticCache   Nc                    SS K nSSKJn	Jn
Jn  SSKJn  Uc  [        S5      eX0l        [        SU R                   35        Uc  [        S5      eX@l
        X`l        0 nU(       a3  [        U[        5      (       a  UR                  S5      (       a  U" U5      nU(       a3  [        U[        5      (       a  UR                  S5      (       a  U" U5      nU=(       d)    UR                  S5      =(       d    UR                  S	5      nU=(       d    UR                  S
5      nSS0nU(       a  X-S'   Uc  [!        S5      eXl        X l        [        SU R"                   35        Xl        U	" 5       U l        U
" UR*                  S9U l        Uc  [        S5        U R(                  R/                  U R"                   SU R                   S3U R&                  S9nUR0                  S:w  a  [!        SUR2                   35      eUR5                  5       S   S   (       ak  U R(                  R/                  U R"                   SU R                   3U R&                  S9nUR5                  5       U l        [        SU R6                   35        g Ub  US:X  a  SSS00nO(US:X  a	  SSSSS .0nOUS!:X  a  S!S"SS#.0nO[        S$5      eU R(                  R9                  U R"                   SU R                   3S%S&S'.US(.U R&                  S)9nUR5                  5       S   (       ak  U R(                  R/                  U R"                   SU R                   3U R&                  S9nUR5                  5       U l        [        S*U R6                   35        g [        S+5      e),Nr   )_get_httpx_clientget_async_httpx_clienthttpxSpecialProvider)get_secret_strz-collection_name must be provided, passed Nonez0qdrant semantic-cache initializing COLLECTION - z2similarity_threshold must be provided, passed Nonezos.environ/
QDRANT_URLQDRANT_API_BASEQDRANT_API_KEYzContent-Typezapplication/jsonzapi-keyzQdrant url must be providedz'qdrant semantic-cache qdrant_api_base: )llm_providerzNQuantization config is not provided. Default binary quantization will be used./collections/z/exists)urlheaders   z1Error from qdrant checking if /collections exist resultexistsz.Collection already exists.
Collection details:binary
always_ramFscalarint8gGz?)typequantiler   productx16)compressionr   zBQuantization config must be one of 'scalar', 'binary' or 'product'i   Cosine)sizedistance)vectorsquantization_config)r   jsonr   z+New collection created.
Collection details:z#Error while creating new collection)os&litellm.llms.custom_httpx.http_handlerr   r   r   litellm.secret_managers.mainr   	Exceptioncollection_namer   similarity_thresholdembedding_model
isinstancestr
startswithgetenv
ValueErrorqdrant_api_baseqdrant_api_keyr   sync_clientCachingasync_clientgetstatus_codetextr'   collection_infoput)selfr4   r5   r,   r-   r&   r.   	host_typer(   r   r   r   r   r   collection_existscollection_detailsquantization_paramsnew_collection_statuss                     _/home/james-whalen/.local/lib/python3.13/site-packages/litellm/caching/qdrant_semantic_cache.py__init__QdrantSemanticCache.__init__   s    		
 	

 	@"KLL.>t?S?S>TU	
  'PQQ$8!. /3//O4N4N5 5 #1"A.#..>3L3L4 4 "0!? Vryy6V"))DU:V 	 (F2995E+F!#56!/I":;;.,?@T@T?UVW,.2-55
 &` !,,00''(d6J6J5K7SLL 1 
 ((C/CDUDZDZC[\  !!#H-h7!%!1!1!5!5++,M$:N:N9OP "6 " $6#:#:#<D A$BVBVAWX #*.AX.M$e'#
 %0v4uU'# %	1uEJ'#  X  %)$4$4$8$8++,M$:N:N9OP(,(C+>  %9 %! %))+H5%)%5%5%9%9//0d>R>R=ST LL &: &" (:'>'>'@$B4CWCWBXY   EFF    cached_responsec                     Uc  U$  [         R                  " U5      nU$ ! [         a    [        R                  " U5      n U$ f = fN)r'   loadsr+   astliteral_eval)r>   rH   s     rD   _get_cache_logic$QdrantSemanticCache._get_cache_logic   sV    """	@"jjO
   	@!..?O	@s     AAc                    [        SU 35        SS KnUS   nSnU H
  nXgS   -  nM     [        R                  " U R                  USSS.S9nUS	   S   S
   n	[        U5      n[        U[
        5      (       d   eS[        UR                  5       5      U	UUS.S./0n
U R                  R                  U R                   SU R                   S3U R                  U
S9  g )Nz)qdrant semantic-cache set_cache, kwargs: r   messages contentTzno-storezno-cachemodelinputcachedata	embeddingpointsr;   responseidvectorpayloadr   /pointsr   r   r'   )r   uuidlitellmrZ   r.   r0   r/   uuid4r6   r=   r4   r,   r   )r>   keyvaluekwargsrd   rQ   promptmessageembedding_responserZ   rY   s              rD   	set_cacheQdrantSemanticCache.set_cache   s   A&JK *%Gi((F   %..&&#6
 'v.q1+>	E
%%%%% djjl+' &$) 	
 	''(d6J6J5K7SLL 	 	

 	rG   c           
         [        SU 35        US   nSnU H
  nXES   -  nM     [        R                  " U R                  USSS.S9nUS   S	   S
   nUSSSSS.0SSS.nU R                  R                  U R                   SU R                   S3U R                  US9n	U	R                  5       S   n
U
c  g [        U
[        5      (       a  [        U
5      S	:X  a  g U
S	   S   nU
S	   S   S   n[        SU R                   SU SU SU 35        XR                  :  a.  U
S	   S   S   n[        SU SU SU 35        U R                  US 9$ g )!Nz.sync qdrant semantic-cache get_cache, kwargs: rQ   rR   rS   TrT   rU   rY   r   rZ   quantizationF      @ignorerescoreoversamplingr   r`   paramslimitwith_payloadr   /points/searchrc   r   scorera   r;   &semantic cache: similarity threshold: , similarity: 
, prompt: , closest_cached_prompt: r]   got a cache hit, similarity: , Current prompt: , cached_prompt: rH   )r   re   rZ   r.   r6   postr4   r,   r   r'   r/   listlenr-   rN   )r>   rg   ri   rQ   rj   rk   rl   rZ   rY   search_responseresults
similaritycached_promptcached_values                 rD   	get_cacheQdrantSemanticCache.get_cache   s   FvhOP *%Gi((F   %..&&#6
 'v.q1+>	  ##$'!  
 **//''(d6J6J5K>ZLL 0 

 "&&(2?gt$$7|q QZ(

9-f5 	4T5N5N4O~^h]iistzs{  |U  Vc  Ud  e	
 222"1:i0<L/
|;MfXUfgtfuv (((FF rG   c                 >  #    SS K nSSKJnJn  [	        SU 35        US   nSnU H
  n	XS   -  nM     Ub  U V
s/ s H  oS   PM	     sn
O/ nUb~  U R
                  U;   an  UR                  S0 5      R                  S	S5      nUR                  U R
                  US
S
S.US
UR                  S0 5      R                  SS 5      S.S9I S h  vN nO+[        R                  " U R
                  US
S
S.S9I S h  vN nUS   S   S   n[        U5      n[        U[        5      (       d   eS[        UR                  5       5      UUUS.S./0nU R                  R                  U R                   SU R                   S3U R                   US9I S h  vN   g s  sn
f  N N N7f)Nr   llm_model_list
llm_routerz/async qdrant semantic-cache set_cache, kwargs: rQ   rR   rS   
model_namemetadatauser_api_keyTrT   trace_idr   zsemantic-cache-embeddingr   rV   rW   rX   r   rU   rY   rZ   r[   r\   r^   r   rb   rc   )rd   litellm.proxy.proxy_serverr   r   r   r.   r9   
aembeddingre   r0   r/   rf   r8   r=   r4   r,   r   )r>   rg   rh   ri   rd   r   r   rQ   rj   rk   mrouter_model_namesr   rl   rZ   rY   s                   rD   async_set_cache#QdrantSemanticCache.async_set_cache  s    IGxPQ *%Gi((F  
 ) '55n|_n5 	
 !d&:&:>P&P!::j"599."ML'1'<'<**#'T:$004 &

:r : > >z4 P	 (= 	( 	" (/'9'9**#'T:( " 'v.q1+>	E
%%%%% djjl+' &$) 	
 ##''(d6J6J5K7SLL $ 
 	
 	

 	a 6	""2	
sB   9FF	B F	F
+F5F6BFFFFFc                   #    [        SU 35        SSKJnJn  US   nSnU H
  nXgS   -  nM     Ub  U Vs/ s H  oS   PM	     snO/ n	Ub~  U R                  U	;   an  UR                  S0 5      R                  S	S5      n
UR                  U R                  US
S
S.U
S
UR                  S0 5      R                  SS 5      S.S9I S h  vN nO+[        R                  " U R                  US
S
S.S9I S h  vN nUS   S   S   nUSSS
SS.0SS
S.nU R                  R                  U R                   SU R                   S3U R                  US9I S h  vN nUR                  5       S   nUc  SUR                  S0 5      S'   g [        U[         5      (       a%  [#        U5      S:X  a  SUR                  S0 5      S'   g US   S   nUS   S   S    n[        S!U R$                   S"U S#U S$U 35        UUR                  S0 5      S'   UU R$                  :  a.  US   S   S%   n[        S&U S'U S(U 35        U R'                  US)9$ g s  snf  GN GNZ N7f)*Nz/async qdrant semantic-cache get_cache, kwargs: r   r   rQ   rR   rS   r   r   r   TrT   r   r   r   rU   rY   rZ   rp   Frq   rr   r   rv   r   rz   rc   r   g        zsemantic-similarityr{   ra   r;   r|   r}   r~   r   r]   r   r   r   r   )r   r   r   r   r.   r9   r   re   r8   r   r4   r,   r   r'   
setdefaultr/   r   r   r-   rN   )r>   rg   ri   r   r   rQ   rj   rk   r   r   r   rl   rZ   rY   r   r   r   r   r   s                      rD   async_get_cache#QdrantSemanticCache.async_get_cacheM  s    GxPQI *%Gi((F  
 ) '55n|_n5 	
 !d&:&:>P&P!::j"599."ML'1'<'<**#'T:$004 &

:r : > >z4 P	 (= 	( 	" (/'9'9**#'T:( " 'v.q1+>	  ##$'!  
 !% 1 1 6 6''(d6J6J5K>ZLL !7 !
 
 "&&(2?GJFj"-.CDgt$$7|q KN!!*b12GHQZ(

9-f5 	4T5N5N4O~^h]iistzs{  |U  Vc  Ud  e	

 DN*b)*?@222"1:i0<L/
|;MfXUfgtfuv (((FF Y 6	"",
sC   5IIB II+I1I2AIIC8IIIc                 "   #    U R                   $ 7frJ   )r<   )r>   s    rD   _collection_info$QdrantSemanticCache._collection_info  s     ###s   c                    #    / nU H,  nUR                  U R                  " US   US   40 UD65        M.     [        R                  " U6 I S h  vN   g  N7f)Nr   r   )appendr   asynciogather)r>   
cache_listri   tasksvals        rD   async_set_cache_pipeline,QdrantSemanticCache.async_set_cache_pipeline  sL     CLL--c!fc!fGGH nne$$$s   AAAA)	r8   r<   r,   r.   r   r4   r5   r-   r6   )NNNNNztext-embedding-ada-002N)__name__
__module____qualname____firstlineno__rE   r   rN   rm   r   r   r   r   r   __static_attributes__ rG   rD   r   r      sP     ! 0GB	 	(T>@>@Xt$%rG   r   )__doc__rL   r   r'   typingr   re   litellm._loggingr   
base_cacher   r   r   rG   rD   <module>r      s/         * !X%) X%rG   