
    +#h                     x    d dl Z d dlZd dlmZ d dlZd dlmZ d dlm	Z	  G d de      Z
ddefdZ G d	 d
      Zy)    N)Union)LM)dotdictc                        e Zd ZdZd	deee   eeef   f   def fdZ	d
deeeeeef      f   fdZ
ddZdefdZ xZS )DummyLMz/Dummy language model for unit testing purposes.answersfollow_examplesc                 N    t         |   d       d| _        || _        || _        y)aD  Initializes the dummy language model.

        Parameters:
        - answers: A list of strings or a dictionary with string keys and values.
        - follow_examples: If True, and the prompt contains an example exactly equal to the prompt,
                           the dummy model will return the next string in the list for each request.
        If a list is provided, the dummy model will return the next string in the list for each request.
        If a dictionary is provided, the dummy model will return the value corresponding to the key that matches the prompt.
        zdummy-modeldummyN)super__init__providerr   r	   )selfr   r	   	__class__s      O/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/utils/dummies.pyr   zDummyLM.__init__   s(     	'.    returnc                 >   dg i}t        |      D ]f  }d}| j                  r{j                  d      d   }j                  d      ^}}	}
}dj                  |
      }t	        j
                  |dz   |      }|r|d   }t        d| d|       nt        d	|       |t        | j                  t              r.t        fd
| j                  j                         D        d      }n;t        | j                        dkD  r#| j                  d   }| j                  dd | _        |d}|d   j                  |dd       d\  }}}t        d       t        d       t        | | |        t        d       i |||d}| j                  j                  |       |S )z/Generates a dummy response based on the prompt.choicesN
z
---
z\s*(.*)z'DummyLM got found previous example for z with value answer=z2DummyLM couldn't find previous example for prefix=c              3   2   K   | ]  \  }}|v s|  y w)N ).0kvprompts      r   	<genexpr>z(DummyLM.basic_request.<locals>.<genexpr>2   s     "UAf1"Us   r      zNo more responseszsimulated completion)textfinish_reason)z[91mz[92mz[0mz=== DummyLM === )endz===)r   responsekwargs
raw_kwargs)ranger	   splitjoinrefindallprint
isinstancer   dictnextitemslenappendhistory)r   r   nr%   dummy_response_answerprefix_instructions_formatexamples_outputexamples_strpossible_answersREDGREENRESEThistory_entrys    `                r   basic_requestzDummyLM.basic_request   s   #Rq '	AF##d+B/=C\\)=T:w7#yy2#%::fz.A<#P # .b1FCF8K_X^W`abOyQR~dllD1!"U1C1C1E"UW[\F4<<(1,!%a'+||AB'7~, 9%,,"%; !BC#$&b!SE&%)*%LO'	V & 	
 	M*r   c                 b     | j                   |fi |}|d   }|D cg c]  }|d   	 c}S c c}w )zRetrieves dummy completions.r   r    )rC   )r   r   _only_completed_return_sortedr%   r$   r   choices           r   __call__zDummyLM.__call__T   s=    %4%%f779% .556v555s   ,c                 d    | j                   |   d   dz   | j                   |   d   d   d   d   z   S )z,Get the prompt + anwer from the ith message.r    r$   r   r   r    )r3   )r   indexs     r   	get_convozDummyLM.get_convo\   s@    ||E"8,s2T\\%5H5TU^5_`a5bci5jjjr   )F)r   )TF)__name__
__module____qualname____doc__r   liststrr.   boolr   rC   rH   rL   __classcell__)r   s   @r   r   r      sg    9/d3ic3h&? @ /SW /5d3T#s(^@T;T6U 5n6k# kr   r   r   c                       sdt         dt        fd}|S t        t        t                     dz   }t        |              dt         dt        f fd}|S )Nqueryr   c                    J d       )NzNo passages definedr   )rV   r   r%   s      r   innerzdummy_rm.<locals>.innerd   s    ///5r   d   c          	          |t              k  sJ  	| g      d   }|z  }| j                         d | }|D cg c]  }t        t        |                c}S c c}w )Nr   )	long_text)r1   argsortr   r.   )
rV   r   r%   	query_vecscoreslargest_idxipassage_vecspassages
vectorizers
          r   rX   zdummy_rm.<locals>.innerl   si    CM!!!w'*		)w'')"1->IJx{34JJJs   A)rR   intmaxmapr1   DummyVectorizer)rb   rX   
max_lengthra   rc   s   `  @@r   dummy_rmri   a   sg    	0 	0C 	0 Sh'(3.J ,Jh'LKS K K Lr   c                   H    e Zd ZdZddZd Zdee   dej                  fdZ
y)	rg   z#Simple vectorizer based on n-grams.c                     || _         || _        d| _        t        j                  d       t        |      D cg c]"  }t        j                  d| j                        $ c}| _        y c c}w )Niʚ;{   r   )rh   n_gramPrandomseedr'   	randrangecoeffs)r   rh   rm   r6   s       r   r   zDummyVectorizer.__init__z   sM    $C<A&MJqv''4662JJs   'A(c                     d}t        | j                  |      D ]%  \  }}||z  t        |      z   }|| j                  z  }' || j                  z  S )z1Hashes a string using a polynomial hash function.r   )ziprr   ordrn   rh   )r   gramhcoeffcs        r   _hashzDummyVectorizer._hash   sU    DKK. 	HE1E	CF"AKA	 4??""r   textsr   c                    g }|D ]  }t        t        |      | j                  z
  dz         D cg c]  }|||| j                  z     }}dg| j                  z  }|D ]  }|| j	                  |      xx   dz  cc<     |j                  |        t        j                  |t        j                        }|t        j                  |dd      z  }|t        j                  j                  |dd      dz   z  }|S c c}w )Nr   r   )dtypeT)axiskeepdimsg|=)r'   r1   rm   rh   rz   r2   nparrayfloat32meanlinalgnorm)r   r{   vecsr    r`   gramsvecrv   s           r   rH   zDummyVectorizer.__call__   s     	D8=c$i$++>UXY>Y8Z[1T!a$++o.[E[#'C +DJJt$%*%+KK	 xxBJJ/1t44		t!d;eCC \s   C9N)rY      )rM   rN   rO   rP   r   rz   rQ   rR   r   ndarrayrH   r   r   r   rg   rg   w   s+    -K#d3i BJJ r   rg   )r   )ro   r*   typingr   numpyr   dsp.modulesr   dsp.utils.utilsr   r   callableri   rg   r   r   r   <module>r      s>     	    #Skb SklX , r   