Ë
    tPêjn  ã                   óP   — d dl Z d dlmZ d dlmZmZ d dlZddlmZ  G d„ d«      Z	y)é    N)ÚIterator)ÚAnyÚFinalé   )ÚUnauthorizedErrorc                   ól  — e Zd Zddededz  defd„Zdeeef   fd„Z	 	 	 	 	 	 	 	 dded	eeeef      d
e	dz  de	dz  dedz  dedz  de	dz  de	dz  dedz  de
deeef   ej                  z  fd„Z	 	 	 	 	 	 	 dded	eeeef      d
e	dz  de	dz  dedz  dedz  de	dz  de	dz  dedz  deeeef      fd„Zy)Ú
ChatClientNÚbase_urlÚapi_keyÚtimeoutc                 óL   — |j                  d«      | _        || _        || _        y)a°  
        Initialize the ChatClient.

        Args:
            base_url (str): The base URL of the LiteLLM proxy server (e.g., "http://localhost:8000")
            api_key (Optional[str]): API key for authentication. If provided, it will be sent as a Bearer token.
            timeout (int): Request timeout in seconds (default: 600, the OpenAI SDK default, since a completion
                can legitimately take minutes)
        ú/N)ÚrstripÚ	_base_urlÚ_api_keyÚ_timeout)Úselfr
   r   r   s       ú^/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/client/chat.pyÚ__init__zChatClient.__init__   s"   € ð "Ÿ™¨Ó-ˆŒØˆŒØˆ�ó    Úreturnc                 óJ   — ddi}| j                   rd| j                   › �|d<   |S )z¬
        Get the headers for API requests, including authorization if api_key is set.

        Returns:
            Dict[str, str]: Headers to use for API requests
        zContent-Typezapplication/jsonzBearer ÚAuthorization)r   )r   Úheaderss     r   Ú_get_headerszChatClient._get_headers   s0   € ð )Ð*<Ð=ˆØ�=Š=Ø)0°·±°Ð'@ˆG�OÑ$Øˆr   ÚmodelÚmessagesÚtemperatureÚtop_pÚnÚ
max_tokensÚpresence_penaltyÚfrequency_penaltyÚuserÚreturn_requestc                 ó6  — | j                   › d�}||dœ}|�||d<   |�||d<   |�||d<   |�||d<   |�||d<   |�||d	<   |	�|	|d
<   t        j                  d|| j                  «       |¬«      }|
r|S t        j                  «       }	 |j                  |j                  «       | j                  ¬«      }|j                  «        |j                  «       S # t        j                  j                  $ r*}|j                  j                  dk(  rt        |«      ‚‚ d}~ww xY w)aÄ  
        Create a chat completion.

        Args:
            model (str): The model to use for completion
            messages (List[Dict[str, str]]): The messages to generate a completion for
            temperature (Optional[float]): Sampling temperature between 0 and 2
            top_p (Optional[float]): Nucleus sampling parameter between 0 and 1
            n (Optional[int]): Number of completions to generate
            max_tokens (Optional[int]): Maximum number of tokens to generate
            presence_penalty (Optional[float]): Presence penalty between -2.0 and 2.0
            frequency_penalty (Optional[float]): Frequency penalty between -2.0 and 2.0
            user (Optional[str]): Unique identifier for the end user
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the completion response from the server or
            a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        ú/chat/completions)r   r   Nr   r   r    r!   r"   r#   r$   ÚPOST)r   Újson)r   é‘  )r   ÚrequestsÚRequestr   ÚSessionÚsendÚpreparer   Úraise_for_statusr)   Ú
exceptionsÚ	HTTPErrorÚresponseÚstatus_coder   )r   r   r   r   r   r    r!   r"   r#   r$   r%   ÚurlÚdataÚrequestÚsessionr3   Úes                    r   ÚcompletionszChatClient.completions%   sA  € ðH Ÿ™Ð'Ð'8Ð9ˆð 16À8Ñ&Lˆð Ð"Ø"-ˆD�ÑØÐØ!ˆD�‰MØˆ=ØˆD�‰IØÐ!Ø!+ˆD�ÑØÐ'Ø'7ˆDÐ#Ñ$ØÐ(Ø(9ˆDÐ$Ñ%ØÐØˆD�‰Lä!×)Ñ)¨&°#¸t×?PÑ?PÓ?RÐY]Ô^ˆáØˆNô "×)Ñ)Ó+ˆð	Ø%Ÿl™l¨7¯?©?Ó+<ÀdÇmÁm˜lÓTˆHØ×%Ñ%Ô'Ø—=‘=“?Ð"øÜ×"Ñ"×,Ñ,ò 	Ø�z‰z×%Ñ%¨Ò,Ü'¨Ó*Ð*Øûð	ús   ÂA
C ÃDÃ.%DÄDc
              #   óâ  K  — | j                   › d�}
||ddœ}|�||d<   |�||d<   |�||d<   |�||d<   |�||d	<   |�||d
<   |	�|	|d<   t        j                  «       }	 |j                  |
| j	                  «       |d| j
                  ¬«      }|j                  «        |j                  «       D ]\  }|sŒ|j                  d«      }|j                  d«      sŒ)|dd }|j                  «       dk(  r y	 t        j                  |«      }|–— Œ^ y# t        j                  $ r Y Œuw xY w# t        j                  j                  $ r*}|j                   j"                  dk(  rt%        |«      ‚‚ d}~ww xY w­w)a  
        Create a streaming chat completion.

        Args:
            model (str): The model to use for completion
            messages (List[Dict[str, str]]): The messages to generate a completion for
            temperature (Optional[float]): Sampling temperature between 0 and 2
            top_p (Optional[float]): Nucleus sampling parameter between 0 and 1
            n (Optional[int]): Number of completions to generate
            max_tokens (Optional[int]): Maximum number of tokens to generate
            presence_penalty (Optional[float]): Presence penalty between -2.0 and 2.0
            frequency_penalty (Optional[float]): Frequency penalty between -2.0 and 2.0
            user (Optional[str]): Unique identifier for the end user

        Yields:
            Dict[str, Any]: Streaming response chunks from the server

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        r'   T)r   r   ÚstreamNr   r   r    r!   r"   r#   r$   )r   r)   r<   r   zutf-8zdata: é   z[DONE]r*   )r   r+   r-   Úpostr   r   r0   Ú
iter_linesÚdecodeÚ
startswithÚstripr)   ÚloadsÚJSONDecodeErrorr1   r2   r3   r4   r   )r   r   r   r   r   r    r!   r"   r#   r$   r5   r6   r8   r3   ÚlineÚdata_strÚchunkr9   s                     r   Úcompletions_streamzChatClient.completions_streamn   s­  è ø€ ðB Ÿ™Ð'Ð'8Ð9ˆð 16À8ÐW[Ñ&\ˆð Ð"Ø"-ˆD�ÑØÐØ!ˆD�‰MØˆ=ØˆD�‰IØÐ!Ø!+ˆD�ÑØÐ'Ø'7ˆDÐ#Ñ$ØÐ(Ø(9ˆDÐ$Ñ%ØÐØˆD�‰Lô "×)Ñ)Ó+ˆð	Ø%Ÿl™lØ˜T×.Ñ.Ó0°tÀDÐRV×R_ÑR_ð +ó ˆHð ×%Ñ%Ô'ð !×+Ñ+Ó-ò %�ÚØŸ;™; wÓ/�DØ—‘ xÕ0Ø#'¨¨ 8˜Ø#Ÿ>™>Ó+¨xÒ7Ù!ð%Ü$(§J¡J¨xÓ$8˜EØ"'›Kñ%øô  $×3Ñ3ò %Ù$ð%ûô ×"Ñ"×,Ñ,ò 	Ø�z‰z×%Ñ%¨Ò,Ü'¨Ó*Ð*Øûð	üsa   ‚AE/ÁAD% Â2"D% ÃD% Ã.E/Ã0DÄ	D% ÄE/ÄD"ÄD% Ä!D"Ä"D% Ä%E,Å%E'Å'E,Å,E/)NiX  )NNNNNNNF)NNNNNNN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚintr   Údictr   ÚlistÚfloatÚboolr   r+   r,   r:   r   rH   © r   r   r	   r	   
   sÇ  „ ñ  ð  ¨s°T©zð  È3ó  ð
˜d 3¨ 8™nó 
ð  %)Ø"ØØ!%Ø)-Ø*.ØØ$ñGàðGð �t˜C ˜H‘~Ñ&ðGð ˜T‘\ð	Gð
 �t‰|ðGð �‰:ðGð ˜$‘JðGð   $™,ðGð ! 4™<ðGð �D‰jðGð ðGð 
ˆc�3ˆh‰˜(×*Ñ*Ñ	*óGðZ %)Ø"ØØ!%Ø)-Ø*.ØñOàðOð �t˜C ˜H‘~Ñ&ðOð ˜T‘\ð	Oð
 �t‰|ðOð �‰:ðOð ˜$‘JðOð   $™,ðOð ! 4™<ðOð �D‰jðOð 
�$�s˜C�x‘.Ñ	!ôOr   r	   )
r)   Úcollections.abcr   Útypingr   r   r+   r1   r   r	   rR   r   r   ú<module>rU      s    ðÛ Ý $ß ã å )÷sò sr   