
    jw?              +          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c mZ d dlmZmZ ddlmZ d dlmZmZmZmZ ddlmZmZmZ  ej>                  e       Z! G d d	      Z"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"d
e#de#de#dejH                  dee#   dee#   dee#   dedee j                      dee j                      deee#ef      deee#ef      deee#ef      deee#      deeee#ef   ejJ                  f      dee#   deeee#f      dee   dee&   deeejN                        de"f*dZ(	 	 	 	 	 	 	 	 	 	 	 d#d e#d
e#dee#   de#de#dejH                  dee j                      deee#ef      deee#ef      deee#ef      deee#      deeee#ef   ejJ                  f      dee#   deeee#f      dee   dee&   deeejN                        ddf$d!Z)y)$    N)AnyDictListOptionalUnion)messagesstreamer   )
converters)DistributedTraceHeadersDictErrorInfoDictLLMProviderSpanType)	constantsvalidation_helpershelpersc            !          e Zd Z	 d!dedededej
                  dedee   fdZ	 	 	 	 	 	 	 	 	 	 d"d	eej                     d
ee	ee
f      dee	ee
f      dee	ee
f      deee      deee	ee
f   ej                  f      dee   deeeef      dee   dee   ddfdZ	 	 	 	 	 	 	 	 	 	 d"d	eej                     d
ee	ee
f      dee	ee
f      dee	ee
f      deee      deee	ee
f   ej                  f      dee   deeeef      dee   dee   ddfdZdddddddddddej(                  dddfdee   dee   dedeej                     d	eej                     d
ee	ee
f      dee	ee
f      dee	ee
f      deee      deee	ee
f   ej                  f      dee   dedee   dee   deeej.                        dd f dZ	 	 d#dededee   dee   ddf
dZdefd Zy)$SpanNidtrace_idproject_namemessage_streamerurl_overrideparent_span_idc                 X    || _         || _        || _        || _        || _        || _        y)z
        A Span object. This object should not be created directly, instead use the `span` method of a Trace (:func:`opik.Opik.span`) or another Span (:meth:`opik.Span.span`).
        N)r   r   r   	_streamer_project_name_url_override)selfr   r   r   r   r   r   s          z/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/api_objects/span/span_client.py__init__zSpan.__init__   s1      ,)))    end_timemetadatainputoutputtagsusagemodelprovider
error_info
total_costreturnc                 j    ||nt        j                         }| j                  |||||||||	|

       y)a  
        End the span and update its attributes.

        This method is similar to the `update` method, but it automatically computes
        the end time if not provided.

        Args:
            end_time: The end time of the span. If not provided, the current time will be used.
            metadata: Additional metadata to be associated with the span.
            input: The input data for the span.
            output: The output data for the span.
            tags: A list of tags to be associated with the span.
            usage: Usage data for the span. In order for input, output and total tokens to be visible in the UI,
                the usage must contain OpenAI-formatted keys (they can be passed additionaly to original usage on the top level of the dict):  prompt_tokens, completion_tokens and total_tokens.
                If OpenAI-formatted keys were not found, Opik will try to calculate them automatically if the usage
                format is recognized (you can see which provider's formats are recognized in opik.LLMProvider enum), but it is not guaranteed.
            model: The name of LLM.
            provider: The provider of LLM. You can find providers officially supported by Opik for cost tracking
                in `opik.LLMProvider` enum. If your provider is not here, please open an issue in our github - https://github.com/comet-ml/opik.
                If your provider not in the list, you can still specify it but the cost tracking will not be available
            error_info: The dictionary with error information (typically used when the span function has failed).
            total_cost: The cost of the span in USD. This value takes priority over the cost calculated by Opik from the usage.

        Returns:
            None
        N)
r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   )datetime_helperslocal_timestampupdater   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   s              r    endzSpan.end,   sO    P !,H2B2R2R2T 	 	!! 	 	
r"   c                     t        di d| j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d|d|d	|d
|d|d|d|d|d|	d|
 y)aA  
        Update the span attributes.

        Args:
            end_time: The end time of the span.
            metadata: Additional metadata to be associated with the span.
            input: The input data for the span.
            output: The output data for the span.
            tags: A list of tags to be associated with the span.
            usage: Usage data for the span. In order for input, output and total tokens to be visible in the UI,
                the usage must contain OpenAI-formatted keys (they can be passed additionaly to original usage on the top level of the dict):  prompt_tokens, completion_tokens and total_tokens.
                If OpenAI-formatted keys were not found, Opik will try to calculate them automatically if the usage
                format is recognized (you can see which provider's formats are recognized in opik.LLMProvider enum), but it is not guaranteed.
            model: The name of LLM.
            provider: The provider of LLM. You can find providers officially supported by Opik for cost tracking
                in `opik.LLMProvider` enum. If your provider is not here, please open an issue in our github - https://github.com/comet-ml/opik.
                If your provider not in the list, you can still specify it but the cost tracking will not be available
            error_info: The dictionary with error information (typically used when the span function has failed).
            total_cost: The cost of the span in USD. This value takes priority over the cost calculated by Opik from the usage.

        Returns:
            None
        r   r   r   r   r   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   N )update_spanr   r   r   r   r   r   r2   s              r    r1   zSpan.updated   s    H 	 	
ww	
]]	
  ..	
 ++		

 "^^	
 ++	
 	
 	
 	
 	
 	
 	
 	
 	
 "	
  "!	
r"   generalnametype
start_timeattachmentsc                     t        di d| j                  d| j                  d| j                  d| j                  d|d| j
                  d|d|d	|d
|d|d|d|d|	d|
d|d|d|d|d|S )a4  
        Create a new child span within the current span.

        Args:
            id: The ID of the span should be in UUIDv7 format. If not provided, a new ID will be generated.
            name: The name of the span.
            type: The type of the span. Defaults to "general".
            start_time: The start time of the span. If not provided, the current time will be used.
            end_time: The end time of the span.
            metadata: Additional metadata to be associated with the span.
            input: The input data for the span.
            output: The output data for the span.
            tags: A list of tags to be associated with the span.
            usage: Usage data for the span. In order for input, output and total tokens to be visible in the UI,
                the usage must contain OpenAI-formatted keys (they can be passed additionally to the original usage on the top level of the dict): prompt_tokens, completion_tokens and total_tokens.
                If OpenAI-formatted keys were not found, Opik will try to calculate them automatically if the usage
                format is recognized (you can see which provider's formats are recognized in opik.LLMProvider enum), but it is not guaranteed.
            model: The name of LLM (in this case `type` parameter should be == `llm`)
            provider: The provider of LLM. You can find providers officially supported by Opik for cost tracking
                in `opik.LLMProvider` enum. If your provider is not here, please open an issue in our GitHub - https://github.com/comet-ml/opik.
                If your provider is not in the list, you can still specify it, but the cost tracking will not be available
            error_info: The dictionary with error information (typically used when the span function has failed).
            total_cost: The cost of the span in USD. This value takes priority over the cost calculated by Opik from the usage.
            attachments: The list of attachments to be uploaded to the span.

        Returns:
            Span: The created child span object.
        r   r   r   r   span_idr   r8   r9   r:   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r;   r5   )create_spanr   r   r   r   r   )r   r   r8   r9   r:   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r;   s                   r    spanz	Span.span   s    \  
]]
++
 ++
 "^^	

 
  77
 
 
 "
 
 
 
 
 
 
  !
" #
$ "%
& "'
( $)
 	
r"   valuecategory_namereasonc                     t        j                  t        j                  | j                  ||||t        j
                  | j                        g      }| j                  j                  |       y)aH  
        Log a feedback score for the span.

        Args:
            name: The name of the feedback score.
            value: The value of the feedback score.
            category_name: The category name for the feedback score.
            reason: The reason for the feedback score.

        Returns:
            None
        )r   r8   r@   rA   rB   sourcer   )batchN)	r   !AddSpanFeedbackScoresBatchMessageFeedbackScoreMessager   r   FEEDBACK_SCORE_SOURCE_SDKr   r   put)r   r8   r@   rA   rB   add_span_feedback_batch_messages         r    log_feedback_scorezSpan.log_feedback_score   sb    & +3*T*T--ww"/!$>>!%!3!3
+
' 	:;r"   c                 4    | j                   | j                  dS )zg
        Returns headers dictionary to be passed into tracked
        function on remote node.
        )opik_parent_span_idopik_trace_id)r   r   )r   s    r    get_distributed_trace_headersz"Span.get_distributed_trace_headers  s    
 (,wwOOr"   )N)
NNNNNNNNNN)NN)__name__
__module____qualname__strr	   Streamerr   r!   datetimer   r   r   r   	llm_usage	OpikUsager   r   floatr3   r1   OPENAIr   
attachment
Attachmentr?   rK   r   rO   r5   r"   r    r   r      s#    )-** * 	*
 #++* * !** 15-1*.+/$(FJ#6:.2&*6
8,,-6
 4S>*6
 S#X'	6

 c3h(6
 tCy!6
 d38ni.A.AABC6
 }6
 5c!1236
 ]+6
 UO6
 
6
t 15-1*.+/$(FJ#6:.2&*5
8,,-5
 4S>*5
 S#X'	5

 c3h(5
 tCy!5
 d38ni.A.AABC5
 }5
 5c!1235
 ]+5
 UO5
 
5
r !""2604-1*.+/$(FJ# + 2 2.2&*=A!C
SMC
 smC
 	C

 X../C
 8,,-C
 4S>*C
 S#X'C
 c3h(C
 tCy!C
 d38ni.A.AABCC
 }C
 C
 ]+C
 UOC
  d:#8#89:!C
" 
#C
R (, $!<!< !<  }	!<
 !< 
!<FP/J Pr"   r   r   r   r   r   r=   r   r8   r9   r:   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r;   r-   c                 .   ||nt        j                         }||nt        j                         }t	        j
                  |t        |      }|t        j                  ||
      }
t        j                  di d|d| d|d|d|d|d	|d
|	d|d|d|
d|d|d|d|d|d|dt        j                         }|j                  |       |2|D ]-  }t        j                  |d|||      }|j                  |       / t        ||| |||      S )Nr(   loggerr*   r(   r$   r=   r   r   r   r8   r9   r:   r#   r%   r&   r$   r'   r(   r)   r*   r+   r,   last_updated_atr?   attachment_dataentity_type	entity_idr   r   )r   r   r   r   r   r   r5   )
id_helpersgenerate_idr/   r0   r   validate_and_parse_usageLOGGERr   add_usage_to_metadatar   CreateSpanMessagerI   attachment_convertersattachment_to_messager   )r   r   r   r   r=   r   r8   r9   r:   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r;   backend_compatible_usagecreate_span_messagerb   create_attachment_messages                           r    r>   r>     s   , !,g*2H2H2JG ,
2B2R2R2T   2JJ   +00uxP"44  " &	
         '    !" #$ )88:%( ,-*O(=(S(S /"!)))%   !:;  + %)!! r"   r   c                 >   t        j                  |t        |      }|t        j                  ||      }t        j                  | |||||||	|
|||||      }|2|D ]-  }t        j                  |d| ||      }|j                  |       / |j                  |       y )Nr]   r_   )r=   r   r   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r?   ra   )
r   rg   rh   r   ri   r   UpdateSpanMessagerk   rl   rI   )r   r   r   r   r   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r;   rm   update_span_messagerb   ro   s                        r    r6   r6   Z  s    &  2JJ   +00uxP"44%!&" *O(=(S(S /")))%   !:;  + ,-r"   )NNNr7   NNNNNNNNNNNN)NNNNNNNNNNN)*rU   loggingtypingr   r   r   r   r   opik.datetime_helpersr/   opik.id_helpersre   opik.llm_usagerV   opik.api_objects.attachmentapi_objectsrZ   opik.message_processingr   r	   r   rk   
opik.typesr   r   r   r    r   r   r   	getLoggerrP   rh   r   rS   rT   rW   rX   r[   r>   r6   r5   r"   r    <module>r~      s|     3 3 0 $ " 0 0 6 <  6 5			8	$pP pPp "$(.2,0)-&*'+ $BF26*."&9=)LLL L ''	L
 c]L SML 3-L L **+L x(()L tCH~&L DcN#L T#s(^$L 49
L E$sCx.)*=*==>?L  C=!L" u[#-./#L$ '%L& 'L( $z4456)L* 
+Ll -1)-&*'+ $BF26*."&9=#8.8.8. SM8. 	8.
 8. ''8. x(()8. tCH~&8. DcN#8. T#s(^$8. 49
8. E$sCx.)*=*==>?8. C=8. u[#-./8. '8.  !8." $z4456#8.$ 
%8.r"   