
    {h                         d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
  G d de      Z	 	 	 dded	e
d
edededeegef   fdZd	edeegef   fdZy)    )CallableOptional)OpenAI)Field)OpenAISchema)
Instructorc                   x    e Zd ZU dZ edd      Zeed<    edd      Ze	e
   ed<    edd	      Ze	e
   ed
<   y)	Validatorzf
    Validate if an attribute is correct and if not,
    return a new value with an error message
    Tz8Whether the attribute is valid based on the requirements)defaultdescriptionis_validNz?The error message if the attribute is not valid, otherwise NonereasonzDIf the attribute is not valid, suggest a new value for the attributefixed_value)__name__
__module____qualname____doc__r   r   bool__annotations__r   r   strr        Z/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/instructor/dsl/validators.pyr
   r
   
   sZ    
 NHd  "UFHSM  "'Z"K# r   r
   	statementclientallow_overridemodeltemperaturereturnc                 <     dt         dt         f fd}|S )a  
    Create a validator that uses the LLM to validate an attribute

    ## Usage

    ```python
    from instructor import llm_validator
    from pydantic import BaseModel, Field, field_validator

    class User(BaseModel):
        name: str = Annotated[str, llm_validator("The name must be a full name all lowercase")
        age: int = Field(description="The age of the person")

    try:
        user = User(name="Jason Liu", age=20)
    except ValidationError as e:
        print(e)
    ```

    ```
    1 validation error for User
    name
        The name is valid but not all lowercase (type=value_error.llm_validator)
    ```

    Note that there, the error message is written by the LLM, and the error type is `value_error.llm_validator`.

    Parameters:
        statement (str): The statement to validate
        model (str): The LLM to use for validation (default: "gpt-3.5-turbo-0613")
        temperature (float): The temperature to use for the LLM (default: 0)
        client (OpenAI): The OpenAI client to use (default: None)
    vr   c           	         j                   j                  j                  t        ddddd|  d dg      }|j                  sJ |j
                         r$|j                  s|j                  |j                  S | S )NsystemzYou are a world class validation model. Capable to determine if the following value is valid for the statement, if it is not, explain why and suggest a new value.)rolecontentuserzDoes `z` follow the rules: )response_modelmessagesr   r   )chatcompletionscreater
   r   r   r   )r!   respr   r   r   r   r   s     r   llmzllm_validator.<locals>.llmG   s    {{&&--$ %  D
 #!'s*>ykJ	 # . 
$ }})dkk)}$--D4D4D4P###r   r   )r   r   r   r   r   r-   s   ````` r   llm_validatorr/      s#    Rs s  4 Jr   c                 ,     dt         dt         f fd}|S )a  
    Validates a message using OpenAI moderation model.

    Should only be used for monitoring inputs and outputs of OpenAI APIs
    Other use cases are disallowed as per:
    https://platform.openai.com/docs/guides/moderation/overview

    Example:
    ```python
    from instructor import OpenAIModeration

    class Response(BaseModel):
        message: Annotated[str, AfterValidator(OpenAIModeration(openai_client=client))]

    Response(message="I hate you")
    ```

    ```
     ValidationError: 1 validation error for Response
     message
    Value error, `I hate you.` was flagged for ['harassment'] [type=value_error, input_value='I hate you.', input_type=str]
    ```

    client (OpenAI): The OpenAI client to use, must be sync (default: None)
    r!   r   c           	          j                   j                  |       }|j                  d   }|j                  j	                         |j
                  r)t        d|  ddj                  fdD                     | S )N)inputr   `z` was flagged for z, c              3   .   K   | ]  }|   s	|  y w)Nr   ).0catcatss     r   	<genexpr>zNopenai_moderation.<locals>.validate_message_with_openai_mod.<locals>.<genexpr>   s     2T3$s)32Ts   
)moderationsr+   results
categories
model_dumpflagged
ValueErrorjoin)r!   responseoutr7   r   s      @r    validate_message_with_openai_modz;openai_moderation.<locals>.validate_message_with_openai_mod   sz    %%,,1,5q!~~((*;;A3(2T$2T)T(UV  r   r.   )r   rB   s   ` r   openai_moderationrC   d   s    6	C 	C 	 ,+r   N)Fzgpt-3.5-turbor   )typingr   r   openair   pydanticr   instructor.function_callsr   instructor.clientr   r
   r   r   floatr/   rC   r   r   r   <module>rJ      s    %   2 ( . ! CCC C 	C
 C seSjCL&,f &,3%*)= &,r   