HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/langchain/output_parsers/__pycache__/retry.cpython-310.pyc
o

���gI)�@s�ddlmZddlmZmZmZddlmZddlm	Z	ddl
mZmZddl
mZddlmZmZddlmZdd	lmZdd
lmZmZdZdZe�e�Ze�e�Zed
�ZGdd�de�ZGdd�de�Z Gdd�dee�Z!Gdd�dee�Z"dS)�)�annotations)�Any�TypeVar�Union)�OutputParserException)�BaseLanguageModel)�BaseOutputParser�StrOutputParser)�PromptValue)�BasePromptTemplate�PromptTemplate)�RunnableSerializable)�SkipValidation)�	Annotated�	TypedDictz�Prompt:
{prompt}
Completion:
{completion}

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

Above, the Completion did not satisfy the constraints given in the Prompt.
Details: {error}
Please try again:�Tc@seZdZUded<ded<dS)� RetryOutputParserRetryChainInput�str�prompt�
completionN��__name__�
__module__�__qualname__�__annotations__�rr�I/usr/local/lib/python3.10/dist-packages/langchain/output_parsers/retry.pyr's
rc@s&eZdZUded<ded<ded<dS)�)RetryWithErrorOutputParserRetryChainInputrrr�errorNrrrrrr,s
rc@�eZdZUdZded<	ded<	dZded<	d	Zd
ed<	eedfd(dd��Z	d)dd�Z
d)dd�Zd*dd�Zd+d d!�Z
ed+d"d#��Zed,d%d&��Zd'S)-�RetryOutputParserz�Wrap 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]rr�return�RetryOutputParser[T]cC�||Bt�B}||||d�S)aFCreate 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"rr&�chainrrr�from_llmF�zRetryOutputParser.from_llmrr�prompt_valuer
rc
Cs�d}||jkrQz|j�|�WStyK}z1||jkr|�|d7}|jr5t|jd�r5|jj|��|d�}n|j�	t
|��|d��}WYd}~nd}~ww||jkstd��)�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.
        rr$�run�rrN�Failed to parse)r&r"�parserr(�hasattrr#r7�	to_string�invoke�dict��selfrr5�retries�errr�parse_with_prompt\s.


�����
�z#RetryOutputParser.parse_with_promptc
�s��d}||jkr^z
|j�|�IdHWStyX}z:||jkr!|�|d7}|jr?t|jd�r?|jj|��|t	|�d�IdH}n|j�
t|��|d��IdH}WYd}~nd}~ww||jkstd��)r6rNr$�arun�rrrr8r9�r&r"�aparserr(r;r#rDr<�repr�ainvoker>r?rrr�aparse_with_prompts2�


��
���
�z$RetryOutputParser.aparse_with_promptcC�td���NzGThis OutputParser can only be called by the `parse_with_prompt` method.��NotImplementedError�r@rrrrr:���zRetryOutputParser.parsecC�
|j��S�N�r"�get_format_instructions�r@rrrrT��
z)RetryOutputParser.get_format_instructionscC�dS)N�retryrrUrrr�_type��zRetryOutputParser._type�type[T]cC�|jjSrR�r"�
OutputTyperUrrrr^��zRetryOutputParser.OutputTypeN)
r)rr"r*rrr&r%r+r,�rrr5r
r+r�rrr+r�r+r�r+r[)rrr�__doc__rr&r(�classmethod�NAIVE_RETRY_PROMPTr3rCrJr:rT�propertyrYr^rrrrr 2s,
�

#
$
r c@r)-�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)rr*rrr+�RetryWithErrorOutputParser[T]cCr-)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.
        r.r/r0rrrr3�r4z#RetryWithErrorOutputParser.from_llmrrr5r
rc
Cs�d}||jkrWz|j�|�WStyQ}z7||jkr|�|d7}|jr8t|jd�r8|jj|��|t	|�d�}n|j�
t||��t	|�d��}WYd}~nd}~ww||jkstd��)Nrr$r7rE)rrrr9)r&r"r:rr(r;r#r7r<rHr=r>r?rrrrC�s2

�����
�z,RetryWithErrorOutputParser.parse_with_promptc
�s��d}||jkraz
|j�|�IdHWSty[}z=||jkr!|�|d7}|jr?t|jd�r?|jj|��|t	|�d�IdH}n|j�
t|��|t	|�d��IdH}WYd}~nd}~ww||jkstd��)Nrr$rDrEr9rFr?rrrrJ�s4�

��
���
�z-RetryWithErrorOutputParser.aparse_with_promptcCrKrLrMrOrrrr:rPz RetryWithErrorOutputParser.parsecCrQrRrSrUrrrrT rVz2RetryWithErrorOutputParser.get_format_instructionscCrW)N�retry_with_errorrrUrrrrY#rZz RetryWithErrorOutputParser._typer[cCr\rRr]rUrrrr^'r_z%RetryWithErrorOutputParser.OutputTypeN)
r)rr"r*rrr&r%r+rir`rarbrc)rrrrdrr&r(re�NAIVE_RETRY_WITH_ERROR_PROMPTr3rCrJr:rTrgrYr^rrrrrh�s,
	�



rhN)#�
__future__r�typingrrr�langchain_core.exceptionsr�langchain_core.language_modelsr�langchain_core.output_parsersrr	�langchain_core.prompt_valuesr
�langchain_core.promptsrr�langchain_core.runnablesr
�pydanticr�typing_extensionsrr�NAIVE_COMPLETION_RETRY�!NAIVE_COMPLETION_RETRY_WITH_ERROR�
from_templaterfrkrrrr rhrrrr�<module>s,
	�