o
    i_u                  
   @  s  U d dl m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 d dlmZmZmZmZ d dl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 d
dlm Z m!Z! d
dl"m#Z# d
dl$m%Z% d
dlm&Z&m'Z(m)Z) d
dl*m+Z+ d
dl,m-Z- d
dl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA d
dlBmCZCmDZD d
dlEmFZF d
dlGmHZHmIZI d
dlJmKZK d
dlLmMZM ddlmNZNmOZOmPZPmQZQmRZR z"d dlSmTZTmUZUmVZVmWZWmXZX d dlYmZZZ d dl[m\Z\ d dl]m^Z^ W n e_y Z` ze_de`dZ`[`ww ed  Za	 ed! Zb	 eceaB ebB Zd	 d"d#d$d%d$d&Zed'efd(< G d)d* d*eKd+d,Zge
d+d-G d.d/ d/eNZhe
G d0d1 d1ePZidId6d7ZjG d8d9 d9eZkG d:d; d;eZlG d<d= d=eZm	dJdKdGdHZndS )L    )annotations)AsyncIterableAsyncIteratorIterable)asynccontextmanager)	dataclassfield)datetime)AnyLiteralcastoverload)	BaseModelJsonValidationError)	from_json)assert_never   )ModelHTTPErrorUnexpectedModelBehavior_utilsusage)DEFAULT_OUTPUT_TOOL_NAMEOutputObjectDefinition)
RunContext)$split_content_into_text_and_thinking)generate_tool_call_idguard_tool_call_idnumber_to_datetime)WebSearchTool)	UserError)BinaryContentBuiltinToolCallPartBuiltinToolReturnPartDocumentUrlFilePartFinishReasonImageUrlModelMessageModelRequestModelResponseModelResponsePartModelResponseStreamEventRetryPromptPartSystemPromptPartTextPartThinkingPartToolCallPartToolReturnPartUserPromptPart)ModelProfileModelProfileSpec)GroqModelProfile)Providerinfer_provider)ModelSettings)ToolDefinition   )ModelModelRequestParametersStreamedResponsecheck_allow_model_requestsget_user_agent)	NOT_GIVENAPIErrorAPIStatusError	AsyncGroqAsyncStream)chat)ImageURL)ExecutedToolu}   Please install `groq` to use the Groq model, you can use the `groq` optional group — `pip install "pydantic-ai-slim[groq]"`N)	zdistil-whisper-large-v3-enzgemma2-9b-itzllama-3.3-70b-versatilezllama-3.1-8b-instantzllama-guard-3-8bzllama3-70b-8192zllama3-8b-8192zwhisper-large-v3zwhisper-large-v3-turbo)z
playai-ttszplayai-tts-arabiczqwen-qwq-32bzmistral-saba-24bzqwen-2.5-coder-32bzqwen-2.5-32bzdeepseek-r1-distill-qwen-32bzdeepseek-r1-distill-llama-70bzllama-3.3-70b-specdeczllama-3.2-1b-previewzllama-3.2-3b-previewzllama-3.2-11b-vision-previewzllama-3.2-90b-vision-previewzmoonshotai/kimi-k2-instructstoplength	tool_callcontent_filter)rI   rJ   
tool_callsrL   function_callz^dict[Literal['stop', 'length', 'tool_calls', 'content_filter', 'function_call'], FinishReason]_FINISH_REASON_MAPc                   @  s   e Zd ZU dZded< dS )GroqModelSettingsz'Settings used for a Groq model request.z"Literal['hidden', 'raw', 'parsed']groq_reasoning_formatN)__name__
__module____qualname____doc____annotations__ rW   rW   ]/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/models/groq.pyrP   r   s   
 rP   F)total)initc                      sD  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d^ fddZ	e
d_ddZe
d`ddZe
d_ddZdad$d%Ze	dbdcd)d*Zeddd/d0Zeded3d0Zdfd6d0Zdgd8d9Zdhd;d<Zdid>d?Zdid@dAZdjdCdDZedkdHdIZedldMdNZdmdRdSZedndWdXZedod\d]Z  ZS )p	GroqModelzA model that uses the Groq API.

    Internally, this uses the [Groq Python client](https://github.com/groq/groq-python) to interact with the API.

    Apart from `__init__`, all methods are private or match those of the base class.
    F)reprrD   clientGroqModelName_model_namezProvider[AsyncGroq]	_providergroqN)providerprofilesettings
model_namerb   0Literal['groq', 'gateway'] | Provider[AsyncGroq]rc   ModelProfileSpec | Nonerd   ModelSettings | Nonec                  sL   || _ t|trt|dkrdn|}|| _|j| _t j||p!|jd dS )a  Initialize a Groq model.

        Args:
            model_name: The name of the Groq model to use. List of model names available
                [here](https://console.groq.com/docs/models).
            provider: The provider to use for authentication and API access. Can be either the string
                'groq' or an instance of `Provider[AsyncGroq]`. If not provided, a new provider will be
                created using the other parameters.
            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.
        gatewayzgateway/groq)rd   rc   N)	r_   
isinstancestrr8   r`   r]   super__init__model_profile)selfre   rb   rc   rd   	__class__rW   rX   rm      s   
zGroqModel.__init__returnrk   c                 C  s   t | jjS N)rk   r]   base_urlro   rW   rW   rX   rt      s   zGroqModel.base_urlc                 C     | j S )zThe model name.r_   ru   rW   rW   rX   re         zGroqModel.model_namec                 C  s   | j jS )zThe model provider.)r`   nameru   rW   rW   rX   system   s   zGroqModel.systemmessageslist[ModelMessage]model_settingsmodel_request_parametersr=   r*   c           	        s   t   | ||\}}z| |dtt|pi |I d H }W nE tyc } z9t|jtr^z(t	
|j}t|jjj|jjjd}t|g|jt | jjddW W  Y d }~S  ty]   Y  w  d }~ww | |}|S )NF)	tool_nameargserror)partsre   	timestampprovider_namefinish_reason)r?   prepare_request_completions_creater   rP   r   rj   bodydict_GroqToolUseFailedErrormodel_validater1   r   failed_generationry   	argumentsr*   re   r   now_utcr`   r   _process_response)	ro   r{   r}   r~   responseer   tool_call_partmodel_responserW   rW   rX   request   sD   
zGroqModel.requestrun_contextRunContext[Any] | NoneAsyncIterator[StreamedResponse]c              	   C s   t   | ||\}}| |dtt|pi |I d H }|4 I d H  | ||I d H V  W d   I d H  d S 1 I d H s>w   Y  d S )NT)r?   r   r   r   rP   _process_streamed_response)ro   r{   r}   r~   r   r   rW   rW   rX   request_stream   s   
.zGroqModel.request_streamstreamLiteral[True]rP   %AsyncStream[chat.ChatCompletionChunk]c                      d S rs   rW   ro   r{   r   r}   r~   rW   rW   rX   r         zGroqModel._completions_createLiteral[False]chat.ChatCompletionc                   r   rs   rW   r   rW   rW   rX   r      r   bool;chat.ChatCompletion | AsyncStream[chat.ChatCompletionChunk]c              
     s  |  |}|| |7 }|sd }n|jsd}nd}| ||}d }|jdkr6|j}	|	d us0J | |	}n|jdkrE|sE| jjrEddi}z|	di }
|

dt  | jjjjd!i d	| jd
|ddd|	dtd|potd|pttd|	dtd|d|ptd|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|
d|	dI d H W S d|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|	dtd|
d|	dI d H W S  ty@ } z|j }dkr;t|| j|jd | d }~ww )"Nrequiredautonativepromptedtypejson_objectextra_headersz
User-Agentmodelr{   nr;   parallel_tool_callstoolstool_choicerI   stop_sequencesr   response_format
max_tokenstemperaturetop_ptimeoutseedpresence_penaltyreasoning_formatrQ   frequency_penalty
logit_bias
extra_bodyi  )status_codere   r   rW   )
_get_tools_get_builtin_toolsallow_text_output_map_messagesoutput_modeoutput_object_map_json_schemarc   supports_json_object_outputget
setdefaultr@   r]   rF   completionscreater_   rA   rC   r   r   re   r   )ro   r{   r   r}   r~   r   r   groq_messagesr   r   r   r   r   rW   rW   rX   r     s   


	

	

r   c              
   C  s  t |j}|jd }g }|jjdur|t|jjd |jjr=|jjD ]}t|| j	\}}|r<|r<|| || q$|jj
rM|t|jj
| jj |jjduri|jjD ]}|t|jj|jj|jd qW|j}	d|	i}
t|	}t|t||j||j| jj||
dS )zAProcess a non-streamed response, and prepare a message to return.r   N)content)r   r   tool_call_idr   )r   r   re   r   provider_response_idr   r   provider_details)r   createdchoicesmessage	reasoningappendr0   executed_tools_map_executed_toolrz   r   extendr   rc   thinking_tagsrM   r1   functionry   r   idr   rO   r   r*   
_map_usager   r`   )ro   r   r   choiceitemstool	call_partreturn_partcraw_finish_reasonr   r   rW   rW   rX   r   ?  s<   



 
zGroqModel._process_responseGroqStreamedResponsec                   sP   t |}| I dH }t|t jrtdt|||j| jt	|j
| jjdS )zHProcess a streamed response, and prepare a streaming response to return.Nz5Streamed response ended without content or tool calls)r~   	_responser_   _model_profile
_timestamp_provider_name)r   PeekableAsyncStreampeekrj   Unsetr   r   r   rc   r   r   r`   ry   )ro   r   r~   peekable_responsefirst_chunkrW   rW   rX   r   b  s   
z$GroqModel._process_streamed_response"list[chat.ChatCompletionToolParam]c                   s    fdd|j  D S )Nc                   s   g | ]}  |qS rW   )_map_tool_definition).0rru   rW   rX   
<listcomp>w  s    z(GroqModel._get_tools.<locals>.<listcomp>)	tool_defsvalues)ro   r~   rW   ru   rX   r   v  s   zGroqModel._get_toolsc                 C  sH   g }|j D ]}t|trt| jjstdqtd|jj	 d|S )Nz(`WebSearchTool` is not supported by Groq`zI` is not supported by `GroqModel`. If it should be, please file an issue.)
builtin_toolsrj   r   r6   from_profilerc   'groq_always_has_web_search_builtin_toolr    rq   rR   )ro   r~   r   r   rW   rW   rX   r   y  s   

zGroqModel._get_builtin_tools%list[chat.ChatCompletionMessageParam]c              	   C  s8  g }|D ]}t |tr|| | qt |trg }g }|jD ]F}t |tr.||j q t |t	r<|| 
| q t |trT| jj\}}	|d||j|	g q t |ttB r\q t |trbq t| q tjdd}
|rvd||
d< |r|||
d< ||
 qt| q| || }r|dtjd|d	 |S )
zOJust maps a `pydantic_ai.Message` to a `groq.types.ChatCompletionMessageParam`.
	assistant)rolez

r   rM   r   rz   r   r   )rj   r)   r   _map_user_messager*   r   r/   r   r   r1   _map_tool_callr0   rc   r   joinr"   r#   r%   r   rF   #ChatCompletionAssistantMessageParam_get_instructionsinsert ChatCompletionSystemMessageParam)ro   r{   r~   r   r   textsrM   item	start_tagend_tagmessage_paraminstructionsrW   rW   rX   r     s<   








zGroqModel._map_messagestr1   'chat.ChatCompletionMessageToolCallParamc                 C  s"   t jt| dd| j|  ddS )Nr  r   )ry   r   )r   r   r   )rF   "ChatCompletionMessageToolCallParam_guard_tool_call_idr   args_as_json_strr
  rW   rW   rX   r     s
   zGroqModel._map_tool_callfr:   chat.ChatCompletionToolParamc                 C  s   d| j | jpd| jddS )Nr    )ry   description
parameters)r   r   )ry   r  parameters_json_schema)r  rW   rW   rX   r     s   zGroqModel._map_tool_definitionor   ,chat.completion_create_params.ResponseFormatc                 C  s4   d|j pt|j|jdd}|jr|j|d d< |S )Njson_schema)ry   schemastrict)r   r  r  )ry   r   r  r  r  )ro   r  response_format_paramrW   rW   rX   r     s   zGroqModel._map_json_schemar   r)   )Iterable[chat.ChatCompletionMessageParam]c                 c  s    |j D ]T}t|trtjd|jdV  qt|tr!| |V  qt|tr5tj	dt
|d| dV  qt|trX|jd u rJtjd| dV  qtj	dt
|d| dV  qd S )Nrz   r   r   r
  )r   r   r   user)r   rj   r.   rF   r  r   r3   _map_user_promptr2   ChatCompletionToolMessageParamr  model_response_strr-   r   ChatCompletionUserMessageParamr   )clsr   partrW   rW   rX   r     s0   







zGroqModel._map_user_messager!  r3   #chat.ChatCompletionUserMessageParamc                 C  s   t | jtr
| j}n\g }| jD ]V}t |tr!|tj|dd qt |tr7t|jd}|tj	|dd qt |t
rT|jrPt|jd}|tj	|dd qtdt |tr]tdtdt| tjd	|d
S )Ntext)r#  r   )url	image_url)r%  r   z5Only images are supported for binary content in Groq.z%DocumentUrl is not supported in Groq.zUnsupported content type: r  r   )rj   r   rk   r   rF   "ChatCompletionContentPartTextParamr'   rG   r$  #ChatCompletionContentPartImageParamr!   is_imagedata_uriRuntimeErrorr$   r   r  )r!  r   r  r%  rW   rW   rX   r    s$   




zGroqModel._map_user_prompt)re   r^   rb   rf   rc   rg   rd   rh   rr   rk   rr   r^   )r{   r|   r}   rh   r~   r=   rr   r*   rs   )
r{   r|   r}   rh   r~   r=   r   r   rr   r   )
r{   r|   r   r   r}   rP   r~   r=   rr   r   )
r{   r|   r   r   r}   rP   r~   r=   rr   r   )
r{   r|   r   r   r}   rP   r~   r=   rr   r   )r   r   rr   r*   )r   r   r~   r=   rr   r   )r~   r=   rr   r   )r{   r|   r~   r=   rr   r   )r  r1   rr   r	  )r  r:   rr   r  )r  r   rr   r  )r   r)   rr   r  )r!  r3   rr   r"  ) rR   rS   rT   rU   r   r]   rV   r_   r`   rm   propertyrt   re   rz   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   classmethodr   r  __classcell__rW   rW   rp   rX   r[   ~   sL   
 
&	
	
<
#


)

r[   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   z5Implementation of `StreamedResponse` for Groq models.r^   r_   r4   r   z'AsyncIterable[chat.ChatCompletionChunk]r   r	   r   rk   r   rr   'AsyncIterator[ModelResponseStreamEvent]c                 C sN  zd }d}d}| j 2 z3 d H W }|  jt|7  _|jr!|j| _z|jd }W n	 ty1   Y qw |j }rBd|i| _t	
|| _|jjd ur_|sP|d7 }d}| jjd| |jjdV  nd}|jjr|jjD ]2}t|| jd|d\}}	|r|j}| jjd	|j d
|dV  |	rd }| jjd	|j d|	dV  qi|jj}
|
r| jjd|
| jj| jjd}|d ur|V  |jjpg D ]}| jj|j|jo|jj|jo|jj|jd}|d ur|V  qq6 W d S  ty& } z8t |j!t"r!zt#$|j!}| jj%d|j&j|j&jdV  W W Y d }~d S  t'y  } zW Y d }~ d }~ww  d }~ww )Nr   Fr   r;   Tz
reasoning-)vendor_part_idr   )	streamingr   zexecuted_tools-z-call)r2  r!  z-returnr   )r2  r   r   ignore_leading_whitespace)r2  r   r   r   tool_use_failed)r2  r   r   )(r   _usager   r   r   r   
IndexErrorr   r   rO   r   deltar   _parts_managerhandle_thinking_deltar   r   r   r   handle_partindexr   handle_text_deltar   r   "ignore_streamed_leading_whitespacerM   handle_tool_call_deltar   ry   r   rB   rj   r   r   _GroqToolUseFailedInnerErrorr   handle_tool_call_partr   r   )ro   executed_tool_call_idreasoning_indexr   chunkr   r   r   r   r   r   maybe_eventdtcr   r   rW   rW   rX   _get_event_iterator  s   




A
z(GroqStreamedResponse._get_event_iteratorc                 C  rv   )z#Get the model name of the response.rw   ru   rW   rW   rX   re   d  rx   zGroqStreamedResponse.model_namec                 C  rv   )zGet the provider name.)r   ru   rW   rW   rX   r   i  rx   z"GroqStreamedResponse.provider_namec                 C  rv   )z"Get the timestamp of the response.)r   ru   rW   rW   rX   r   n  rx   zGroqStreamedResponse.timestampN)rr   r1  r,  r+  )rr   r	   )
rR   rS   rT   rU   rV   rG  r-  re   r   r   rW   rW   rW   rX   r     s   
 
Vr   
completion.chat.ChatCompletionChunk | chat.ChatCompletionrr   usage.RequestUsagec                 C  sL   d }t | tjr| j}n	| jd ur| jj}|d u rt S tj|j|jdS )N)input_tokensoutput_tokens)rj   rF   ChatCompletionr   x_groqRequestUsageprompt_tokenscompletion_tokens)rH  response_usagerW   rW   rX   r   t  s   
r   c                   @  s   e Zd ZU ded< ded< dS )_GroqToolUseFailedGenerationrk   ry   zdict[str, Any]r   NrR   rS   rT   rV   rW   rW   rW   rX   rS    s   
 rS  c                   @  s.   e Zd ZU ded< ded< ded< ded< d	S )
r@  rk   r   z Literal['invalid_request_error']r   zLiteral['tool_use_failed']codez"Json[_GroqToolUseFailedGeneration]r   NrT  rW   rW   rW   rX   r@    s
   
 r@  c                   @  s   e Zd ZU ded< dS )r   r@  r   NrT  rW   rW   rW   rX   r     s   
 r   r   rH   r   rk   r3  r   r   
str | None?tuple[BuiltinToolCallPart | None, BuiltinToolReturnPart | None]c                 C  s   | j dkrE| jr| jjs| jjr| jjdd}n| j}|pt }ttj	t
| j||d}ttj	|||d}|rA|r=d |fS |d fS ||fS dS )Nsearchjson)mode)r   r   r   r   )r   r   r   r   )NN)r   search_resultsimagesresults
model_dumpoutputr   r"   r   kindr   r   r#   )r   r   r3  r   r]  r   r   rW   rW   rX   r     s.   

r   )rH  rI  rr   rJ  )FN)
r   rH   r   rk   r3  r   r   rV  rr   rW  )o
__future__r   _annotationscollections.abcr   r   r   
contextlibr   dataclassesr   r   r	   typingr
   r   r   r   pydanticr   r   r   pydantic_corer   typing_extensionsr   r  r   r   r   r   _outputr   r   _run_contextr   _thinking_partr   r   r   r  r   r   r   
exceptionsr    r{   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   profilesr4   r5   profiles.groqr6   	providersr7   r8   rd   r9   r   r:   r<   r=   r>   r?   r@   ra   rA   rB   rC   rD   rE   
groq.typesrF   8groq.types.chat.chat_completion_content_part_image_paramrG   'groq.types.chat.chat_completion_messagerH   ImportError_import_errorProductionGroqModelNamesPreviewGroqModelNamesrk   r^   rO   rV   rP   r[   r   r   rS  r@  r   r   rW   rW   rW   rX   <module>   s    T		   
o