
    j                         d dl Z d dlZd dlZd dlmZmZmZmZmZ d dl	m
Z
 d dlmZ ddlmZ ddlmZ ddlmZ dd	lmZ  ej(                  e      Z G d
 dej.                        Zy)    N)AnyDictOptionalUnionList)override)types   )prompt_template   )client)base_promptc                      e Zd ZdZdej
                  j                  dddddfdededee	ee
f      dej
                  ded	ee   d
ee   dee   deee      ddfdZddZeedefd              Zedefd       Zeedee   fd              Zeedefd              Zeedee	ee
f      fd              Zeedej
                  fd              Zeedee   fd              Zeedee   fd              Zeedee   fd              Zeedeee      fd              Zede
deeee	ee
f      f   fd       Zede	ee
f   fd       Zededej@                  dd fd       Z!y) Promptz]
    Prompt class represents a prompt with a name, prompt text/template and commit hash.
    NTnamepromptmetadatatypevalidate_placeholdersiddescriptionchange_descriptiontagsreturnc
                     t        j                  |||      | _        || _        || _        || _        || _        || _        || _        |	rt        j                  |	      ng | _
        | j                          y)a  
        Initializes a new instance of the class with the given parameters.
        Creates a new text prompt using the opik client and sets the initial state of the instance attributes based on the created prompt.

        Parameters:
            name: The name for the prompt.
            prompt: The template for the prompt.
            metadata: Optional metadata for the prompt.
            type: The template type (MUSTACHE or JINJA2).
            validate_placeholders: Whether to validate template placeholders.
            id: Optional unique identifier (UUID) for the prompt.
            description: Optional description of the prompt (up to 255 characters).
            change_description: Optional description of changes in this version.
            tags: Optional list of tags to associate with the prompt.

        Raises:
            PromptTemplateStructureMismatch: If a chat prompt with the same name already exists (template structure is immutable).
        )templater   r   N)r   PromptTemplate	_template_name	_metadata_type_id_description_change_descriptioncopy_tags_sync_with_backend)
selfr   r   r   r   r   r   r   r   r   s
             |/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/api_objects/prompt/text/prompt.py__init__zPrompt.__init__   sk    > )77$>S
 
!
'#5 (,TYYt_"
!    c           
          ddl m} |j                         }t        j                  |j
                        }|j                  | j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  | j                        }|j                   | _        |j$                  | _        |j(                  | _        |j(                  | _        |j,                  | _        |j.                  | _        y )Nr   )opik_client)r   r   r   r   r   r   r   r   )opik.api_objectsr-   get_client_cachedprompt_clientPromptClientrest_clientcreate_promptr   r   textr    r!   r"   r#   r$   r&   commit_commit	prompt_id__internal_api__prompt_id__r   __internal_api__version_id__r   r   )r(   r-   opik_client_prompt_client_prompt_versions        r)   r'   zPrompt._sync_with_backend@   s    0"446&33L4L4LM'55>>&&^^xx))#77 6 	
 &,,+9+C+C(,:,=,=)!$$#1#D#D #((
r+   c                     | j                   S )zThe name of the prompt.)r   r(   s    r)   r   zPrompt.nameX        zzr+   c                 ,    t        | j                        S )z"The latest template of the prompt.)strr   r>   s    r)   r   zPrompt.prompt^   s     4>>""r+   c                     | j                   S )zThe commit hash of the prompt.)r6   r>   s    r)   r5   zPrompt.commitc   s     ||r+   c                     | j                   S )z,The unique identifier of the prompt version.)r9   r>   s    r)   
version_idzPrompt.version_idi   s     000r+   c                 @    t        j                  | j                        S )z2The metadata dictionary associated with the prompt)r%   deepcopyr    r>   s    r)   r   zPrompt.metadatao   s     }}T^^,,r+   c                     | j                   S )zThe prompt type of the prompt.)r!   r>   s    r)   r   zPrompt.typeu   r?   r+   c                     | j                   S )z+The unique identifier (UUID) of the prompt.)r"   r>   s    r)   r   z	Prompt.id{   s     xxr+   c                     | j                   S )zThe description of the prompt.)r#   r>   s    r)   r   zPrompt.description   s        r+   c                     | j                   S )z+The description of changes in this version.)r$   r>   s    r)   r   zPrompt.change_description   s     '''r+   c                 \    | j                   rt        j                  | j                         S g S )z,The list of tags associated with the prompt.)r&   r%   r>   s    r)   r   zPrompt.tags   s"     )-

tyy$::r+   kwargsc                 r   | j                   duxr> | j                   j                  d      dk(  xr | j                   j                  d      dk(  } | j                  j                  di |}|r	 t	        j
                  |      S |S # t        j                  $ r t        j                  d| d       |cY S w xY w)	a  
        Replaces placeholders in the template with provided keyword arguments.

        Args:
            **kwargs: Arbitrary keyword arguments where the key represents the placeholder
                      in the template and the value is the value to replace the placeholder with.

        Returns:
            A string with all placeholders replaced by their corresponding values from kwargs.
        Ncreated_fromopik_uir   messages_jsonzFailed to parse JSON string: z@. Make sure chat prompt is valid JSON. Returning the raw string. )	r    getr   formatjsonloadsJSONDecodeErrorLOGGERerror)r(   rL   is_playground_chat_promptformatted_strings       r)   rS   zPrompt.format   s     NN$& >"">2i?>""6*o= 	"
 14>>00:6:$(zz"233   '' (34D3E  FF  G ('	(s   .B .B65B6c                 (   | j                   dd| j                  id}| j                  | j                  |d<   | j                  | j                  |d   d<   | j                  | j                  |d   d<   | j
                  | j
                  |d   d<   |S )z
        Convert the prompt to an info dictionary for serialization.

        Returns:
            Dictionary containing prompt metadata and version information.
        r4   r   )r   template_structureversionr   r]   r5   r   )r   r   r8   r5   r9   r    )r(   	info_dicts     r)   __internal_api__to_info_dict__z%Prompt.__internal_api__to_info_dict__   s     II"(DKK%
	 ++7">>IdO;;"-1[[Ii *,,8)-)J)JIi &>>%/3~~Ii ,r+   r<   c                 b   | j                  |       }|j                  |_        |j                  |_        ||_        t        j                  |j                  t        j                  |j                        xs t        j                  j                        |_        |j                  |_        |j                   |_        |j                  |_        |j                  |_        d |_        |j*                  |_        |j.                  r&t1        j0                  |j.                        |_        |S g |_        |S )N)r   r   )__new__r   r9   r7   r8   r   r   r   r   prompt_types
PromptTyper   MUSTACHEr   r5   r6   r   r    r!   r"   r#   r   r$   r   r%   r&   )clsr   r<   r   s       r)   from_fern_prompt_versionzPrompt.from_fern_prompt_version   s     S!.<.?.?+-;-E-E**99#,,(()<)<= 0&&//

 (..)22%**#&&
 	 &4%F%F"9G9L9Ltyy!4!45 SUr+   )r   N)"__name__
__module____qualname____doc__rb   rc   rd   rA   r   r   r   boolr   r*   r'   propertyr   r   r   r5   rD   r   r   r   r   r   r   r   rS   r_   classmethodrest_api_typesPromptVersionDetailrf   rQ   r+   r)   r   r      s    .2(4(?(?(H(H&* %),0$(*"*" *" 4S>*	*"
 %%*"  $*" SM*" c]*" %SM*" tCy!*" 
*"X)0 c    # # #     1C 1  1 -(4S>2 -  - l--    HSM    !Xc] !  ! (HSM (  ( ;htCy) ;  ;  s  uS$tCH~2F-F'G    : S#X  :  ':: 
	 r+   r   )r%   rT   loggingtypingr   r   r   r   r   typing_extensionsr   opik.rest_apir	   rn    r   rb   r   r0   r   	getLoggerrg   rW   
BasePromptr   rQ   r+   r)   <module>rw      sL       3 3 & 1  $ & 			8	$[[## [r+   