
    *j                        d Z ddlmZ ddlZddlmc mZ ddl	Z	ddl
ZddlZddlmZ ddlmZ dZ G d d          Z e            Zg d	Zej        d
             Zd Zd Zd Zd ZdS )uJ   Tests for runtime/conversation/retrieval.py — offline via stub embedder.    )annotationsN)build_index)retrieve    c                      e Zd Zddddd
ZdS )_HashEmbedderF)show_progress_barnormalize_embeddings	sentences	list[str]r	   boolr
   return
np.ndarrayc                  g }|D ]}t          j        |                                                                          }|t          t          |          z  dz   z  d t                   }t          j        |t          j                  	                    t          j
                  dz
  }|r*t          j                            |          }	|	dk    r||	z  }|                    |           t          j        |          S )N   )dtypeg      `@r   )hashlibsha256encodedigestDIMlennp
frombufferuint8astypefloat32linalgnormappendstack)
selfr   r	   r
   vecsshrawvecr   s
             E/Users/jameslopez/projects/MentorCore/tests/runtime/test_retrieval.pyr   z_HashEmbedder.encode   s      	 	Aqxxzz**1133As1vv)*DSD1C-28444;;BJGG%OC#  y~~c**!884KCKKx~~    N)r   r   r	   r   r
   r   r   r   )__name__
__module____qualname__r    r)   r(   r   r      s:        
 #(%*       r)   r   )z2You have power over your mind, not outside events.z)The impediment to action advances action.z Confine yourself to the present.c                N    | dz  }t          dt          i|t          d           |S )NindexsrcT)embedder	overwrite)r   PASSAGESFAKE)tmp_pathidxs     r(   	index_dirr7   -   s-    
W
C!3FFFFJr)   c                d   t          d| dt                    }t          |          }d}||k    }|st          j        d|fd||f          dt          j                    v st          j        t                    rt          j        t                    nddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          d	z  }d
d|iz  }t          t          j
        |                    d x}x}}d}|d         }||v }|slt          j        d|fd||f          t          j        |          t          j        |          dz  }	dd|	iz  }t          t          j
        |                    d x}x}}d}|d         }||v }|slt          j        d|fd||f          t          j        |          t          j        |          dz  }	dd|	iz  }t          t          j
        |                    d x}x}}d}|d         }||v }|slt          j        d|fd||f          t          j        |          t          j        |          dz  }	dd|	iz  }t          t          j
        |                    d x}x}}d S )Nzmind and power   top_kr1   r   )>)z/%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} > %(py6)sr   resultspy0py1py3py6assert %(py8)spy8chunk_id)in)z%(py1)s in %(py4)sr@   py4assert %(py6)srB   	source_idtextr   r4   r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation)
r7   r=   @py_assert2@py_assert5@py_assert4@py_format7@py_format9@py_assert0@py_assert3@py_format5s
             r(   test_retrieve_returns_resultsr]   4   s)   '!dKKKGw<<!<!<!33ww<!##:##########:#####:###############################$'!*$;*$$$$$$$$$$;*$$$$$;$$$$*$$$$$$$$$$$$$$$$$$$$$$$$$$$WQZ6Z6Z6Zr)   c                   t          d| dz  t                    }g }||k    }|st          j        d|fd||f          dt	          j                    v st          j        |          rt          j        |          ndt          j        |          dz  }dd	|iz  }t          t          j	        |                    d x}}d S )
Nanythingr/   )r1   ==)z%(py0)s == %(py3)sr=   )r?   rA   zassert %(py5)spy5)
r   r4   rM   rN   rO   rP   rQ   rR   rS   rT   )r5   r=   rU   @py_assert1@py_format4@py_format6s         r(   'test_retrieve_empty_index_returns_emptyrf   <   s    z8g#5EEEG7b=7b77br)   c                X   t          d| dt                    }t          |          }d}||k    }|st          j        d|fd||f          dt          j                    v st          j        t                    rt          j        t                    nddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }d	d
|iz  }t          t          j
        |                    d x}x}}d S )Nactionr9   r:   )<=)z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} <= %(py6)sr   r=   r>   rC   rD   rL   )r7   r=   rU   rV   rW   rX   rY   s          r(   test_retrieve_top_k_respectedrj   A   s9   x!dCCCGw<<1<1<133ww<1r)   c                H   t          d| dt                    }|D ]}|d         }d}||k    }|slt          j        d|fd||f          t          j        |          t          j        |          dz  }d	d
|iz  }t          t          j        |                    d x}x}}d S )Npresent   r:   rJ   r0   r`   )z%(py1)s == %(py4)srG   rI   rB   )r   r4   rM   rN   rR   rS   rT   )r7   r=   rrZ   r[   rU   r\   rX   s           r(   "test_retrieve_result_has_source_idro   F   s    y)1tDDDG ' '~&&~&&&&&&&&&&~&&&&&~&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&' 'r)   )__doc__
__future__r   builtinsrO   _pytest.assertion.rewrite	assertionrewriterM   r   numpyr   pytestcompiler.stages.indexr   runtime.conversation.retrievalr   r   r   r4   r3   fixturer7   r]   rf   rj   ro   r-   r)   r(   <module>r{      s'   P P " " " " " "                    - - - - - - 3 3 3 3 3 3       * }            
  
' ' ' ' 'r)   