Ë
    ±q±i'  ã                   óŽ   — d Z ddlmZ ddl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Z e G d„ d	e
«      «       Z G d
„ de«      Zy)z¸OpenRouter LLM service implementation.

This module provides an OpenAI-compatible interface for interacting with OpenRouter's API,
extending the base OpenAI LLM service functionality.
é    )Ú	dataclass)ÚAnyÚDictÚOptional)Úlogger)ÚOpenAILLMSettings)ÚOpenAILLMService)Ú_warn_deprecated_paramc                   ó   — e Zd ZdZy)ÚOpenRouterLLMSettingsz"Settings for OpenRouterLLMService.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úQ/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/openrouter/llm.pyr   r      s   „ á,àr   r   c            
       ó–   ‡ — e Zd ZU dZeZeed<   dddddœdee   dee   ded	ee   fˆ fd
„Z	dˆ fd„	Z
deeef   deeef   fˆ fd„Zˆ xZS )ÚOpenRouterLLMServicea  A service for interacting with OpenRouter's API using the OpenAI-compatible interface.

    This service extends OpenAILLMService to connect to OpenRouter's API endpoint while
    maintaining full compatibility with OpenAI's interface and functionality.
    Ú	_settingsNzhttps://openrouter.ai/api/v1)Úapi_keyÚmodelÚbase_urlÚsettingsr   r   r   r   c                óž   •— t        d¬«      }|t        dt         d«       ||_        ||j                  |«       t	        ‰|   d|||dœ|¤Ž y)aÞ  Initialize the OpenRouter LLM service.

        Args:
            api_key: The API key for accessing OpenRouter's API. If None, will attempt
                to read from environment variables.
            model: The model identifier to use. Defaults to "openai/gpt-4o-2024-11-20".

                .. deprecated:: 0.0.105
                    Use ``settings=OpenAILLMSettings(model=...)`` instead.

            base_url: The base URL for OpenRouter API. Defaults to "https://openrouter.ai/api/v1".
            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional keyword arguments passed to OpenAILLMService.
        zopenai/gpt-4o-2024-11-20)r   Nr   )r   r   r   r   )r   r
   r   Úapply_updateÚsuperÚ__init__)Úselfr   r   r   r   ÚkwargsÚdefault_settingsÚ	__class__s          €r   r   zOpenRouterLLMService.__init__(   sj   ø€ ô2 1Ð7QÔRÐð ÐÜ" 7Ô,AÀ7ÔKØ%*ÐÔ"ð
 ÐØ×)Ñ)¨(Ô3ä‰Ñð 	
ØØØ%ñ	
ð ó		
r   c                 óV   •— t        j                  d|› «       t        ‰|   ||fi |¤ŽS )a‰  Create an OpenRouter API client.

        Args:
            api_key: The API key to use for authentication. If None, uses instance default.
            base_url: The base URL for the API. If None, uses instance default.
            **kwargs: Additional arguments passed to the parent client creation method.

        Returns:
            The configured OpenRouter API client instance.
        z$Creating OpenRouter client with api )r   Údebugr   Úcreate_client)r   r   r   r    r"   s       €r   r%   z"OpenRouterLLMService.create_clientU   s0   ø€ ô 	‰Ð;¸H¸:ÐFÔGÜ‰wÑ$ W¨hÑA¸&ÑAÐAr   Úparams_from_contextÚreturnc                 ó‚  •— t         ‰|   |«      }d| j                  j                  j	                  «       v rˆ|j                  dg «      }|s|S g }d}|D ]d  }|j                  d«      dk(  r=|s|j                  |«       d}Œ-|j                  «       }d|d<   |j                  |«       ŒT|j                  |«       Œf ||d<   |S )zãBuilds chat parameters, handling model-specific constraints.

        Args:
            params_from_context: Parameters from the LLM context.

        Returns:
            Transformed parameters ready for the API call.
        ÚgeminiÚmessagesFÚroleÚsystemTÚuser)r   Úbuild_chat_completion_paramsr   r   ÚlowerÚgetÚappendÚcopy)	r   r&   Úparamsr*   Útransformed_messagesÚsystem_message_seenÚmsgÚnew_msgr"   s	           €r   r.   z1OpenRouterLLMService.build_chat_completion_paramsc   sË   ø€ ô ‘Ñ5Ð6IÓJˆØt—~‘~×+Ñ+×1Ñ1Ó3Ñ3Ø—z‘z *¨bÓ1ˆHÙØØ#%Ð Ø"'ÐØò 
5Ø—7‘7˜6“? hÒ.Ù.Ø,×3Ñ3°CÔ8Ø.2Ñ+à"%§(¡(£*˜Ø*0˜ ™Ø,×3Ñ3°GÕ<à(×/Ñ/°Õ4ð
5ð "6ˆF:Ñàˆr   )NN)r   r   r   r   r   ÚSettingsÚ__annotations__r   Ústrr   r%   r   r   r.   Ú__classcell__)r"   s   @r   r   r      s’   ø… ñð %€HØ$Ó$ð
 "&Ø#Ø6Ø48ò+
ð ˜#‘ð+
ð ˜‰}ð	+
ð
 ð+
ð Ð0Ñ1õ+
õZBðÀÀSÈ#ÀXÁð ÐSWÐX[Ð]`ÐX`ÑSa÷ ñ r   r   N)r   Údataclassesr   Útypingr   r   r   Úlogurur   Ú pipecat.services.openai.base_llmr   Úpipecat.services.openai.llmr	   Úpipecat.services.settingsr
   r   r   r   r   r   ú<module>rB      sL   ðñõ "ß &Ñ &å å >Ý 8Ý <ð ô	Ð-ó 	ó ð	ôbÐ+õ br   