o
    i                  
   @   s   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ zddlmZ W n  eyP Z zed	e  ed
 ede dZ[ww eG dd dejZG dd deZdS )zOpenPipe LLM service implementation for Pipecat.

This module provides an OpenPipe-specific implementation of the OpenAI LLM service,
enabling integration with OpenPipe's fine-tuning and monitoring capabilities.
    )	dataclass)DictOptional)logger)OpenAILLMInvocationParams)BaseOpenAILLMService)OpenAILLMService)AsyncOpenAIzException: zIIn order to use OpenPipe, you need to `pip install pipecat-ai[openpipe]`.zMissing module: Nc                   @   s   e Zd ZdZdS )OpenPipeLLMSettingsz Settings for OpenPipeLLMService.N)__name__
__module____qualname____doc__ r   r   Q/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/openpipe/llm.pyr
      s    r
   c                       s   e Zd ZU dZeZeed< dddddddddee dee dee d	ee d
edee	eef  dee f fddZ
dddZdedef fddZ  ZS )OpenPipeLLMServicezOpenPipe-powered Large Language Model service.

    Extends OpenAI's LLM service to integrate with OpenPipe's fine-tuning and
    monitoring platform. Provides enhanced request logging and tagging capabilities
    for model training and evaluation.
    	_settingsNzhttps://app.openpipe.ai/api/v1)modelapi_keybase_urlopenpipe_api_keyopenpipe_base_urltagssettingsr   r   r   r   r   r   r   c          
         s`   | j dd}	|dur| dd ||	_|dur|	| t jd|||||	d| || _dS )a  Initialize OpenPipe LLM service.

        Args:
            model: The model name to use. Defaults to "gpt-4.1".

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

            api_key: OpenAI API key for authentication. If None, reads from environment.
            base_url: Custom OpenAI API endpoint URL. Uses default if None.
            openpipe_api_key: OpenPipe API key for enhanced features. If None, reads from environment.
            openpipe_base_url: OpenPipe API endpoint URL. Defaults to "https://app.openpipe.ai/api/v1".
            tags: Optional dictionary of tags to apply to all requests for tracking.
            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional arguments passed to parent OpenAILLMService.
        zgpt-4.1)r   Nr   )r   r   r   r   r   r   )Settings"_warn_init_param_moved_to_settingsr   apply_updatesuper__init___tags)
selfr   r   r   r   r   r   r   kwargsdefault_settings	__class__r   r   r   0   s    

zOpenPipeLLMService.__init__c                 K   s4   | dpd}| dpd}t||||dd}|S )aJ  Create an OpenPipe client instance.

        Args:
            api_key: OpenAI API key for authentication.
            base_url: OpenAI API base URL.
            **kwargs: Additional arguments including openpipe_api_key and openpipe_base_url.

        Returns:
            Configured OpenPipe AsyncOpenAI client instance.
        r    r   )r   r   )r   r   openpipe)get
OpenPipeAI)r    r   r   r!   r   r   clientr   r   r   create_cliente   s   z OpenPipeLLMService.create_clientparams_from_contextreturnc                    s    t  |}| jdd|d< |S )a  Build parameters for OpenPipe chat completion request.

        Adds OpenPipe-specific logging and tagging parameters.

        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   log_requestr&   )r   build_chat_completion_paramsr   )r    r+   paramsr#   r   r   r.   y   s
   
z/OpenPipeLLMService.build_chat_completion_params)NN)r   r   r   r   r
   r   __annotations__r   strr   r   r*   r   dictr.   __classcell__r   r   r#   r   r   %   s8   
 	
5r   )r   dataclassesr   typingr   r   logurur   )pipecat.adapters.services.open_ai_adapterr    pipecat.services.openai.base_llmr   pipecat.services.openai.llmr   r&   r	   r(   ModuleNotFoundErroreerror	Exceptionr   r
   r   r   r   r   r   <module>   s$   
