o
    i                     @   sv   d Z ddlmZ ddl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	 d	e
jZG d
d deZdS )zFFireworks AI service implementation using OpenAI-compatible interface.    )	dataclass)Optional)logger)OpenAILLMInvocationParams)BaseOpenAILLMService)OpenAILLMServicec                   @   s   e Zd ZdZdS )FireworksLLMSettingsz!Settings for FireworksLLMService.N)__name__
__module____qualname____doc__ r   r   R/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/fireworks/llm.pyr      s    r   c                
       sr   e Zd ZU dZeZeed< dddddedee ded	ee f fd
dZ	d fdd	Z
dedefddZ  ZS )FireworksLLMServicezA service for interacting with Fireworks AI using the OpenAI-compatible interface.

    This service extends OpenAILLMService to connect to Fireworks' API endpoint while
    maintaining full compatibility with OpenAI's interface and functionality.
    	_settingsNz%https://api.fireworks.ai/inference/v1)modelbase_urlsettingsapi_keyr   r   r   c                   sV   | j dd}|dur| dd ||_|dur|| t jd|||d| dS )a  Initialize the Fireworks LLM service.

        Args:
            api_key: The API key for accessing Fireworks AI.
            model: The model identifier to use. Defaults to "accounts/fireworks/models/firefunction-v2".

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

            base_url: The base URL for Fireworks API. Defaults to "https://api.fireworks.ai/inference/v1".
            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional keyword arguments passed to OpenAILLMService.
        z)accounts/fireworks/models/firefunction-v2)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FireworksLLMService.__init__c                    s&   t d|  t j||fi |S )a  Create OpenAI-compatible client for Fireworks API endpoint.

        Args:
            api_key: API key for authentication. If None, uses instance default.
            base_url: Base URL for the API. If None, uses instance default.
            **kwargs: Additional arguments passed to the client constructor.

        Returns:
            Configured OpenAI client instance for Fireworks API.
        z#Creating Fireworks client with api )r   debugr   create_client)r   r   r   r   r   r   r   r    K   s   z!FireworksLLMService.create_clientparams_from_contextreturnc                 C   s   | j jd| j j| j j| j j| j j| j jd}|| || j j | j j	rK|
dg }|r?|d 
ddkr?t|  d d| j j	dg| |d< |S )	a  Build parameters for Fireworks chat completion request.

        Fireworks doesn't support some OpenAI parameters like seed, max_completion_tokens,
        and stream_options.

        Args:
            params_from_context: Parameters, derived from the LLM context, to
                use for the chat completion. Contains messages, tools, and tool
                choice.

        Returns:
            Dictionary of parameters for the chat completion request.
        T)r   streamfrequency_penaltypresence_penaltytemperaturetop_p
max_tokensmessagesr   rolesystemzc: Both system_instruction and an initial system message in context are set. This may be unintended.)r*   content)r   r   r$   r%   r&   r'   r(   updateextrasystem_instructiongetr   warning)r   r!   paramsr)   r   r   r   build_chat_completion_paramsY   s*   
z0FireworksLLMService.build_chat_completion_params)NN)r	   r
   r   r   r   r   __annotations__strr   r   r    r   dictr3   __classcell__r   r   r   r   r      s$   
 'r   N)r   dataclassesr   typingr   logurur   )pipecat.adapters.services.open_ai_adapterr    pipecat.services.openai.base_llmr   pipecat.services.openai.llmr   r   r   r   r   r   r   r   <module>   s   