
    dhK                       S SK Jr  S SKJrJr  S SKJrJrJrJ	r	J
r
JrJrJr  S SKJr  S SKJrJr  S SKJr  S SKJrJrJr  S SKJr  S S	KJrJrJr  S S
KJ r J!r!J"r"J#r#  \(       a"  S SK$J%r%J&r&  S SK'J(r(  S SK)J*r*  S SK+J,r,J-r-  S SK.J/r/  Sr0Sr1S"S jr2S"S jr3   S#             S$S jjr4  S%           S&S jjr5 " S S\5      r6 " S S\65      r7\" SSSS9 " S S\7\5      5       r8\" SSSS9 " S  S!\6\5      5       r9g)'    )annotations)ExecutorThreadPoolExecutor)TYPE_CHECKINGAnyClassVarDictIteratorListOptionalUnion)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLLM)
GenerationGenerationChunk	LLMResult)pre_init)	BaseModel
ConfigDictField)create_retry_decoratorget_client_infoinit_vertexairaise_vertex_import_errorPredictionServiceAsyncClientPredictionServiceClient)
PredictionValue)TextGenerationResponse_LanguageModel)ImageNc                    SU ;   $ )z/Return True if the model name is a Codey model.code 
model_names    Y/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/vertexai.pyis_codey_modelr,   *   s    Z    c                "    U SL=(       a    SU ;   $ )z0Return True if the model name is a Gemini model.Ngeminir(   r)   s    r+   is_gemini_modelr0   /   s    T!<h*&<<r-   VertexAIc                \   ^ ^ [        T US9nU S       SU U4S jjj5       nU" X40 UD6$ )*Use tenacity to retry the completion call.run_managerc                   > U(       a  TR                   R                  U TUS9$ T(       a   TR                   R                  " U S   40 UD6$ TR                   R                  " U S   40 UD6$ )N)streamgeneration_configr   )clientgenerate_contentpredict_streamingpredict)prompt	is_geminikwargsllmr7   s      r+   _completion_with_retry5completion_with_retry.<locals>._completion_with_retry?   sl     ::..v /   zz33F1IHHH::%%fQi:6::r-   F)r=   List[Union[str, 'Image']]r>   boolr?   r   returnr   r   )r@   r=   r7   r>   r5   r?   retry_decoratorrA   s   ` `     r+   completion_with_retryrI   4   s]     -SkJO=B
;)
;6:
;NQ
;	
; 
; 
; "&>v>>r-   c                t   ^ #    [        T US9nU S       SU 4S jjj5       nU" X40 UD6I Sh  vN $  N7f)r3   r4   c                   >#    U(       a!  TR                   R                  XS9I S h  vN $ TR                   R                  " U 40 UD6I S h  vN $  N) N7f)N)r8   )r9   generate_content_asyncpredict_async)r=   r>   r?   r@   s      r+   _acompletion_with_retry7acompletion_with_retry.<locals>._acompletion_with_retryY   sY      :: ;    ZZ--f???? @s!   $AA$AAAANrC   )r=   strr>   rE   r?   r   rF   r   rG   )r@   r=   r>   r5   r?   rH   rN   s   `      r+   acompletion_with_retryrQ   O   sg      -SkJO',@@ $@8;@	@ @ )EfEEEEs   .868c                      \ rS rSr% \" SS9rSrS\S'    SrS\S	'    S
r	S\S'     Sr
S\S'    \" SSS9rS\S'   SrS\S'    SrS\S'    \SSS jj5       rSrg)_VertexAIBasef   r(   )protected_namespacesNOptional[str]projectzus-central1rP   location   intrequest_parallelism   max_retriesTdefaultexcludezClassVar[Optional[Executor]]task_executorOptional[List[str]]stopr*   c                P    U R                   c  [        US9U l         U R                   $ )N)max_workers)ra   r   )clsr[   s     r+   _get_task_executor _VertexAIBase._get_task_executorx   s(    $ 2?R SC   r-   )rY   )r[   rZ   rF   r   )__name__
__module____qualname____firstlineno__r   model_configrW   __annotations__rX   r[   r]   r   ra   rc   r*   classmethodrg   __static_attributes__r(   r-   r+   rS   rS   f   s    26L!G]!B!Hc!8  PK@27d2SM/S $D
$9 $J$! !r-   rS   c                  X   \ rS rSr% SrS\S'   SrS\S'   S\S'    SrS	\S
'    SrS\S'    Sr	S	\S'     Sr
S\S'     \" SSS9rS\S'      SrS\S'    SrS\S'    \S#S j5       r\S$S j5       r\S$S j5       r\S%S j5       r\S%S j5       r\S&S  j5       r  S'       S(S! jjrS"rg))_VertexAICommon   Nz'_LanguageModel'r9   client_previewrP   r*   g        floattemperature   rZ   max_output_tokensgffffff?top_p(   top_kTr^   r   credentials   nFrE   	streamingc                    g)Nvertexair(   selfs    r+   	_llm_type_VertexAICommon._llm_type   s    r-   c                ,    [        U R                  5      $ N)r,   r*   r   s    r+   r,   _VertexAICommon.is_codey_model   s    doo..r-   c                ,    [        U R                  5      $ r   )r0   r*   r   s    r+   _is_gemini_model _VertexAICommon._is_gemini_model   s    t//r-   c                :    0 SU R                   0EU R                  E$ )z Gets the identifying parameters.r*   )r*   _default_paramsr   s    r+   _identifying_params#_VertexAICommon._identifying_params   s$     K<1JT5I5IJJr-   c                    U R                   U R                  U R                  S.nU R                  (       d(  UR	                  U R
                  U R                  S.5        U$ )N)rv   rx   candidate_count)r{   ry   )rv   rx   r~   r,   updater{   ry   )r   paramss     r+   r   _VertexAICommon._default_params   sU      ++!%!7!7#vv

 ""MM!ZZ!ZZ r-   c                    / SQnUR                  5        VVs0 sH  u  p4X2;   d  M  X4_M     nnn[        S0 UD6  g s  snnf )N)rW   rX   r|   r(   )itemsr   )rf   valuesallowed_paramskvr   s         r+   _try_init_vertexai"_VertexAICommon._try_init_vertexai   s@    ?#)<<>I>41Q5H$!$>I Js   
;;c                $   U=(       d    U R                   nSS0nUR                  5        VVs0 sH  u  pgUR                  Xf5      U_M     nnn0 U R                  ESU0EUEnU(       d  U R                  (       a  UR                  S5        U$ s  snnf )Nr~   r   stop_sequences)rc   r   getr   r   pop)	r   rc   r7   r?   r   params_mappingr   r   r   s	            r+   _prepare_params_VertexAICommon._prepare_params   s     *01:@,,.I.$!.$$Q*A-.IUD((U*:NUfUT^^JJ()	 Js   Br(   rF   rP   rF   rE   )rF   zDict[str, Any])r   r	   rF   None)NF)rc   rb   r7   rE   r?   r   rF   dict)ri   rj   rk   rl   r9   rn   rt   rv   rx   ry   r{   r   r|   r~   r   propertyr   r,   r   r   r   ro   r   r   rp   r(   r-   r+   rr   rr      s=   #F#'+N$+OKT s OE5MNE3OONT48K8RSAsJ;It/  / / 0 0 K K     %)!  	
 
 r-   rr   z0.0.12z1.0z"langchain_google_vertexai.VertexAI)sinceremovalalternative_importc                     \ rS rSr% SrSrS\S'    SrS\S'    \SS	 j5       r	\SS
 j5       r
\SS j5       rSS jr    SS jr   S           SS jjr  S         SS jjr  S         SS jjrSrg)r1      z'Google Vertex AI large language models.z
text-bisonrP   r*   NrV   tuned_model_namec                    g)NTr(   r   s    r+   is_lc_serializableVertexAI.is_lc_serializable   s    r-   c                
    / SQ$ )z*Get the namespace of the langchain object.)	langchainllmsr   r(   )rf   s    r+   get_lc_namespaceVertexAI.get_lc_namespace   s
     10r-   c                T   UR                  S5      nUS   n[        US   5      nU R                  U5         SSKJnJn  SSKJn  SSKJn  U(       a  SSKJn	  [        U5      (       a  Un
UnOU(       a  W	n
U	nOUn
UnU(       a)  U
R                  U5      US'   UR                  U5      US	'   OCU(       a  U
" US
9US'   U" US
9US	'   O)U
R                  U5      US'   UR                  U5      US	'    US   (       a  US   S:  a  [        S5      eU$ ! [         a    [        5          N6f = f)7Validate that the python package exists in environment.r   r*   r   )CodeGenerationModelTextGenerationModel)r   )r   )GenerativeModelr9   rt   r)   r   r~   r}   z3Only one candidate can be generated with streaming!)r   r0   r   vertexai.language_modelsr   r    vertexai.preview.language_models"vertexai.preview.generative_modelsr   r,   get_tuned_modelfrom_pretrainedImportErrorr   
ValueError)rf   r   r   r*   r>   r   r   PreviewCodeGenerationModelPreviewTextGenerationModelr   	model_clspreview_model_clss               r+   validate_environmentVertexAI.validate_environment   sJ    "::&89L)
#F<$89	v&+	(  j))/	$>!+	$3!/	$>!#,#<#<=M#Nx +<+L+L$,'( '0J'GF8$/@J/WF+,'0'@'@'LF8$/@/P/P"0F+, +6#;?RSS  	(%'	(s   A5D -D (D D'&D'c                     U R                   R                  U/5      nUR                  $ ! [         a    [        5          WR                  $ f = f)zGet the number of tokens present in the text.

Useful for checking if an input will fit in a model's context window.

Args:
    text: The string input to tokenize.

Returns:
    The integer number of tokens in the text.
)rt   count_tokensAttributeErrorr   total_tokens)r   textresults      r+   get_num_tokensVertexAI.get_num_tokens  sP    	(((55tf=F """  	(%'"""	(s   * AAc                     UR                   UR                  S.n[        UR                  US9$ ! [         a    Sn N"f = f)z1Converts a stream response to a generation chunk.)
is_blockedsafety_attributesN)r   generation_info)r   r   	Exceptionr   r   )r   responser   s      r+   _response_to_generation VertexAI._response_to_generation.  sI    	#&11%-%?%?O HMM?SS  	#"O	#s   / >>c           	        Ub  UOU R                   nU R                  " SX&S.UD6n/ nU H  n	U(       a=  [        SS9n
U R                  " U	4X#S.UD6 H  nX-  n
M	     UR	                  U
/5        MG  [        U U	/4UU R                  US.UD6nUR	                  UR                   Vs/ sH  oR                  U5      PM     sn5        M     [        US9$ s  snf )Nrc   r7    r   )rc   r5   r7   r>   r5   generationsr(   )
r   r   r   _streamappendrI   r   
candidatesr   r   )r   promptsrc   r5   r7   r?   should_streamr   r   r=   
generationchunkresrs                 r+   	_generateVertexAI._generate;  s     #)"4$..%%P4PP.0F,"5
!\\!%BHE 'J ""J<0+H )"33 +  "">AnnMn11!4nM# ( [11 Ns   &C
c           	       #    U R                   " SSU0UD6n/ nU H^  n[        U U4U R                  US.UD6I S h  vN nUR                  UR                   V	s/ sH  oR                  U	5      PM     sn	5        M`     [        US9$  NJs  sn	f 7f)Nrc   )r>   r5   r   r(   )r   rQ   r   r   r   r   r   )
r   r   rc   r5   r?   r   r   r=   r   r   s
             r+   
_agenerateVertexAI._agenerate\  s      %%:4:6:F. //'	
  C :=..I.Q--a0.I  [11 Js!   9BBBB
2BBc              +     #    U R                   " SUSS.UD6n[        U U/4SU R                  US.UD6 HD  nU R                  U5      nU(       a%  UR	                  UR
                  UU R                  S9  Uv   MF     g 7f)NTr   r   )r   verboser(   )r   rI   r   r   on_llm_new_tokenr   r   )r   r=   rc   r5   r?   r   stream_respr   s           r+   r   VertexAI._streamr  s      %%G4GG0H
 ++#
 
K 00=E,,JJ LL - 
 K
s   A;A=r(   r   )rF   	List[str]r   r	   rF   r	   )r   rP   rF   rZ   )r   r#   rF   r   )NNN)r   r   rc   rb   r5   "Optional[CallbackManagerForLLMRun]r7   zOptional[bool]r?   r   rF   r   NN
r   r   rc   rb   r5   'Optional[AsyncCallbackManagerForLLMRun]r?   r   rF   r   )
r=   rP   rc   rb   r5   r   r?   r   rF   zIterator[GenerationChunk])ri   rj   rk   rl   __doc__r*   rn   r   ro   r   r   r   r   r   r   r   r   r   rp   r(   r-   r+   r1   r1      sD    2"J"5&*m*D  1 1 5 5n#$T.T	T  %):>!%22 "2 8	2
 2 2 
2H %)?C	22 "2 =	2
 2 
22 %):>	 " 8	
  
# r-   z-langchain_google_vertexai.VertexAIModelGardenc                     \ rS rSr% SrSrS\S'   SrS\S'   S\S	'    SrS
\S'    Sr	S\S'   Sr
S\S'     \SS j5       r\SS j5       r\SS j5       rSS jr  S         SS jjrSS jrS S jr  S         S!S jjrSrg)"VertexAIModelGardeni  z-Vertex AI Model Garden large language models.Nz'PredictionServiceClient'r9   z'PredictionServiceAsyncClient'async_clientrP   endpoint_idrb   allowed_model_argsr=   
prompt_arggenerated_textrV   
result_argc                     SSK Jn  SSKJnJn  US   (       d  [        S5      eW" US    S3S9n[        S	S
9nW" XVS9US'   W" XVS9US'   U$ ! [
         a    [        5          NTf = f)r   r   )ClientOptionsr   rW   zBA GCP project should be provided to run inference on Model Garden!rX   z-aiplatform.googleapis.com)api_endpointzvertex-ai-model-garden)module)client_optionsclient_infor9   r   )	google.api_core.client_optionsr  google.cloud.aiplatform.gapicr   r   r   r   r   r   )rf   r   r  r   r   r  r  s          r+   r   (VertexAIModelGarden.validate_environment  s    	(D i T  '":.//IJ
 &-EF2)
x ">)"
~ %  	(%'	(s   A A%$A%c                t    U R                   R                  U R                  U R                  U R                  S9$ )N)rW   rX   endpoint)r9   endpoint_pathrW   rX   r   r   s    r+   r  !VertexAIModelGarden.endpoint_path  s5    {{((LL]]%% ) 
 	
r-   c                    g)Nvertexai_model_gardenr(   r   s    r+   r   VertexAIModelGarden._llm_type  s    &r-   c                    SSK Jn  SSKJn  / nU Hj  nU R
                  (       a5  UR                  5        VVs0 sH  u  pxXpR
                  ;   d  M  Xx_M     n	nnO0 n	XiU R                  '   UR                  U	5        Ml     U V
s/ sH  oR                  X" 5       5      PM     nn
U$ ! [         a    [	        S5      ef = fs  snnf s  sn
f )Nr   )json_formatr!   zIprotobuf package not found, please install it with `pip install protobuf`)
google.protobufr  google.protobuf.struct_pb2r"   r   r   r   r   r   	ParseDict)r   r   r?   r  r"   	instancesr=   r   r   instanceinstance_dictpredict_instancess               r+   _prepare_request$VertexAIModelGarden._prepare_request  s    	38 	F&&%+\\^%3TQq<S<S7SDAD^   (.T__%X&  PY
OXm!!-9y 	 
 ! '  	* 	
s   B& B?B?C&B<c                    U R                   " U40 UD6nU R                  R                  U R                  US9nU R	                  U5      $ )*Run the LLM on the given prompt and input.r  r  )r  r9   r<   r  _parse_responser   r   rc   r5   r?   r  r   s          r+   r   VertexAIModelGarden._generate  sI     ))'<V<	;;&&0B0Bi&X##H--r-   c                    / nUR                    H:  nUR                  U Vs/ sH  n[        U R                  U5      S9PM     sn5        M<     [	        US9$ s  snf )Nr   r   )predictionsr   r   _parse_predictionr   )r   r$  r   r   
predictions        r+   r   #VertexAIModelGarden._parse_response  sd    .0!--F '-&,
 D$:$::$FG&, . [11s   !A
c                8   [        U[        5      (       a  U$ U R                  (       a   XR                     $ U$ ! [         aU    [        U[        5      (       a'  SU R                   S[	        U5       S3n[        U5      e[        U R                   S35      ef = f)Nz+Provided non-None `result_arg` (result_arg=z). But got prediction of type zl instead of dict. Most probably, youneed to set `result_arg=None` during VertexAIModelGarden initialization.z key not found in prediction!)
isinstancerP   r  KeyErrortyper   )r   r&  
error_descs      r+   r%  %VertexAIModelGarden._parse_prediction  s    j#&&??X!//22   Xj#..E??++I
+, -**  %Z00$'88U%VWWXs
   : ABc                   #    U R                   " U40 UD6nU R                  R                  U R                  US9I Sh  vN nU R	                  U5      $  N7f)r  r  N)r  r   r<   r  r   r!  s          r+   r   VertexAIModelGarden._agenerate  s_      ))'<V<	**22''9 3 
 
 ##H--
s   ;AAAr(   r   r   )r   r   r?   r   rF   zList['Value']r   )
r   r   rc   rb   r5   r   r?   r   rF   r   )r$  z'Prediction'rF   r   )r&  r   rF   rP   r   )ri   rj   rk   rl   r   r9   rn   r   r   r   r  r   r   r   r  r   r  r   r   r%  r   rp   r(   r-   r+   r   r     s'    8 	 %  	 0  >.2+26J 0J0OO 8 
 
 ' '!8 %):>	
.
. "
. 8	
.
 
. 

.	22 %)?C	.. ". =	.
 . 
. .r-   r   )r*   rP   rF   rE   )FFN)r@   r1   r=   rD   r7   rE   r>   rE   r5   r   r?   r   rF   r   )FN)r@   r1   r=   rP   r>   rE   r5   r   r?   r   rF   r   ):
__future__r   concurrent.futuresr   r   typingr   r   r   r	   r
   r   r   r   langchain_core._api.deprecationr    langchain_core.callbacks.managerr   r   #langchain_core.language_models.llmsr   langchain_core.outputsr   r   r   langchain_core.utilsr   pydanticr   r   r   &langchain_community.utilities.vertexair   r   r   r   r	  r   r   google.cloud.aiplatform.modelsr    r  r"   )vertexai.language_models._language_modelsr#   r$   r   r%   r   stream_completion_with_retryr,   r0   rI   rQ   rS   rr   r1   r   r(   r-   r+   <module>r=     s   " ; V V V 6 8 I I ) 1 1   :0 9  #  
= 6:?	?%? ? 	?
 4? ? 	?< ;?	F	FF F 9	F
 F 	F.!I !2Lm L^ 
;
v v
vr 
F
M.- M.
M.r-   