o
    i`                  
   @  sH  d dl mZ d dlZd dlmZ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 d	dlmZ d	dl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*m+Z+m,Z,m-Z-m.Z.m/Z/ d	dl0m1Z1m2Z2 d	dl3m4Z4m5Z5 d	dl6m7Z7 ddlm8Z8m9Z9m:Z:m;Z;m<Z< zHd dl=m>Z>m?Z? d dl@mAZBm9ZC d dlDmEZEmFZF d dlGmHZHmIZI d dlJmKZKmLZLmMZM d dlNmOZOmPZP d dlQmRZRmSZS d dlTmUZU d dlVm?ZW W n eXy ZY zeXdeYdZY[Yww erd dlZZZd dl[m\Z\ d dl]Z]e
ddG d d! d!e9Z^e
G d"d# d#e;Z_dS )$    )annotationsN)AsyncIterableAsyncIteratorSequence)asynccontextmanager)	dataclassreplace)datetimetimezone)TYPE_CHECKINGAnyLiteralcast)assert_never   )UnexpectedModelBehavior_utils)
RunContext)$split_content_into_text_and_thinking)	UserError)BinaryContentBuiltinToolCallPartBuiltinToolReturnPartFilePartImageUrlModelMessageModelRequestModelResponseModelResponsePartModelResponseStreamEventRetryPromptPartSystemPromptPartTextPartThinkingPartToolCallPartToolReturnPartUserPromptPart)ModelProfileModelProfileSpec)Providerinfer_provider)ModelSettings   )DownloadedItemModelModelRequestParametersStreamedResponsedownload_item)ChatImage)
AsyncModelr.   )LlamaCppfrom_llamacpp)MLXLM
from_mlxlm)AsyncSGLangSGLangfrom_sglang)Transformersfrom_transformers)VLLMOfflinefrom_vllm_offline)
JsonSchema)r3   u   Please install `outlines` to use the Outlines model, you can use the `outlines` optional group — `pip install "pydantic-ai-slim[outlines]"`F)initc                      sZ  e Zd ZdZddddd^ fddZeddddd_ddZeddddd`ddZedddddaddZe		dbdddddcd$d%Z	eddddddd(d)Z
eded+d,Zeded-d.Zdfd5d6Ze	dgdhd:d;Zdid=d>Zdjd@dAZdkdBdCZdkdDdEZdkdFdGZdkdHdIZdkdJdKZdldMdNZdmdSdTZdndVdWZdodZd[Zdpd\d]Z  ZS )qOutlinesModelzHA model that relies on the Outlines library to run non API-based models.outlinesNproviderprofilesettingsmodel*OutlinesBaseModel | OutlinesAsyncBaseModelrE   1Literal['outlines'] | Provider[OutlinesBaseModel]rF   ModelProfileSpec | NonerG   ModelSettings | Nonec                  s8   || _ d| _t|trt|}t j||p|jd dS )a  Initialize an Outlines model.

        Args:
            model: The Outlines model used for the model.
            provider: The provider to use for OutlinesModel. Can be either the string 'outlines' or an
                instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used.
            profile: The model profile to use. Defaults to a profile picked by the provider.
            settings: Default model settings for this model instance.
        zoutlines-model)rG   rF   N)rH   _model_name
isinstancestrr*   super__init__model_profile)selfrH   rE   rF   rG   	__class__ a/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/models/outlines.pyrQ   S   s
   
zOutlinesModel.__init__hf_model+transformers.modeling_utils.PreTrainedModelhf_tokenizer_or_processorbtransformers.tokenization_utils.PreTrainedTokenizer | transformers.processing_utils.ProcessorMixinc                C     t ||}| ||||dS )a  Create an Outlines model from a Hugging Face model and tokenizer.

        Args:
            hf_model: The Hugging Face PreTrainedModel or any model that is compatible with the
                `transformers` API.
            hf_tokenizer_or_processor: Either a HuggingFace `PreTrainedTokenizer` or any tokenizer that is compatible
                with the `transformers` API, or a HuggingFace processor inheriting from `ProcessorMixin`. If a
                tokenizer is provided, a regular model will be used, while if you provide a processor, it will be a
                multimodal model.
            provider: The provider to use for OutlinesModel. Can be either the string 'outlines' or an
                instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used.
            profile: The model profile to use. Defaults to a profile picked by the provider.
            settings: Default model settings for this model instance.
        rD   )r=   )clsrX   rZ   rE   rF   rG   outlines_modelrV   rV   rW   r=   l   s   
zOutlinesModel.from_transformersllama_modelllama_cpp.Llamac                C     t |}| ||||dS )a  Create an Outlines model from a LlamaCpp model.

        Args:
            llama_model: The llama_cpp.Llama model to use.
            provider: The provider to use for OutlinesModel. Can be either the string 'outlines' or an
                instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used.
            profile: The model profile to use. Defaults to a profile picked by the provider.
            settings: Default model settings for this model instance.
        rD   )r6   )r]   r_   rE   rF   rG   r^   rV   rV   rW   r6         zOutlinesModel.from_llamacpp	mlx_model	nn.Modulemlx_tokenizer3transformers.tokenization_utils.PreTrainedTokenizerc                C  r\   )a+  Create an Outlines model from a MLXLM model.

        Args:
            mlx_model: The nn.Module model to use.
            mlx_tokenizer: The PreTrainedTokenizer to use.
            provider: The provider to use for OutlinesModel. Can be either the string 'outlines' or an
                instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used.
            profile: The model profile to use. Defaults to a profile picked by the provider.
            settings: Default model settings for this model instance.
        rD   )r8   )r]   rc   re   rE   rF   rG   r^   rV   rV   rW   r8      s   
zOutlinesModel.from_mlxlmbase_urlrO   api_key
str | None
model_namec             
   C  sZ   zddl m} W n ty } ztd|d}~ww |||d}	t|	|}
| |
|||dS )a  Create an Outlines model to send requests to an SGLang server.

        Args:
            base_url: The url of the SGLang server.
            api_key: The API key to use for authenticating requests to the SGLang server.
            model_name: The name of the model to use.
            provider: The provider to use for OutlinesModel. Can be either the string 'outlines' or an
                instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used.
            profile: The model profile to use. Defaults to a profile picked by the provider.
            settings: Default model settings for this model instance.
        r   )AsyncOpenAIu   Please install `openai` to use the Outlines SGLang model, you can use the `openai` optional group — `pip install "pydantic-ai-slim[openai]"`N)rg   rh   rD   )openairk   ImportErrorr;   )r]   rg   rh   rj   rE   rF   rG   rk   _import_erroropenai_clientr^   rV   rV   rW   r;      s   
zOutlinesModel.from_sglang
vllm_modelr   c                C  ra   )a  Create an Outlines model from a vLLM offline inference model.

        Args:
            vllm_model: The vllm.LLM local model to use.
            provider: The provider to use for OutlinesModel. Can be either the string 'outlines' or an
                instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used.
            profile: The model profile to use. Defaults to a profile picked by the provider.
            settings: Default model settings for this model instance.
        rD   )r?   )r]   rp   rE   rF   rG   r^   rV   rV   rW   r?      rb   zOutlinesModel.from_vllm_offlinereturnc                 C     | j S NrM   rS   rV   rV   rW   rj      s   zOutlinesModel.model_namec                 C  s   dS )NrC   rV   ru   rV   rV   rW   system   s   zOutlinesModel.systemmessageslist[ModelMessage]model_settingsmodel_request_parametersr/   r   c                   sx   |  ||\}}	 | |||I d H \}}}t| jtr,| j||d fi |I d H }n| j||d fi |}| |S rs   )prepare_request_build_generation_argumentsrN   rH   OutlinesAsyncBaseModel_process_response)rS   rw   ry   rz   promptoutput_typeinference_kwargsresponserV   rV   rW   request   s   
zOutlinesModel.requestrun_contextRunContext[Any] | NoneAsyncIterator[StreamedResponse]c           	       s   |  ||\}}| |||I d H \}}}t| jtr4| jj||d fi | |  |I d H V  d S | jj||d fi |  fdd}| | |I d H V  d S )Nc                   s    D ]} | V  qd S rs   rV   )chunkr   rV   rW   async_response   s   z4OutlinesModel.request_stream.<locals>.async_response)r{   r|   rN   rH   r}   stream_process_streamed_response)	rS   rw   ry   rz   r   r   r   r   r   rV   r   rW   request_stream	  s   zOutlinesModel.request_stream.tuple[Chat, JsonSchema | None, dict[str, Any]]c                   sZ   |j s
|js
|jrtd|jrt|jj}nd}| ||I dH }| |}|||fS )z-Build the generation arguments for the model.z?Outlines does not support function tools and builtin tools yet.N)	function_toolsbuiltin_toolsoutput_toolsr   output_objectr@   json_schema_format_promptformat_inference_kwargs)rS   rw   ry   rz   r   r   r   rV   rV   rW   r|   &  s   

z)OutlinesModel._build_generation_argumentsdict[str, Any]c                 C  s   |rt |ni }t| jtr| |}n1t| jtr | |}n%t| jtr,| |}nt| jt	t
B r:| |}nt| jtrE| |}|di }|| |S )z3Format the model settings for the inference kwargs.
extra_body)dictrN   rH   r<   %_format_transformers_inference_kwargsr5   "_format_llama_cpp_inference_kwargsr7   _format_mlxlm_inference_kwargsr:   r9   _format_sglang_inference_kwargsr>   %_format_vllm_offline_inference_kwargspopupdate)rS   ry   settings_dictr   rV   rV   rW   r   >  s   

z%OutlinesModel.format_inference_kwargsc                      g d} fdd|D }|S )z>Select the model settings supported by the Transformers model.)
max_tokenstemperaturetop_p
logit_biasr   c                      i | ]}| v r| | qS rV   rV   .0kry   rV   rW   
<dictcomp>[      zGOutlinesModel._format_transformers_inference_kwargs.<locals>.<dictcomp>rV   rS   ry   supported_argsfiltered_settingsrV   r   rW   r   R  s   z3OutlinesModel._format_transformers_inference_kwargsc                   r   )z:Select the model settings supported by the LlamaCpp model.r   r   r   seedpresence_penaltyfrequency_penaltyr   r   c                   r   rV   rV   r   r   rV   rW   r   k  r   zDOutlinesModel._format_llama_cpp_inference_kwargs.<locals>.<dictcomp>rV   r   rV   r   rW   r   _  s   
z0OutlinesModel._format_llama_cpp_inference_kwargsc                   s   dg} fdd|D }|S )z7Select the model settings supported by the MLXLM model.r   c                   r   rV   rV   r   r   rV   rW   r   v  r   z@OutlinesModel._format_mlxlm_inference_kwargs.<locals>.<dictcomp>rV   r   rV   r   rW   r   o  s   z,OutlinesModel._format_mlxlm_inference_kwargsc                   r   )z8Select the model settings supported by the SGLang model.)r   r   r   r   r   r   c                   r   rV   rV   r   r   rV   rW   r     r   zAOutlinesModel._format_sglang_inference_kwargs.<locals>.<dictcomp>rV   r   rV   r   rW   r   z  s   z-OutlinesModel._format_sglang_inference_kwargsc                 C  s`   ddl m} g d}|di d| }|D ]}t||||d qd|i|di }|S )z=Select the model settings supported by the vLLMOffline model.r   )SamplingParamsr   r   sampling_paramsN)vllm.sampling_paramsr   getr   setattr)rS   ry   r   r   r   keyr   rV   rV   rW   r     s   
z3OutlinesModel._format_vllm_offline_inference_kwargsr2   c                   sz  t  }| || }r|| |D ]&}t|tr|jD ]}t|tr,||j qt|trt|jt	r>|
|j qt|jtrg }|jD ]I}t|t	rV|| qIt|trwt|dddI dH }	| |	d |	d }
|t|
 qIt|tr|jr| |j|j}
|t|
 qItd|
| qt|j qt|tr|
|  qt|trtdt| qqt|tr6g }g }|jD ]M}t|tr||j qt|trqt|ttB tB rtdt|t rt|jtr|jjr| |jj|jj}
|t|
 qtd	t| qt!|d
kr,t!|dkr,|"|d  q|"g || qt| q|S )z7Turn the model messages into an Outlines Chat instance.bytesmime)data_formattype_formatNdata	data_typezXEach element of the content sequence must be a string, an `ImageUrl` or a `BinaryImage`.z5Tool calls are not supported for Outlines models yet.zNFile parts other than `BinaryImage` are not supported for Outlines models yet.r,   r   )#r2   _get_instructionsadd_system_messagerN   r   partsr!   contentr&   rO   add_user_messager   appendr   r1   _create_PIL_imager3   r   is_imager   
media_typer   r   r    model_responser%   r   r"   r#   r$   r   r   r   lenadd_assistant_message)rS   rw   rz   chatinstructionsmessagepartoutlines_inputitemimage_contentimage
text_partsimage_partsrV   rV   rW   r     sx   











"




zOutlinesModel._format_promptr   r   r   PILImage.Imagec                 C  s$   t t|}|dd |_|S )z/Create a PIL Image from the data and data type./)PILImageopenioBytesIOsplitformat)rS   r   r   r   rV   rV   rW   r     s   zOutlinesModel._create_PIL_imager   c                 C  s   t ttt t|| jjdS )zKTurn the Outlines text response into a Pydantic AI model response instance.)r   )r   r   listr   r   rF   thinking_tags)rS   r   rV   rV   rW   r~     s
   zOutlinesModel._process_responseAsyncIterable[str]r0   c                   sT   t |}| I dH }t|t jrtdtjtj	d}t
|| j| j||ddS )zNTurn the Outlines text response into a Pydantic AI streamed response instance.Nz5Streamed response ended without content or tool calls)tzrC   )rz   rM   _model_profile	_response
_timestamp_provider_name)r   PeekableAsyncStreampeekrN   Unsetr   r	   nowr
   utcOutlinesStreamedResponserM   rF   )rS   r   rz   peekable_responsefirst_chunk	timestamprV   rV   rW   r     s   
z(OutlinesModel._process_streamed_responsec                 C  s   |j dv rt|ddS |S )z5Customize the model request parameters for the model.)autonativeprompted)output_mode)r   r   )rS   rz   rV   rV   rW   customize_request_parameters  s   
z*OutlinesModel.customize_request_parameters)rH   rI   rE   rJ   rF   rK   rG   rL   )
rX   rY   rZ   r[   rE   rJ   rF   rK   rG   rL   )r_   r`   rE   rJ   rF   rK   rG   rL   )
rc   rd   re   rf   rE   rJ   rF   rK   rG   rL   )NN)rg   rO   rh   ri   rj   ri   rE   rJ   rF   rK   rG   rL   )rp   r   rE   rJ   rF   rK   rG   rL   rq   rO   )rw   rx   ry   rL   rz   r/   rq   r   rs   )
rw   rx   ry   rL   rz   r/   r   r   rq   r   )rw   rx   ry   rL   rz   r/   rq   r   )ry   rL   rq   r   )ry   r   rq   r   )rw   rx   rz   r/   rq   r2   )r   r   r   rO   rq   r   )r   rO   rq   r   )r   r   rz   r/   rq   r0   )rz   r/   rq   r/   )__name__
__module____qualname____doc__rQ   classmethodr=   r6   r8   r;   r?   propertyrj   rv   r   r   r   r|   r   r   r   r   r   r   r   r   r~   r   r   __classcell__rV   rV   rT   rW   rB   O   sl    !









J

rB   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   z9Implementation of `StreamedResponse` for Outlines models.rO   rM   r'   r   r   r   r	   r   r   rq   'AsyncIterator[ModelResponseStreamEvent]c                 C sF   | j 2 z3 d H W }| jjd|| jj| jjd}|d ur|V  q6 d S )Nr   )vendor_part_idr   r   ignore_leading_whitespace)r   _parts_managerhandle_text_deltar   r   "ignore_streamed_leading_whitespace)rS   eventrV   rV   rW   _get_event_iterator$  s   z,OutlinesStreamedResponse._get_event_iteratorc                 C  rr   )z#Get the model name of the response.rt   ru   rV   rV   rW   rj   /     z#OutlinesStreamedResponse.model_namec                 C  rr   )zGet the provider name.)r   ru   rV   rV   rW   provider_name4  r  z&OutlinesStreamedResponse.provider_namec                 C  rr   )z"Get the timestamp of the response.)r   ru   rV   rV   rW   r   9  r  z"OutlinesStreamedResponse.timestampN)rq   r   r   )rq   r	   )
r   r   r   r   __annotations__r  r   rj   r  r   rV   rV   rV   rW   r     s   
 
r   )`
__future__r   r   collections.abcr   r   r   
contextlibr   dataclassesr   r   r	   r
   typingr   r   r   r   typing_extensionsr    r   r   _run_contextr   _thinking_partr   
exceptionsr   rw   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   profilesr'   r(   	providersr)   r*   rG   r+   r-   r.   r/   r0   r1   outlines.inputsr2   r3   outlines.models.baser4   r}   OutlinesBaseModeloutlines.models.llamacppr5   r6   outlines.models.mlxlmr7   r8   outlines.models.sglangr9   r:   r;   outlines.models.transformersr<   r=   outlines.models.vllm_offliner>   r?   outlines.types.dslr@   PILr   rm   rn   	llama_cppmlx.nnnntransformersrB   r   rV   rV   rV   rW   <module>   s\   L   M