o
    sg                     @   sx   d Z ddlZddlZddlZddlmZ e  G dd dZedkr:dgZe Z	e	
eZee ee	e dS dS )zModule to call perplexity api    N)load_dotenvc                   @   sd   e Zd ZdZedZdZdZ			dde	d	e
d
ededef
ddZdd Zdd Zedd ZdS )PerplexityResearchzclass to call perplexity apiPERPLEXITY_KEYz*https://api.perplexity.ai/chat/completionsz	\[(\d+)\]333333?Tsonarmessagestemperaturereturn_citationsmodelreturnc                 C   sD   |||  ||d}d| j dd}tj| j||dd}| |S )a/  
        Asks Perplexity AI a question with a given set of messages and returns a response.

        Args:
            messages (list): A list of strings with the messages to send to the AI.
            temperature (float, optional): The temperature of the response. Defaults to 0.3.
            return_citations (bool, optional): Whether to return the citations. Defaults to True.
            model (str, optional): The model to use. Defaults to "sonar".

        Returns:
            dict: A dictionary with the answer and optionally the citations.
        )r	   r
   r   r   zBearer zapplication/json)AuthorizationzContent-Type<   )jsonheaderstimeout)convert_messagesapi_keyrequestsposturlextract_answer_and_citations)selfr   r   r	   r
   payloadr   response r   7/var/www/html/XCapMarket/utils/researcher/perplexity.pyask_perplexity   s   

z!PerplexityResearch.ask_perplexityc                 C   sj   g }|D ].}|d dkr| d|d d q|d dkr(| d|d d q| d|d d q|S )zconvert the messages to jsonr   system   )rolecontenthumanuser	assistant)append)r   r   perplexity_messagesmessager   r   r   r   5   s   z#PerplexityResearch.convert_messagesc                 C   s   |j dkrdS | d d d d }t| j|}t| jd|}| d }g }|D ]}t|}|||d	   q,||d
S )zBfrom the response of the request, extract the answer and citations   Nchoicesr   r&   r     	citationsr   )answerr*   )status_coder   refindallcitation_patternsubintr$   )r   r   r+   matchesr*   citations_to_returnmatchnumberr   r   r   r   B   s   

z/PerplexityResearch.extract_answer_and_citationsc                 C   s   d| d  d| d  S )zpretty print the responsezAnswer: r+   z, Citations: r*   r   )r   r   r   r   pretty_printW   s   zPerplexityResearch.pretty_printN)r   Tr   )__name__
__module____qualname____doc__osgetenvr   r   r/   listfloatboolstrdictr   r   r   staticmethodr6   r   r   r   r   r      s.    

#r   __main__)r   zYou are a fan of F1.)r:   r;   r-   r   dotenvr   r   r7   messages_perplexity
perplexityr   per_responseprintr6   r   r   r   r   <module>   s    Q
