
    qi                         d Z ddlmZmZ ddlmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ  e	deeef         Z G d	 d
eee         Zy)zBase adapter for LLM provider integration.

This module provides the abstract base class for implementing LLM provider-specific
adapters that handle tool format conversion and standardization.
    )ABCabstractmethod)AnyDictGenericListTypeVar)logger)ToolsSchema)
LLMContextLLMContextMessageLLMSpecificMessageNotGivenTLLMInvocationParams)boundc                       e Zd ZdZeedefd              Zedede	fd       Z
ededee   fd       Zededeeeef      fd       Zd	edefd
Zdedee   fdZdedee   ez  fdZy)BaseLLMAdaptera  Abstract base class for LLM provider adapters.

    Provides a standard interface for converting to provider-specific formats.

    Handles:

    - Extracting provider-specific parameters for LLM invocation from a
      universal LLM context
    - Converting standardized tools schema to provider-specific tool formats.
    - Extracting messages from the LLM context for the purposes of logging
      about the specific provider.

    Subclasses must implement provider-specific conversion logic.
    returnc                      y)zGet the identifier used in LLMSpecificMessage instances for this LLM provider.

        Returns:
            The identifier string.
        N )selfs    S/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/adapters/base_llm_adapter.pyid_for_llm_specific_messagesz+BaseLLMAdapter.id_for_llm_specific_messages.   s     	    contextc                      y)aR  Get provider-specific LLM invocation parameters from a universal LLM context.

        Args:
            context: The LLM context containing messages, tools, etc.
            **kwargs: Additional provider-specific arguments that subclasses can use.

        Returns:
            Provider-specific parameters for invoking the LLM.
        Nr   )r   r   kwargss      r   get_llm_invocation_paramsz(BaseLLMAdapter.get_llm_invocation_params8        	r   tools_schemac                      y)zConvert tools schema to the provider's specific format.

        Args:
            tools_schema: The standardized tools schema to convert.

        Returns:
            List of tools in the provider's expected format.
        Nr   )r   r    s     r   to_provider_tools_formatz'BaseLLMAdapter.to_provider_tools_formatE   s     	r   c                      y)a  Get messages from a universal LLM context in a format ready for logging about this provider.

        Args:
            context: The LLM context containing messages.

        Returns:
            List of messages in a format ready for logging about this
            provider.
        Nr   r   r   s     r   get_messages_for_loggingz'BaseLLMAdapter.get_messages_for_loggingQ   r   r   messagec                 0    t        | j                  |      S )zCreate an LLM-specific message (as opposed to a standard message) for use in an LLMContext.

        Args:
            message: The message content.

        Returns:
            A LLMSpecificMessage instance.
        )llmr&   )r   r   )r   r&   s     r   create_llm_specific_messagez*BaseLLMAdapter.create_llm_specific_message^   s     "d&G&GQXYYr   c                 8    |j                  | j                        S )zGet messages from the LLM context, including standard and LLM-specific messages.

        Args:
            context: The LLM context containing messages.

        Returns:
            List of messages including standard and LLM-specific messages.
        )get_messagesr   r$   s     r   r+   zBaseLLMAdapter.get_messagesi   s     ##D$E$EFFr   toolsc                     t        |t              r2t        j                  dt	        |               | j                  |      S |S )a  Convert tools from standard format to provider format.

        Args:
            tools: Tools in standard format or provider-specific format.

        Returns:
            List of tools converted to provider format, or original tools
            if not in standard format.
        z(Retrieving the tools using the adapter: )
isinstancer   r
   debugtyper"   )r   r,   s     r   from_standard_toolsz"BaseLLMAdapter.from_standard_toolst   s<     e[)LLCDJ<PQ0077r   N)__name__
__module____qualname____doc__propertyr   strr   r   r   r   r   r   r   r"   r   r%   r   r)   r   r+   r   r1   r   r   r   r   r      s     c    
 
J^ 
 
 	[ 	T#Y 	 	 

 
tDcN?S 
 
	Z3 	Z;M 	Z	GJ 	G48I3J 	G cX1E r   r   N)r5   abcr   r   typingr   r   r   r   r	   logurur
   %pipecat.adapters.schemas.tools_schemar   *pipecat.processors.aggregators.llm_contextr   r   r   r   dictr7   r   r   r   r   r   <module>r>      sS    $ 4 4  =  5T#s(^L dS'"67 dr   