
    6i{                         d dl mZ d dlmZ ddlmZ ddlm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)OpenAI   )	Validator)
Instructor	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-4o-mini")
        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   is_validreasonfixed_value)r   respr
   r	   r   r   r   s     o/root/.openclaw/workspace/visionaryfx/venv/lib/python3.12/site-packages/instructor/validation/llm_validators.pyllmzllm_validator.<locals>.llm2   s    {{&&--$ %  D
 #!'s*>ykJ	 # . 
$ }})dkk)}$--D4D4D4P###    str)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)N ).0catcatss     r   	<genexpr>zNopenai_moderation.<locals>.validate_message_with_openai_mod.<locals>.<genexpr>p   s     2T3$s)32Ts   
)moderationsr   results
categories
model_dumpflagged
ValueErrorjoin)r   responseoutr,   r	   s      @r    validate_message_with_openai_modz;openai_moderation.<locals>.validate_message_with_openai_modj   sz    %%,,1,5q!~~((*;;A3(2T$2T)T(UV  r    r!   )r	   r7   s   ` r   openai_moderationr8   O   s    6	C 	C 	 ,+r    N)Fzgpt-3.5-turbor   )typingr   openair   processing.validatorsr   core.clientr   r"   boolfloatr#   r8   r)   r    r   <module>r?      s      - $ ! CCC C 	C
 C seSjCL&,f &,3%*)= &,r    