
    @h                        S r SSKJr  SSKrSSKJrJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  \R6                  " \5      r " S S\\
5      rg)zDHypothetical Document Embeddings.

https://arxiv.org/abs/2212.10496
    )annotationsN)AnyOptional)CallbackManagerForChainRun)
Embeddings)BaseLanguageModel)StrOutputParser)BasePromptTemplate)Runnable)
ConfigDict)Chain)
PROMPT_MAP)LLMChainc                      \ rS rSr% SrS\S'   S\S'   \" SSS	9r\SS
 j5       r	\SS j5       r
SS jrSS jrSS jr S     SS jjr\  S           SS jj5       r\SS j5       rSrg)HypotheticalDocumentEmbedder   zjGenerate hypothetical document for query, and then embed that.

Based on https://arxiv.org/abs/2212.10496
r   base_embeddingsr   	llm_chainTforbid)arbitrary_types_allowedextrac                P    U R                   R                  R                  5       S   $ )z Input keys for Hyde's LLM chain.required)r   input_schemamodel_json_schemaselfs    R/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/chains/hyde/base.py
input_keys'HypotheticalDocumentEmbedder.input_keys(   s"     ~~**<<>zJJ    c                r    [        U R                  [        5      (       a  U R                  R                  $ S/$ )z!Output keys for Hyde's LLM chain.text)
isinstancer   r   output_keysr   s    r   r%   (HypotheticalDocumentEmbedder.output_keys-   s,     dnnh//>>---xr!   c                8    U R                   R                  U5      $ )zCall the base embeddings.)r   embed_documents)r   textss     r   r(   ,HypotheticalDocumentEmbedder.embed_documents4   s    ##33E::r!   c                $    SSK n[        UR                  U5      R                  SS95      $ ! [         aX    [
        R                  S5        U(       d  / s $ [        U5      n[        U6  Vs/ sH  n[        U5      U-  PM     Os  snf sns $ f = f)z)Combine embeddings into final embeddings.r   N)axisa*  NumPy not found in the current Python environment. HypotheticalDocumentEmbedder will use a pure Python implementation for internal calculations, which may significantly impact performance, especially for large datasets. For optimal speed and efficiency, consider installing NumPy: pip install numpy)
numpylistarraymeanImportErrorloggerwarninglenzipsum)r   
embeddingsnpnum_vectors
dim_valuess        r   combine_embeddings/HypotheticalDocumentEmbedder.combine_embeddings8   s    	V,11q19:: 	VNNK 	j/KDGDTUDTjC
Ok1DTUU	Vs!   *- (BB,B	BBc                   U R                   S   nU R                  R                  X!05      n[        U R                  [        5      (       a  X0R
                  S      /nOU/nU R                  U5      nU R                  U5      $ )z1Generate a hypothetical document and embedded it.r   )r   r   invoker$   r   r%   r(   r;   )r   r#   var_nameresult	documentsr7   s         r   embed_query(HypotheticalDocumentEmbedder.embed_queryK   sv    ??1%&&'78dnnh// 0 0 345II)))4
&&z22r!   Nc                    U=(       d    [         R                  " 5       nU R                  R                  USUR	                  5       0S9$ )zCall the internal llm chain.	callbacks)config)r   get_noop_managerr   r>   	get_child)r   inputsrun_manager_run_managers       r   _call"HypotheticalDocumentEmbedder._callV   sH     #S&@&Q&Q&S~~$$!7!7!9: % 
 	
r!   c                    Ub  UnODUb  U[         ;   a
  [         U   nO-S[        [         R                  " 5       5       S3n[        U5      eXa-  [	        5       -  nU " SX(S.UD6$ )zILoad and use LLMChain with either a specific prompt key or custom prompt.zHMust specify prompt_key if custom_prompt not provided. Should be one of .)r   r    )r   r.   keys
ValueErrorr	   )	clsllmr   
prompt_keycustom_promptkwargspromptmsgr   s	            r   from_llm%HypotheticalDocumentEmbedder.from_llmb   sw     $"F#
j(@
+F:??,-.a1  S/!L?#44	R?R6RRr!   c                    g)N
hyde_chainrP   r   s    r   _chain_type(HypotheticalDocumentEmbedder._chain_typez   s    r!   rP   )return	list[str])r)   ra   r`   list[list[float]])r7   rb   r`   list[float])r#   strr`   rc   )N)rI   zdict[str, Any]rJ   z$Optional[CallbackManagerForChainRun]r`   zdict[str, str])NN)rT   r   r   r   rU   zOptional[str]rV   zOptional[BasePromptTemplate]rW   r   r`   r   )r`   rd   )__name__
__module____qualname____firstlineno____doc____annotations__r   model_configpropertyr   r%   r(   r;   rB   rL   classmethodrZ   r^   __static_attributes__rP   r!   r   r   r      s    
   $L
 K K  ;V&	3 =A



 :

 
	

 
 %)6:SS $S "	S
 4S S 
&S S.  r!   r   )ri   
__future__r   loggingtypingr   r   langchain_core.callbacksr   langchain_core.embeddingsr   langchain_core.language_modelsr   langchain_core.output_parsersr	   langchain_core.promptsr
   langchain_core.runnablesr   pydanticr   langchain.chains.baser   langchain.chains.hyde.promptsr   langchain.chains.llmr   	getLoggerre   r2   r   rP   r!   r   <module>r}      sS   
 #    ? 0 < 9 5 -  ' 4 )			8	$b5* br!   