o
    g                     @   s  d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z deeef deeef fddZd	edeeef fd
dZd	efddZdd Zdd Zdeeef d	efddZdee ded	edee deeeef  dedeeeef  dededef dedefddZdee ded	edee deeeef  dedeeeef  dededef dedefd d!Zded"ed#efd$d%ZdS )&    N)AnyCallableDictOptional)URL)Clientkwargsreturnc                 C   s2   i }dD ]}|| v r| | dur| | ||< q|S )z?
    Extracts model parameters from the kwargs dictionary.
    )
temperature
max_tokensmax_completion_tokenstop_pfrequency_penaltypresence_penaltynstopstream	streamingN )r   model_paramsparamr   r   G/var/www/visachat/venv/lib/python3.10/site-packages/posthog/ai/utils.pyget_model_params
   s   r   providerc                 C   s>   |dkr| j j| j jdS |dkr| j j| j jdS dddS )N	anthropic)input_tokensoutput_tokensopenair   )usager   r   prompt_tokenscompletion_tokens)responser   r   r   r   	get_usage    s   r"   c                 C   s4   g }| du r|S |dkrt | S |dkrt| S |S )z4
    Format a regular (non-streaming) response.
    Nr   r   )format_response_anthropicformat_response_openai)r!   r   outputr   r   r   format_response1   s   r&   c                 C   s,   g }| j D ]}|jr|d|jd q|S )N	assistantrolecontent)r*   textappendr!   r%   choicer   r   r   r#   ?   s   
r#   c                 C   s4   g }| j D ]}|jjr||jj|jjd q|S )N)r*   r)   )choicesmessager*   r,   r)   r-   r   r   r   r$   L   s   
r$   c                 C   sH   |dkr	|  dS |  dpg }|  dd u r|S d|  ddg| S )Nr   messagessystemr(   )get)r   r   r1   r   r   r   merge_system_promptY   s   
r4   posthog_distinct_id	ph_clientposthog_trace_idposthog_propertiesposthog_privacy_modeposthog_groupsbase_urlcall_method.c	                 K   sL  t   }
d}d}d}i }i }zz	|di |	}W n! ty9 } z|}t|dd}d| d}W Y d}~nd}~ww W t   }||
 }|du rKt }|rWt|drWt||}t|	|}||		dt
|	t|||t||t||||	d	d|	d
d||t|d|pi |pi }| du rd|d< t|drt|jr|j| p|d||d nut   }||
 }|du rt }|rt|drt||}t|	|}||		dt
|	t|||t||t||||	d	d|	d
d||t|d|pi |pi }| du rd|d< t|drt|jr|j| p|d||d w w w |r$||S )z
    Common usage-tracking logic for both sync and async calls.
    call_method: the llm call method (e.g. openai.chat.completions.create)
    N   status_coder   Tz$ai_is_errorz	$ai_errorr   modelr   r   z$ai_providerz	$ai_modelz$ai_model_parametersz	$ai_inputz$ai_output_choicesz$ai_http_statusz$ai_input_tokensz$ai_output_tokensz$ai_latencyz$ai_trace_idz$ai_base_urlF$process_person_profilecapture$ai_generationdistinct_idevent
propertiesgroupsr   time	Exceptiongetattr__str__uuiduuid4hasattrr"   r4   r3   r   with_privacy_moder&   strcallablerC   )r5   r6   r   r7   r8   r9   r:   r;   r<   r   
start_timer!   errorhttp_statusr   error_paramsexcend_timelatencyr1   event_propertiesr   r   r   call_llm_and_track_usageb   s   










r]   call_async_methodc	                    sT  t   }
d }d }d}i }i }zz|di |	I d H }W n! ty= } z|}t|dd}d| d}W Y d }~nd }~ww W t   }||
 }|d u rOt }|r[t|dr[t||}t|	|}||		dt
|	t|||t||t||||	dd|	d	d||t|d
|pi |pi }| d u rd|d< t|drt|jr|j| p|d||d nut   }||
 }|d u rt }|rt|drt||}t|	|}||		dt
|	t|||t||t||||	dd|	d	d||t|d
|pi |pi }| d u rd|d< t|dr!t|jr"|j| p|d||d w w w |r(||S )Nr=   r>   r   Tr?   r   r@   r   r   rA   FrB   rC   rD   rE   r   rJ   )r5   r6   r   r7   r8   r9   r:   r;   r^   r   rU   r!   rV   rW   r   rX   rY   rZ   r[   r1   r\   r   r   r   call_llm_and_track_usage_async   s   










r_   privacy_modevaluec                 C   s   | j s|rd S |S )N)r`   )r6   r`   ra   r   r   r   rR      s   
rR   )rK   rO   typingr   r   r   r   httpxr   posthog.clientr   PostHogClientrS   r   r"   r&   r#   r$   r4   boolr]   r_   rR   r   r   r   r   <module>   st    "	
	

P
	

L