o
    h                     @  sb   d dl mZ d dlmZ ddlmZ ddlmZmZ ddl	m
Z
 G dd	 d	ZG d
d deZdS )    )annotations)AsyncOpenAI   )	UserError   )ModelModelProvider)OpenAIProviderc                   @  sT   e Zd ZdZdd Zddd	ZdddZdddZdddZdddZ	dddZ
dS ) MultiProviderMapz/A map of model name prefixes to ModelProviders.c                 C  s
   i | _ d S )N_mappingself r   ]/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/models/multi_provider.py__init__   s   
zMultiProviderMap.__init__prefixstrreturnboolc                 C  s
   || j v S )z3Returns True if the given prefix is in the mapping.r   r   r   r   r   r   
has_prefix      
zMultiProviderMap.has_prefixdict[str, ModelProvider]c                 C  s
   | j  S )z>Returns a copy of the current prefix -> ModelProvider mapping.)r   copyr   r   r   r   get_mapping   r   zMultiProviderMap.get_mappingmappingc                 C  s
   || _ dS )z.Overwrites the current mapping with a new one.Nr   )r   r   r   r   r   set_mapping   r   zMultiProviderMap.set_mappingModelProvider | Nonec                 C  s   | j |S )zReturns the ModelProvider for the given prefix.

        Args:
            prefix: The prefix of the model name e.g. "openai" or "my_prefix".
        )r   getr   r   r   r   get_provider      zMultiProviderMap.get_providerproviderr   c                 C  s   || j |< dS )zAdds a new prefix -> ModelProvider mapping.

        Args:
            prefix: The prefix of the model name e.g. "openai" or "my_prefix".
            provider: The ModelProvider to use for the given prefix.
        Nr   )r   r   r"   r   r   r   add_provider$   s   zMultiProviderMap.add_providerc                 C  s   | j |= dS )zRemoves the mapping for the given prefix.

        Args:
            prefix: The prefix of the model name e.g. "openai" or "my_prefix".
        Nr   r   r   r   r   remove_provider-   r!   z MultiProviderMap.remove_providerN)r   r   r   r   )r   r   )r   r   )r   r   r   r   )r   r   r"   r   )r   r   )__name__
__module____qualname____doc__r   r   r   r   r    r#   r$   r   r   r   r   r
   
   s    




	r
   c                   @  sT   e Zd ZdZddddddddd!ddZd"ddZd#ddZd$ddZd%dd ZdS )&MultiProvideraA  This ModelProvider maps to a Model based on the prefix of the model name. By default, the
    mapping is:
    - "openai/" prefix or no prefix -> OpenAIProvider. e.g. "openai/gpt-4.1", "gpt-4.1"
    - "litellm/" prefix -> LitellmProvider. e.g. "litellm/openai/gpt-4.1"

    You can override or customize this mapping.
    N)provider_mapopenai_api_keyopenai_base_urlopenai_clientopenai_organizationopenai_projectopenai_use_responsesr*   MultiProviderMap | Noner+   
str | Noner,   r-   AsyncOpenAI | Noner.   r/   r0   bool | Noner   Nonec                C  s&   || _ t||||||d| _i | _dS )a  Create a new OpenAI provider.

        Args:
            provider_map: A MultiProviderMap that maps prefixes to ModelProviders. If not provided,
                we will use a default mapping. See the documentation for this class to see the
                default mapping.
            openai_api_key: The API key to use for the OpenAI provider. If not provided, we will use
                the default API key.
            openai_base_url: The base URL to use for the OpenAI provider. If not provided, we will
                use the default base URL.
            openai_client: An optional OpenAI client to use. If not provided, we will create a new
                OpenAI client using the api_key and base_url.
            openai_organization: The organization to use for the OpenAI provider.
            openai_project: The project to use for the OpenAI provider.
            openai_use_responses: Whether to use the OpenAI responses API.
        )api_keybase_urlr-   organizationprojectuse_responsesN)r*   r	   openai_provider_fallback_providers)r   r*   r+   r,   r-   r.   r/   r0   r   r   r   r   ?   s   
	zMultiProvider.__init__
model_nametuple[str | None, str | None]c                 C  s4   |d u rdS d|v r| dd\}}||fS d |fS )N)NN/r   )split)r   r=   r   r   r   r   _get_prefix_and_model_namef   s   z(MultiProvider._get_prefix_and_model_namer   r   r   c                 C  s(   |dkrddl m} | S td| )Nlitellmr   )LitellmProviderzUnknown prefix: )"extensions.models.litellm_providerrC   r   )r   r   rC   r   r   r   _create_fallback_providero   s   z'MultiProvider._create_fallback_providerc                 C  sD   |d u s|dkr| j S || jv r| j| S | || j|< | j| S )Nopenai)r;   r<   rE   r   r   r   r   _get_fallback_providerw   s   


z$MultiProvider._get_fallback_providerr   c                 C  sB   |  |\}}|r| jr| j| }r||S | ||S )aK  Returns a Model based on the model name. The model name can have a prefix, ending with
        a "/", which will be used to look up the ModelProvider. If there is no prefix, we will use
        the OpenAI provider.

        Args:
            model_name: The name of the model to get.

        Returns:
            A Model.
        )rA   r*   r    	get_modelrG   )r   r=   r   r"   r   r   r   rH      s   
zMultiProvider.get_model)r*   r1   r+   r2   r,   r2   r-   r3   r.   r2   r/   r2   r0   r4   r   r5   )r=   r2   r   r>   )r   r   r   r   )r   r2   r   r   )r=   r2   r   r   )	r%   r&   r'   r(   r   rA   rE   rG   rH   r   r   r   r   r)   6   s    
'
	
	r)   N)
__future__r   rF   r   
exceptionsr   	interfacer   r   r;   r	   r
   r)   r   r   r   r   <module>   s    ,