o
    iT                  
   @  sZ  U d dl 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mZ d dlmZmZmZmZ d dlmZ dd	l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  ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z< ddl=m>Z>m?Z? ddl@mAZA ddlBmCZC ddlmDZDmEZEmFZFmGZG z$d dlHZHd dlImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZS d dlTmUZU W n eVy ZW zeVdeWdZW[Www dZXed ZYed ZZ	 e[eZB Z\	 ddddZ]de^d < G d!d" d"eAd#d$Z_e	d#d%G d&d' d'eDZ`e	G d(d) d)eFZad0d.d/ZbdS )1    )annotations)AsyncIterableAsyncIterator)asynccontextmanager)	dataclassfield)datetimetimezone)AnyLiteralcastoverload)assert_never   )ModelHTTPErrorUnexpectedModelBehavior_utilsusage)
RunContext)$split_content_into_text_and_thinking)guard_tool_call_idnow_utc)	UserError)AudioUrlBinaryContentBuiltinToolCallPartBuiltinToolReturnPart
CachePointDocumentUrlFilePartFinishReasonImageUrlModelMessageModelRequestModelResponseModelResponsePartModelResponseStreamEventRetryPromptPartSystemPromptPartTextPartThinkingPartToolCallPartToolReturnPartUserPromptPartVideoUrl)ModelProfileModelProfileSpec)Providerinfer_provider)ModelSettings)ToolDefinition   )ModelModelRequestParametersStreamedResponsecheck_allow_model_requestsN)
AsyncInferenceClientChatCompletionInputMessageChatCompletionInputMessageChunkChatCompletionInputToolChatCompletionInputToolCallChatCompletionInputURLChatCompletionOutputChatCompletionOutputMessageChatCompletionStreamOutput TextGenerationOutputFinishReason)HfHubHTTPErroru   Please install `huggingface_hub` to use Hugging Face Inference Providers, you can use the `huggingface` optional group — `pip install "pydantic-ai-slim[huggingface]"`)HuggingFaceModelHuggingFaceModelSettings)systemuser)zdeepseek-ai/DeepSeek-R1z!meta-llama/Llama-3.3-70B-Instructz-meta-llama/Llama-4-Maverick-17B-128E-Instructz)meta-llama/Llama-4-Scout-17B-16E-InstructzQwen/QwQ-32BzQwen/Qwen2.5-72B-InstructzQwen/Qwen3-235B-A22BzQwen/Qwen3-32Blengthstop)rI   	eos_tokenstop_sequencez4dict[TextGenerationOutputFinishReason, FinishReason]_FINISH_REASON_MAPc                   @  s   e Zd ZdZdS )rF   z/Settings used for a Hugging Face model request.N)__name__
__module____qualname____doc__ rR   rR   d/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/models/huggingface.pyrF   k   s    rF   F)total)initc                      s  e Zd ZU dZeddZded< eddZded< eddZded	< d
ddddU fddZ	e
dVddZe
dWddZdXd"d#Ze	dYdZd'd(Zed[d-d.Zed\d1d.Zd]d4d.Zd^d6d7Zd_d9d:Zd`d<d=Zdad?d@ZedbdDdEZedcdIdJZdddNdOZededSdTZ  ZS )frE   a
  A model that uses Hugging Face Inference Providers.

    Internally, this uses the [HF Python client](https://github.com/huggingface/huggingface_hub) to interact with the API.

    Apart from `__init__`, all methods are private or match those of the base class.
    F)reprr:   clientstr_model_namezProvider[AsyncInferenceClient]	_providerhuggingfaceN)providerprofilesettings
model_namer\   7Literal['huggingface'] | Provider[AsyncInferenceClient]r]   ModelProfileSpec | Noner^   ModelSettings | Nonec                  s@   || _ t|trt|}|| _|j| _t j||p|jd dS )a  Initialize a Hugging Face model.

        Args:
            model_name: The name of the Model to use. You can browse available models [here](https://huggingface.co/models?pipeline_tag=text-generation&inference_provider=all&sort=trending).
            provider: The provider to use for Hugging Face Inference Providers. Can be either the string 'huggingface' or an
                instance of `Provider[AsyncInferenceClient]`. If not provided, the other parameters will be used.
            profile: The model profile to use. Defaults to a profile picked by the provider based on the model name.
            settings: Model-specific settings that will be used as defaults for this model.
        )r^   r]   N)	rY   
isinstancerX   r2   rZ   rW   super__init__model_profile)selfr_   r\   r]   r^   	__class__rR   rS   re      s   
zHuggingFaceModel.__init__returnHuggingFaceModelNamec                 C     | j S )zThe model name.rY   rg   rR   rR   rS   r_         zHuggingFaceModel.model_namec                 C  s   | j jS )zThe system / model provider.)rZ   namern   rR   rR   rS   rG      s   zHuggingFaceModel.systemmessageslist[ModelMessage]model_settingsmodel_request_parametersr7   r$   c                   sF   t   | ||\}}| |dtt|pi |I d H }| |}|S )NF)r9   prepare_request_completions_creater   rF   _process_response)rg   rq   rs   rt   responsemodel_responserR   rR   rS   request   s   

zHuggingFaceModel.requestrun_contextRunContext[Any] | NoneAsyncIterator[StreamedResponse]c                 C sP   t   | ||\}}| |dtt|pi |I d H }| ||I d H V  d S )NT)r9   ru   rv   r   rF   _process_streamed_response)rg   rq   rs   rt   r{   rx   rR   rR   rS   request_stream   s   
zHuggingFaceModel.request_streamstreamLiteral[True]rF   )AsyncIterable[ChatCompletionStreamOutput]c                      d S NrR   rg   rq   r   rs   rt   rR   rR   rS   rv         z$HuggingFaceModel._completions_createLiteral[False]r@   c                   r   r   rR   r   rR   rR   rS   rv      r   bool@ChatCompletionOutput | AsyncIterable[ChatCompletionStreamOutput]c           	        s2  |  |}|sd }n|jsd}nd}|jrtd| ||I d H }zE| jjjj| j	|||p0d ||
dd |
dd |
dd |
dd |
dd |
d	d |
d
d |
dd |
dd |
ddI d H W S  tjy } zt|j| j|jd|d }~w ty } zt|jj| j|jjd|d }~ww )Nrequiredautoz+HuggingFace does not support built-in toolsstop_sequencestemperaturetop_pseedpresence_penaltyfrequency_penalty
logit_biaslogprobstop_logprobs
extra_body)modelrq   toolstool_choicer   rJ   r   r   r   r   r   r   r   r   r   )status_coder_   body)
_get_toolsallow_text_outputbuiltin_toolsr   _map_messagesrW   chatcompletionscreaterY   getaiohttpClientResponseErrorr   statusr_   response_error_payloadrD   rx   r   content)	rg   rq   r   rs   rt   r   r   hf_messageserR   rR   rS   rv      s^   










rx   c              
   C  s   |j rtj|j tjd}nt }|jd }|jj}|jj	}g }|r+|
t|| jj |durC|D ]}|t|jj|jj|jd q1|j}d|i}	ttt|d}
t|t||j||j| jj|
|	dS )zAProcess a non-streamed response, and prepare a message to return.tzr   N)tool_call_idfinish_reason)partsr   r_   	timestampprovider_response_idprovider_namer   provider_details)createdr   fromtimestampr	   utc_now_utcchoicesmessager   
tool_callsextendr   r]   thinking_tagsappendr+   functionrp   	argumentsidr   rM   r   r   rC   r$   
_map_usager   rZ   )rg   rx   r   choicer   r   itemscraw_finish_reasonr   r   rR   rR   rS   rw     s2   
 z"HuggingFaceModel._process_responser8   c              	     sX   t |}| I dH }t|t jrtdt||j| j|t	j
|jtjd| jjdS )zHProcess a streamed response, and prepare a streaming response to return.Nz5Streamed response ended without content or tool callsr   )rt   rY   _model_profile	_response
_timestamp_provider_name)r   PeekableAsyncStreampeekrc   Unsetr   HuggingFaceStreamedResponser   r]   r   r   r   r	   r   rZ   rp   )rg   rx   rt   peekable_responsefirst_chunkrR   rR   rS   r~   .  s   
z+HuggingFaceModel._process_streamed_responselist[ChatCompletionInputTool]c                   s    fdd|j  D S )Nc                   s   g | ]}  |qS rR   )_map_tool_definition).0rrn   rR   rS   
<listcomp>C  s    z/HuggingFaceModel._get_tools.<locals>.<listcomp>)	tool_defsvalues)rg   rt   rR   rn   rS   r   B  s   zHuggingFaceModel._get_tools>list[ChatCompletionInputMessage | ChatCompletionOutputMessage]c              	     sJ  g }|D ]}t |tr| |2 z3 dH W }|| q6 qt |trg }g }|jD ]F}t |tr9||j q+t |trG|| 	| q+t |t
r_| jj\}}	|d||j|	g q+t |ttB rgq+t |trmq+t| q+tdd}
|rd||
d< |r||
d< ||
 qt| q| || }r|dt|d	d
 |S )zTJust maps a `pydantic_ai.Message` to a `huggingface_hub.ChatCompletionInputMessage`.N
	assistant)rolez

r   r   r   rG   )r   r   )rc   r#   _map_user_messager   r$   r   r)   r   r+   _map_tool_callr*   r]   r   joinr   r   r   r   r;   _get_instructionsinsert)rg   rq   rt   r   r   itemtextsr   	start_tagend_tagmessage_paraminstructionsrR   rR   rS   r   E  sB   









zHuggingFaceModel._map_messagestr+   r>   c                 C  s$   t t| dd| j|  ddS )Nr   r   )rp   r   )r   typer   )r>   parse_obj_as_instance_guard_tool_call_id	tool_nameargs_as_json_strr   rR   rR   rS   r   o  s   zHuggingFaceModel._map_tool_callfr4   r=   c                 C  s"   t d| j| j| jdd}|S )Nr   )rp   description
parameters)r   r   )r=   r   rp   r   parameters_json_schema)r   
tool_paramrR   rR   rS   r   |  s   
z%HuggingFaceModel._map_tool_definitionr   r#   GAsyncIterable[ChatCompletionInputMessage | ChatCompletionOutputMessage]c                 C s   |j D ]`}t|trtd|jdV  qt|tr%| |I d H V  qt|tr:t	dt
|d| dV  qt|tr`|jd u rPtd| dV  qtdt
|d| dV  qt| qd S )NrG   r   r   toolr   )r   r   r   rH   )r   rc   r(   r;   r   r   r-   _map_user_promptr,   rA   r   model_response_strr'   r   ry   r   )rg   r   partrR   rR   rS   r     s4   






z"HuggingFaceModel._map_user_messager   r-   r;   c                   s  t | jtr| j}nqg }| jD ]k}t |tr!|td|d qt |tr6t|jd}|td|d qt |trV|j	rNt|j
d}|td|d qtd|j t |tr_tdt |trhtdt |trqtd	t |trwqt| qtd
|dS )Ntext)r   r   )url	image_url)r   r   z!Unsupported binary content type: z*AudioUrl is not supported for Hugging Facez-DocumentUrl is not supported for Hugging Facez*VideoUrl is not supported for Hugging FacerH   r   )rc   r   rX   r   r<   r!   r?   r   r   is_imagedata_uriRuntimeError
media_typer   NotImplementedErrorr   r.   r   r   r;   )r   r   r   r   rR   rR   rS   r     s2   








z!HuggingFaceModel._map_user_prompt)r_   rX   r\   r`   r]   ra   r^   rb   )rj   rk   rj   rX   )rq   rr   rs   rb   rt   r7   rj   r$   r   )
rq   rr   rs   rb   rt   r7   r{   r|   rj   r}   )
rq   rr   r   r   rs   rF   rt   r7   rj   r   )
rq   rr   r   r   rs   rF   rt   r7   rj   r@   )
rq   rr   r   r   rs   rF   rt   r7   rj   r   )rx   r@   rj   r$   )rx   r   rt   r7   rj   r8   )rt   r7   rj   r   )rq   rr   rt   r7   rj   r   )r   r+   rj   r>   )r   r4   rj   r=   )r   r#   rj   r   )r   r-   rj   r;   )rN   rO   rP   rQ   r   rW   __annotations__rY   rZ   re   propertyr_   rG   rz   r   r   r   rv   rw   r~   r   r   staticmethodr   r   r   r   __classcell__rR   rR   rh   rS   rE   r   sB   
 


4
"

*
 rE   c                   @  sn   e Zd ZU dZded< ded< ded< ded	< ded
< dddZedddZedddZedddZ	dS )r   z=Implementation of `StreamedResponse` for Hugging Face models.rX   rY   r/   r   r   r   r   r   r   rj   'AsyncIterator[ModelResponseStreamEvent]c              	   C s  | j 2 z|3 d H W }|  jt|7  _|jr|j| _z|jd }W n	 ty*   Y qw |j }r?d|i| _t	
tt|d | _|jj}|rZ| jjd|| jj| jjd}|d urZ|V  |jjp_g D ]}| jj|j|jom|jj|jos|jj|jd}|d ur|V  q`q6 d S )Nr   r   r   )vendor_part_idr   r   ignore_leading_whitespace)r  r   argsr   )r   _usager   r   r   r   
IndexErrorr   r   rM   r   r   rC   deltar   _parts_managerhandle_text_deltar   r   "ignore_streamed_leading_whitespacer   handle_tool_call_deltaindexr   rp   r   )rg   chunkr   r   r   maybe_eventdtcrR   rR   rS   _get_event_iterator  sJ   


z/HuggingFaceStreamedResponse._get_event_iteratorc                 C  rl   )z#Get the model name of the response.rm   rn   rR   rR   rS   r_     ro   z&HuggingFaceStreamedResponse.model_namec                 C  rl   )zGet the provider name.)r   rn   rR   rR   rS   r     ro   z)HuggingFaceStreamedResponse.provider_namec                 C  rl   )z"Get the timestamp of the response.)r   rn   rR   rR   rS   r     ro   z%HuggingFaceStreamedResponse.timestampN)rj   r  r   )rj   r   )
rN   rO   rP   rQ   r   r  r  r_   r   r   rR   rR   rR   rS   r     s   
 
(r   rx   1ChatCompletionOutput | ChatCompletionStreamOutputrj   usage.RequestUsagec                 C  s(   | j }|d u rt  S t j|j|jdS )N)input_tokensoutput_tokens)r   RequestUsageprompt_tokenscompletion_tokens)rx   response_usagerR   rR   rS   r     s   r   )rx   r  rj   r  )c
__future__r   _annotationscollections.abcr   r   
contextlibr   dataclassesr   r   r   r	   typingr
   r   r   r   typing_extensionsr    r   r   r   r   _run_contextr   _thinking_partr   r   r   r   r   
exceptionsr   rq   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   profilesr/   r0   	providersr1   r2   r^   r3   r   r4   r6   r7   r8   r9   r   huggingface_hubr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   huggingface_hub.errorsrD   ImportError_import_error__all__HFSystemPromptRoleLatestHuggingFaceModelNamesrX   rk   rM   r   rF   rE   r   r   rR   rR   rR   rS   <module>   sd    `0
  ZA