o
    i                     @   sr   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 eG dd de
jZG d	d
 d
eZdS )zOpenRouter 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)BaseOpenAILLMService)OpenAILLMServicec                   @   s   e Zd ZdZdS )OpenRouterLLMSettingsz"Settings for OpenRouterLLMService.N)__name__
__module____qualname____doc__ r   r   S/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/openrouter/llm.pyr	      s    r	   c                
       s   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
dZ	d fdd	Z
deeef deeef f fddZ  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                   sV   | j dd}|dur| dd ||_|dur|| t jd|||d| dS )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=OpenRouterLLMService.Settings(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   )Settings"_warn_init_param_moved_to_settingsr   apply_updatesuper__init__)selfr   r   r   r   kwargsdefault_settings	__class__r   r   r   '   s   

zOpenRouterLLMService.__init__c                    s&   t d|  t j||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   r   r   r!   T   s   z"OpenRouterLLMService.create_clientparams_from_contextreturnc                    s   t  |}d| jj v rI|dg }|s|S g }d}|D ]&}|ddkr?|s1|| d}q| }d|d< || q|| q||d< |S )zBuilds 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   r   r   r)   b   s$   	
z1OpenRouterLLMService.build_chat_completion_params)NN)r
   r   r   r   r	   r   __annotations__r   strr   r!   r   r   r)   __classcell__r   r   r   r   r      s&   
 -.r   N)r   dataclassesr   typingr   r   r   logurur    pipecat.services.openai.base_llmr   pipecat.services.openai.llmr   r   r	   r   r   r   r   r   <module>   s   