
    hh                         d Z ddlmZmZmZ ddlZddlmZ ddl	m
Z
 ddlmZmZ dee         dee         d	efd
Z G d dee          ZdS )zSelect and order examples based on ngram overlap score (sentence_bleu score).

https://www.nltk.org/_modules/nltk/translate/bleu_score.html
https://aclanthology.org/P02-1040.pdf
    )AnyDictListN)BaseExampleSelector)PromptTemplate)	BaseModelmodel_validatorsourceexamplereturnc                     ddl m}m} | d                                         }d |D             }t	           ||| |            j        d                    S )aR  Compute ngram overlap score of source and example as sentence_bleu score
    from NLTK package.

    Use sentence_bleu with method1 smoothing function and auto reweighting.
    Return float value between 0.0 and 1.0 inclusive.
    https://www.nltk.org/_modules/nltk/translate/bleu_score.html
    https://aclanthology.org/P02-1040.pdf
    r   SmoothingFunctionsentence_bleuc                 6    g | ]}|                                 S  )split).0ss     p/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/example_selectors/ngram_overlap.py
<listcomp>z'ngram_overlap_score.<locals>.<listcomp>   s     ---!''))---    T)smoothing_functionauto_reweigh)nltk.translate.bleu_scorer   r   r   floatmethod1)r
   r   r   r   
hypotheses
referencess         r   ngram_overlap_scorer       s           
 ""J--W---J0022:		
 	
 	
  r   c                       e Zd ZU dZee         ed<   	 eed<   	 dZe	ed<   	  e
d          eded	efd
                        Zdeeef         d	dfdZdeeef         d	ee         fdZdS )NGramOverlapExampleSelectorzSelect and order examples based on ngram overlap score (sentence_bleu score
    from NLTK package).

    https://www.nltk.org/_modules/nltk/translate/bleu_score.html
    https://aclanthology.org/P02-1040.pdf
    examplesexample_promptg      	thresholdbefore)modevaluesr   c                 ^    	 ddl m}m} n"# t          $ r}t          d          |d}~ww xY w|S )z$Check that valid dependencies exist.r   r   zjNot all the correct dependencies for this ExampleSelect exist.Please install nltk with `pip install nltk`.N)r   r   r   ImportError)clsr(   r   r   es        r   check_dependenciesz.NGramOverlapExampleSelector.check_dependenciesD   sx    		          	 	 	?  	 s    
*%*r   Nc                 :    | j                             |           dS )zAdd new example to list.N)r#   append)selfr   s     r   add_examplez'NGramOverlapExampleSelector.add_exampleU   s    W%%%%%r   input_variablesc                    t          |                                          }g }t          | j                  }dg|z  }| j        j        d         }t          |          D ]'}t          || j        |         |         g          ||<   (	 t          j	        |          }||         | j
        k     s!t          ||         | j
        z
            dk     rn.|                    | j        |                    | j
        dz
  ||<   u|S )zReturn list of examples sorted by ngram_overlap_score with input.

        Descending order.
        Excludes any examples with ngram_overlap_score less than or equal to threshold.
        g        r   Tg&.>g      ?)listr(   lenr#   r$   r2   ranger    npargmaxr%   absr/   )	r0   r2   inputsr#   kscorefirst_prompt_template_keyiarg_maxs	            r   select_examplesz+NGramOverlapExampleSelector.select_examplesY   s    o,,..//	$($7$G$J!q 	 	A*q)*CDE E!HH	2i&&Gg//Cg/5 55 5 OODM'2333!^c1E'N	2 r   )__name__
__module____qualname____doc__r   dict__annotations__r   r%   r   r	   classmethodr   r   r-   strr1   r@   r   r   r   r"   r"   *   s           4jB""""6Iu	 _(###     [ $#&4S> &d & & & &tCH~ $t*      r   r"   )rD   typingr   r   r   numpyr7    langchain_core.example_selectorsr   langchain_core.promptsr   pydanticr   r	   rH   r   r    r"   r   r   r   <module>rN      s     # " " " " " " " " "     @ @ @ @ @ @ 1 1 1 1 1 1 / / / / / / / /S	 DI %    6J J J J J"5y J J J J Jr   