o
    i+                     @  s   d dl mZ d dlmZmZ d dlmZmZm	Z	 d dl
mZm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mZ d
dlmZ ddlmZmZm Z m!Z!m"Z" erld
dl#m$Z$m%Z% d
dl&m'Z' eddG dd deZ(dddZ)dS )    )annotations)AsyncIteratorCallable)AsyncExitStackasynccontextmanagersuppress)	dataclassfield)cached_property)TYPE_CHECKINGAny)get_current_span)
RunContext)InstrumentedModel   )FallbackExceptionGroupModelHTTPError)ModelProfile   )KnownModelNameModelModelRequestParametersStreamedResponseinfer_model)ModelMessageModelResponse)ModelSettingsF)initc                      s   e Zd ZU dZded< eddZded< ded	< efd
d5 fddZe	d6ddZ
e	d6ddZe	d7ddZd8d!d"Ze	#d9d:d'd(Zed;d*d+Zd<d,d-Zd=d/d0Zd>d3d4Z  ZS )?FallbackModelzA model that uses one or more fallback models upon failure.

    Apart from `__init__`, all methods are private or match those of the base class.
    zlist[Model]modelsF)reprstr_model_nameCallable[[Exception], bool]_fallback_on)fallback_ondefault_modelModel | KnownModelName | strfallback_modelsr%   9Callable[[Exception], bool] | tuple[type[Exception], ...]c                  sF   t    t|gdd |D | _t|trt|| _dS || _dS )aE  Initialize a fallback model instance.

        Args:
            default_model: The name or instance of the default model to use.
            fallback_models: The names or instances of the fallback models to use upon failure.
            fallback_on: A callable or tuple of exceptions that should trigger a fallback.
        c                 S  s   g | ]}t |qS  )r   ).0mr*   r*   a/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/models/fallback.py
<listcomp>1   s    z*FallbackModel.__init__.<locals>.<listcomp>N)super__init__r   r   
isinstancetuple#_default_fallback_condition_factoryr$   )selfr&   r%   r(   	__class__r*   r-   r0   #   s
   


zFallbackModel.__init__returnc                 C     dd dd | jD  S )zThe model name.	fallback:,c                 s      | ]}|j V  qd S N)
model_namer+   modelr*   r*   r-   	<genexpr>;       z+FallbackModel.model_name.<locals>.<genexpr>joinr   r4   r*   r*   r-   r=   8   s   zFallbackModel.model_namec                 C  r8   )Nr9   r:   c                 s  r;   r<   )systemr>   r*   r*   r-   r@   ?   rA   z'FallbackModel.system.<locals>.<genexpr>rB   rD   r*   r*   r-   rE   =   s   zFallbackModel.system
str | Nonec                 C  s   | j d jS )Nr   )r   base_urlrD   r*   r*   r-   rG   A   s   zFallbackModel.base_urlmessageslist[ModelMessage]model_settingsModelSettings | Nonemodel_request_parametersr   r   c           
        s   g }| j D ]>}z|||\}}||||I dH }W n ty: }	 z| |	r4||	 W Y d}	~	q|	d}	~	ww | || |  S td|)zTry each model in sequence until one succeeds.

        In case of failure, raise a FallbackExceptionGroup with all exceptions.
        N$All models from FallbackModel failed)r   prepare_requestrequest	Exceptionr$   append_set_span_attributesr   )
r4   rH   rJ   rL   
exceptionsr?   _prepared_parametersresponseexcr*   r*   r-   rO   E   s    




zFallbackModel.requestNrun_contextRunContext[Any] | NoneAsyncIterator[StreamedResponse]c                 C s   g }| j D ]k}t 4 I dH W}z|||\}}	||||||I dH }
W n' tyO } z| |rI|| W Y d}~W d  I dH  q|d}~ww | ||	 |
V  	 W d  I dH   dS 1 I dH slw   Y  qt	d|)z.Try each model in sequence until one succeeds.NrM   )
r   r   rN   enter_async_contextrequest_streamrP   r$   rQ   rR   r   )r4   rH   rJ   rL   rX   rS   r?   stackrT   rU   rV   rW   r*   r*   r-   r\   `   s,   	




0
zFallbackModel.request_streamr   c                 C  s   t d)Nz2FallbackModel does not have its own model profile.)NotImplementedErrorrD   r*   r*   r-   profile~   s   zFallbackModel.profilec                 C  s   |S r<   r*   )r4   rL   r*   r*   r-   customize_request_parameters   s   z*FallbackModel.customize_request_parameters3tuple[ModelSettings | None, ModelRequestParameters]c                 C  s   ||fS r<   r*   )r4   rJ   rL   r*   r*   r-   rN      s   zFallbackModel.prepare_requestr?   r   c                 C  s   t t= t }| r1t|di }|d| jkr9|i t	|t
| W d    d S W d    d S W d    d S 1 sDw   Y  d S )N
attributeszgen_ai.request.model)r   rP   r   is_recordinggetattrgetr=   set_attributesr   model_attributes#model_request_parameters_attributes)r4   r?   rL   spanrb   r*   r*   r-   rR      s"   
"z"FallbackModel._set_span_attributes)r&   r'   r(   r'   r%   r)   )r7   r!   )r7   rF   )rH   rI   rJ   rK   rL   r   r7   r   r<   )
rH   rI   rJ   rK   rL   r   rX   rY   r7   rZ   )r7   r   )rL   r   r7   r   )rJ   rK   rL   r   r7   ra   )r?   r   rL   r   )__name__
__module____qualname____doc____annotations__r	   r"   r   r0   propertyr=   rE   rG   rO   r   r\   r
   r_   r`   rN   rR   __classcell__r*   r*   r5   r-   r      s,   
 


r   rS   tuple[type[Exception], ...]r7   r#   c                   s   d fdd}|S )	z=Create a default fallback condition for the given exceptions.	exceptionrP   r7   boolc                   s
   t |  S r<   )r1   )rr   rS   r*   r-   fallback_condition   s   
z?_default_fallback_condition_factory.<locals>.fallback_conditionN)rr   rP   r7   rs   r*   )rS   ru   r*   rt   r-   r3      s   r3   N)rS   rq   r7   r#   )*
__future__r   _annotationscollections.abcr   r   
contextlibr   r   r   dataclassesr   r	   	functoolsr
   typingr   r   opentelemetry.tracer   pydantic_ai._run_contextr   pydantic_ai.models.instrumentedr   rS   r   r   profilesr    r   r   r   r   r   rH   r   r   settingsr   r   r3   r*   r*   r*   r-   <module>   s&     