
    hD                         d Z ddlmZmZmZmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ  G d de      Zy)zW
Translate from OpenAI's `/v1/chat/completions` to Perplexity's `/v1/chat/completions`
    )AnyListOptionalTupleN)verbose_loggerget_secret_str)AllMessageValues)UsagePromptTokensDetailsWrapper)Logging)OpenAIGPTConfig)ModelResponsec                        e Zd Zedee   fd       Zdee   dee   deee   ee   f   fdZdede	fdZ
	 	 dded	ej                  d
edededee   dedededee   dee   def fdZd
ededdfdZ xZS )PerplexityChatConfigreturnc                      y)N
perplexity )selfs    g/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/perplexity/chat/transformation.pycustom_llm_providerz(PerplexityChatConfig.custom_llm_provider   s        api_baseapi_keyc                 h    |xs t        d      xs d}|xs t        d      xs t        d      }||fS )NPERPLEXITY_API_BASEzhttps://api.perplexity.aiPERPLEXITYAI_API_KEYPERPLEXITY_API_KEYr   )r   r   r   dynamic_api_keys       r   $_get_openai_compatible_provider_infoz9PerplexityChatConfig._get_openai_compatible_provider_info   sK     c~.CDcHc 445423 	
 ((r   modelc                    g d}	 t        j                  || j                        r|j                  d       	 t        j                  || j                        r|j                  d       |S # t        $ r"}t        j                  d|        Y d}~[d}~ww xY w# t        $ r#}t        j                  d|        Y d}~|S d}~ww xY w)z
        Perplexity supports a subset of OpenAI params

        Ref: https://docs.perplexity.ai/api-reference/chat-completions

        Eg. Perplexity does not support tools, tool_choice, function_call, functions, etc.
        )
frequency_penalty
max_tokensmax_completion_tokenspresence_penaltyresponse_formatstreamtemperaturetop_pmax_retriesextra_headers)r"   r   reasoning_effortz,Error checking if model supports reasoning: Nweb_search_optionsz-Error checking if model supports web search: )litellmsupports_reasoningr   append	Exceptionr   debugsupports_web_search)r   r"   base_openai_paramses       r   get_supported_openai_paramsz0PerplexityChatConfig.get_supported_openai_params"   s    
	U))1I1I #))*<=	V**1I1I #))*>? "!  	U  #OPQs!STT	U  	V  #PQRPS!TUU!!	Vs.   2A- 2B -	B6BB	C$CCNraw_responsemodel_responselogging_objrequest_datamessagesoptional_paramslitellm_paramsencoding	json_modec                     t         |   |||||||||	|
|      }	 |j                         }| j                  ||       |S # t        $ r#}t        j                  d|        Y d }~|S d }~ww xY w)N)r"   r9   r:   r;   r<   r=   r>   r?   r@   r   rA   z3Error extracting Perplexity-specific usage fields: )supertransform_responsejson%_enhance_usage_with_perplexity_fieldsr3   r   r4   )r   r"   r9   r:   r;   r<   r=   r>   r?   r@   r   rA   raw_response_jsonr7   	__class__s                 r   rD   z'PerplexityChatConfig.transform_responseI   s     3%)#%+) 4 
	\ , 1 1 366~GXY   	\  #VWXVY!Z[[	\s   "A 	A-
A((A-rG   c                 L   t        |d      r|j                  t        ddd      |_        |j                  }|j                  dg       }d}|r&t	        d |D              }|dkD  rt        d|dz        }|j                  di       }|j                  d	      }||j                  d	      }||j                  d
      }||j                  d
      }|dkD  s|N|dkD  rH|j                  t               |_        |dkD  rt        |d|       ||dkD  r||j                  _	        yyyyy)z
        Extract citation tokens and search queries from Perplexity API response
        and add them to the usage object using standard LiteLLM fields.
        usageNr   )prompt_tokenscompletion_tokenstotal_tokens	citationsc              3   J   K   | ]  }|st        t        |              y w)N)lenstr).0citations     r   	<genexpr>zMPerplexityChatConfig._enhance_usage_with_perplexity_fields.<locals>.<genexpr>   s     &`hW_s3x='9&`s   ##      num_search_queriessearch_queriescitation_tokens)
hasattrrJ   r   getsummaxprompt_tokens_detailsr   setattrweb_search_requests)	r   r:   rG   rJ   rN   rY   total_citation_charsperplexity_usagerW   s	            r   rF   z:PerplexityChatConfig._enhance_usage_with_perplexity_fieldso   sn    ~w/>3G3G3O#("#$N  $$ &))+r:	 $'&`)&`#` #a'"%a)=)B"C -00"= .112FG%!2!6!67K!L%!1!5!56F!G%!2!6!67G!H Q#5#AFX[\F\**2.H.J+ "0/B "-2Dq2HBT++? 3I- G]#Ar   )NN)__name__
__module____qualname__propertyr   rQ   r   r   r!   listr8   httpxResponser   LiteLLMLoggingObjdictr   r
   r   boolrD   rF   __classcell__)rH   s   @r   r   r      s"   Xc]  	) 	)08	)	x}hsm+	,	)%" %" %"d "&$($$ nn$ &	$
 '$ $ '($ $ $ $ #$ D>$ 
$L4U+4U@D4U	4Ur   r   )__doc__typingr   r   r   r   rh   r0   litellm._loggingr   litellm.secret_managers.mainr	   litellm.types.llms.openair
   litellm.types.utilsr   r   *litellm.litellm_core_utils.litellm_loggingr   rj   +litellm.llms.openai.chat.gpt_transformationr   r   r   r   r   r   <module>rv      s=    . -   + 7 6 A S G -QU? QUr   