
    qi                      "   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 ddlmZ 	 dd	lmZ e G d de             Z G d de      Zy# e$ r7Z ej*                  d
e         ej*                  d        ede       dZ[ww xY w)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)OpenAILLMSettings)OpenAILLMService)_warn_deprecated_param)AsyncOpenAIzException: zIIn order to use OpenPipe, you need to `pip install pipecat-ai[openpipe]`.zMissing module: Nc                       e Zd ZdZy)OpenPipeLLMSettingsz Settings for OpenPipeLLMService.N)__name__
__module____qualname____doc__     O/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/openpipe/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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Z
ddZdedef fdZ x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          	          t        d      }	|t        dt         d       ||	_        ||	j                  |       t	        
|   d|||||	d| || _        y)a  Initialize OpenPipe LLM service.

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

                .. deprecated:: 0.0.105
                    Use ``settings=OpenAILLMSettings(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   )r   r
   r   apply_updatesuper__init___tags)selfr   r   r   r   r   r   r   kwargsdefault_settings	__class__s             r   r"   zOpenPipeLLMService.__init__1   sx    < /Y? "7,?I%*"
 ))(3 	
-/%	
 	
 
r   c                 |    |j                  d      xs d}|j                  d      xs d}t        ||||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   clients          r   create_clientz OpenPipeLLMService.create_clientf   sN     "::&89?R"JJ':;Ar!1?PQ

 r   params_from_contextreturnc                 J    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'   s      r   r3   z/OpenPipeLLMService.build_chat_completion_paramsz   s4     56IJ JJ
z
 r   )NN)r   r   r   r   r   Settings__annotations__r   strr   r"   r.   r   dictr3   __classcell__)r'   s   @r   r   r   &   s     #H""
  $!%"&*.!A)-263 }3 #	3
 3-3 #3-3 3 tCH~&3 ./3j(@Y ^b  r   r   )r   dataclassesr   typingr   r   logurur   )pipecat.adapters.services.open_ai_adapterr    pipecat.services.openai.base_llmr   pipecat.services.openai.llmr	   pipecat.services.settingsr
   r*   r   r,   ModuleNotFoundErroreerror	Exceptionr   r   r   r   r   <module>rE      s    " !  O > 8 <,2 	+ 	 	j) j  ,FLL;qc"#FLL\]
&qc*
++,s   A B2B		B