
    NgI)                    x   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZ dZdZ ej        e          Z ej        e          Z ed          Z G d de          Z G d de          Z  G d dee                   Z! G d dee                   Z"dS )    )annotations)AnyTypeVarUnion)OutputParserException)BaseLanguageModel)BaseOutputParserStrOutputParser)PromptValue)BasePromptTemplatePromptTemplate)RunnableSerializable)SkipValidation)	Annotated	TypedDictzPrompt:
{prompt}
Completion:
{completion}

Above, the Completion did not satisfy the constraints given in the Prompt.
Please try again:zPrompt:
{prompt}
Completion:
{completion}

Above, the Completion did not satisfy the constraints given in the Prompt.
Details: {error}
Please try again:Tc                  $    e Zd ZU ded<   ded<   dS ) RetryOutputParserRetryChainInputstrprompt
completionN__name__
__module____qualname____annotations__     Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/output_parsers/retry.pyr   r   '   s"         KKKOOOOOr   r   c                  .    e Zd ZU ded<   ded<   ded<   dS ))RetryWithErrorOutputParserRetryChainInputr   r   r   errorNr   r   r   r   r!   r!   ,   s+         KKKOOOJJJJJr   r!   c                      e Zd ZU dZded<   	 ded<   	 dZded<   	 d	Zd
ed<   	 eedfd!d            Z	d"dZ
d"dZd#dZd$dZed$d            Zed%d            Zd S )&RetryOutputParserzWrap a parser and try to fix parsing errors.

    Does this by passing the original prompt and the completion to another
    LLM, and telling it the completion did not satisfy criteria in the prompt.
    0Annotated[BaseOutputParser[T], SkipValidation()]parserzdAnnotated[Union[RunnableSerializable[RetryOutputParserRetryChainInput, str], Any], SkipValidation()]retry_chain   intmax_retriesTboollegacyllmr   BaseOutputParser[T]r   r   returnRetryOutputParser[T]c                F    ||z  t                      z  } | |||          S )aF  Create an RetryOutputParser from a language model and a parser.

        Args:
            llm: llm to use for fixing
            parser: parser to use for parsing
            prompt: prompt to use for fixing
            max_retries: Maximum number of retries to parse.

        Returns:
            RetryOutputParser
        r&   r'   r*   r
   clsr-   r&   r   r*   chains         r   from_llmzRetryOutputParser.from_llmF   /    & 000s&eMMMMr   r   r   prompt_valuer   r   c                   d}|| j         k    r	 | j                            |          S # t          $ r}|| j         k    r||dz  }| j        rDt          | j        d          r/| j                            |                                |          }n;| j        	                    t          |                                |                    }Y d}~nd}~ww xY w|| j         k    t          d          )   Parse the output of an LLM call using a wrapped parser.

        Args:
            completion: The chain completion to parse.
            prompt_value: The prompt to use to parse the completion.

        Returns:
            The parsed completion.
        r   r(   runr   r   NFailed to parse)r*   r&   parser   r,   hasattrr'   r<   	to_stringinvokedictselfr   r9   retrieses        r   parse_with_promptz#RetryOutputParser.parse_with_prompt\   s     ))){((444(   d...GqLG{ wt/?'G'G %)%5%9%9#/#9#9#;#;'1 &: & &


 &*%5%<%< '3'='='?'?+5  & &
 )))* $$5666s   ) 
CBCCc                (  K   d}|| j         k    r	 | j                            |           d{V S # t          $ r}|| j         k    r||dz  }| j        rXt          | j        d          rC| j                            |                                |t          |                     d{V }nA| j        
                    t          |                                |                     d{V }Y d}~nd}~ww xY w|| j         k    t          d          )r;   r   Nr(   arunr   r   r"   r=   r>   r*   r&   aparser   r,   r@   r'   rJ   rA   reprainvokerC   rD   s        r   aparse_with_promptz$RetryOutputParser.aparse_with_prompt   s|      )))![//
;;;;;;;;;(   d...GqLG{ wt/?'H'H +/+;+@+@#/#9#9#;#;'1"&q'' ,A , , & & & & & &

 ,0+;+C+C '3'='='?'?+5  , , & & & & & &
 ))), $$5666s   1 
C7B2C22C7c                     t          d          NzGThis OutputParser can only be called by the `parse_with_prompt` method.NotImplementedErrorrE   r   s     r   r?   zRetryOutputParser.parse       !U
 
 	
r   c                4    | j                                         S Nr&   get_format_instructionsrE   s    r   rZ   z)RetryOutputParser.get_format_instructions       {22444r   c                    dS )Nretryr   r[   s    r   _typezRetryOutputParser._type   s    wr   type[T]c                    | j         j        S rX   r&   
OutputTyper[   s    r   rc   zRetryOutputParser.OutputType       {%%r   N)
r-   r   r&   r.   r   r   r*   r)   r/   r0   r   r   r9   r   r/   r   r   r   r/   r   r/   r   r/   r`   )r   r   r   __doc__r   r*   r,   classmethodNAIVE_RETRY_PROMPTr7   rH   rP   r?   rZ   propertyr_   rc   r   r   r   r$   r$   2   s5          =<<<0    VK9FC
 &8N N N N [N*!7 !7 !7 !7F"7 "7 "7 "7H
 
 
 

5 5 5 5    X & & & X& & &r   r$   c                      e Zd ZU dZded<   	 ded<   	 dZded<   	 d	Zd
ed<   	 eedfd!d            Z	d"dZ
d"dZd#dZd$dZed$d            Zed%d            Zd S )&RetryWithErrorOutputParsera  Wrap a parser and try to fix parsing errors.

    Does this by passing the original prompt, the completion, AND the error
    that was raised to another language model and telling it that the completion
    did not work, and raised the given error. Differs from RetryOutputParser
    in that this implementation provides the error that was raised back to the
    LLM, which in theory should give it more information on how to fix it.
    r%   r&   zmAnnotated[Union[RunnableSerializable[RetryWithErrorOutputParserRetryChainInput, str], Any], SkipValidation()]r'   r(   r)   r*   Tr+   r,   r-   r   r.   r   r   r/   RetryWithErrorOutputParser[T]c                F    ||z  t                      z  } | |||          S )a  Create a RetryWithErrorOutputParser from an LLM.

        Args:
            llm: The LLM to use to retry the completion.
            parser: The parser to use to parse the output.
            prompt: The prompt to use to retry the completion.
            max_retries: The maximum number of times to retry the completion.

        Returns:
            A RetryWithErrorOutputParser.
        r2   r3   r4   s         r   r7   z#RetryWithErrorOutputParser.from_llm   r8   r   r   r   r9   r   r   c           
        d}|| j         k    r	 | j                            |          S # t          $ r}|| j         k    r||dz  }| j        rRt          | j        d          r=| j                            |                                |t          |                    }nI| j        
                    t          ||                                t          |                              }Y d }~nd }~ww xY w|| j         k    t          d          )Nr   r(   r<   rK   )r   r   r"   r>   )r*   r&   r?   r   r,   r@   r'   r<   rA   rN   rB   rC   rD   s        r   rH   z,RetryWithErrorOutputParser.parse_with_prompt   s0   ))){((444(   d...GqLG{ wt/?'G'G %)%5%9%9#/#9#9#;#;'1"&q'' &: & &

 &*%5%<%< +5'3'='='?'?&*1gg  & &
 ))). $$5666s   ) 
C1B4C,,C1c           
     H  K   d}|| j         k    r	 | j                            |           d {V S # t          $ r}|| j         k    r||dz  }| j        rXt          | j        d          rC| j                            |                                |t          |                     d {V }nO| j        
                    t          |                                |t          |                               d {V }Y d }~nd }~ww xY w|| j         k    t          d          )Nr   r(   rJ   rK   r>   rL   rD   s        r   rP   z-RetryWithErrorOutputParser.aparse_with_prompt   s     )))![//
;;;;;;;;;(   d...GqLG{ wt/?'H'H +/+;+@+@#/#9#9#;#;'1"&q'' ,A , , & & & & & &

 ,0+;+C+C '3'='='?'?+5&*1gg  , , & & & & & &
 ))). $$5666s   2 
DC DDc                     t          d          rR   rS   rU   s     r   r?   z RetryWithErrorOutputParser.parse  rV   r   c                4    | j                                         S rX   rY   r[   s    r   rZ   z2RetryWithErrorOutputParser.get_format_instructions   r\   r   c                    dS )Nretry_with_errorr   r[   s    r   r_   z RetryWithErrorOutputParser._type#  s    !!r   r`   c                    | j         j        S rX   rb   r[   s    r   rc   z%RetryWithErrorOutputParser.OutputType'  rd   r   N)
r-   r   r&   r.   r   r   r*   r)   r/   ro   re   rf   rg   rh   )r   r   r   ri   r   r*   r,   rj   NAIVE_RETRY_WITH_ERROR_PROMPTr7   rH   rP   r?   rZ   rl   r_   rc   r   r   r   rn   rn      s4          =<<<0    VK9FC
 &CN N N N [N*7 7 7 787 7 7 78
 
 
 

5 5 5 5 " " " X" & & & X& & &r   rn   N)#
__future__r   typingr   r   r   langchain_core.exceptionsr   langchain_core.language_modelsr   langchain_core.output_parsersr	   r
   langchain_core.prompt_valuesr   langchain_core.promptsr   r   langchain_core.runnablesr   pydanticr   typing_extensionsr   r   NAIVE_COMPLETION_RETRY!NAIVE_COMPLETION_RETRY_WITH_ERRORfrom_templaterk   rx   r   r   r!   r$   rn   r   r   r   <module>r      s   " " " " " " & & & & & & & & & & ; ; ; ; ; ; < < < < < < K K K K K K K K 4 4 4 4 4 4 E E E E E E E E 9 9 9 9 9 9 # # # # # # 2 2 2 2 2 2 2 2 % ! 2^12HII  < <%! !  GCLL    y   
    	   & & & & &(+ & & &Du& u& u& u& u&!1!!4 u& u& u& u& u&r   