o
    ZhCL                     @  s  d dl mZ d dlmZmZ d dlmZmZmZm	Z	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mZmZ d dlmZ d d	lmZmZmZ d d
lm Z m!Z!m"Z"m#Z# erd dl$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Z0dZ1d5ddZ2d5ddZ3			d6d7d$d%Z4		d8d9d'd(Z5G d)d* d*eZ6G d+d, d,e6Z7ed-d.d/d0G d1d de7eZ8ed-d.d2d0G d3d4 d4e6eZ9dS ):    )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)ImageN
model_namestrreturnboolc                 C  s   d| v S )z/Return True if the model name is a Codey model.code r%   r*   r*   X/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/vertexai.pyis_codey_model*   s   r-   c                 C  s   | duod| v S )z0Return True if the model name is a Gemini model.Ngeminir*   r+   r*   r*   r,   is_gemini_model/   s   r/   FllmVertexAIpromptList[Union[str, 'Image']]stream	is_geminirun_manager"Optional[CallbackManagerForLLMRun]kwargsr   c                   s6   t  |d}|	dd fd
d}|||fi |S )*Use tenacity to retry the completion call.r6   Fr2   r3   r5   r(   r8   r   r'   c                   sJ   |r j j| |dS r j j| d fi |S  j j| d fi |S )N)r4   generation_configr   )clientZgenerate_contentZpredict_streamingpredictr2   r5   r8   r0   r4   r*   r,   _completion_with_retry?   s   z5completion_with_retry.<locals>._completion_with_retryNF)r2   r3   r5   r(   r8   r   r'   r   r   )r0   r2   r4   r5   r6   r8   retry_decoratorr@   r*   r?   r,   completion_with_retry4   s
   	rD   'Optional[AsyncCallbackManagerForLLMRun]c                   s<   t  |d}|	dd fd
d}|||fi |I dH S )r9   r:   Fr2   r&   r5   r(   r8   r   r'   c                   s6   |r j j| |dI d H S  j j| fi |I d H S )N)r;   )r<   Zgenerate_content_asyncZpredict_asyncr>   r0   r*   r,   _acompletion_with_retryY   s   z7acompletion_with_retry.<locals>._acompletion_with_retryNrA   )r2   r&   r5   r(   r8   r   r'   r   rB   )r0   r2   r5   r6   r8   rC   rG   r*   rF   r,   acompletion_with_retryO   s   	rH   c                   @  s   e Zd ZU eddZdZded< 	 dZded< 	 d	Zd
ed< 	 	 dZ	d
ed< 	 e
dddZded< dZded< 	 dZded< 	 eddddZdS )_VertexAIBaser*   )Zprotected_namespacesNOptional[str]projectzus-central1r&   location   intrequest_parallelism   max_retriesTdefaultexcludezClassVar[Optional[Executor]]task_executorOptional[List[str]]stopr%   r'   r   c                 C  s   | j d u rt|d| _ | j S )N)max_workers)rU   r   )clsrO   r*   r*   r,   _get_task_executorx   s   
z _VertexAIBase._get_task_executor)rM   )rO   rN   r'   r   )__name__
__module____qualname__r   Zmodel_configrK   __annotations__rL   rO   rQ   r   rU   rW   r%   classmethodrZ   r*   r*   r*   r,   rI   f   s$   
 
rI   c                   @  s  e Zd ZU dZded< dZded< ded< 	 dZded	< 	 d
Zded< 	 dZded< 	 	 dZ	ded< 	 	 e
dddZded< 	 	 	 dZded< 	 dZded< 	 ed2ddZed3ddZed3dd Zed4d"d#Zed4d$d%Zed5d)d*Z		d6d7d0d1ZdS )8_VertexAICommonNz'_LanguageModel'r<   client_previewr&   r%   g        floattemperature   rN   max_output_tokensgffffff?top_p(   top_kTrR   r   credentials   nFr(   	streamingr'   c                 C     dS )Nvertexair*   selfr*   r*   r,   	_llm_type      z_VertexAICommon._llm_typec                 C  
   t | jS N)r-   r%   ro   r*   r*   r,   r-         
z_VertexAICommon.is_codey_modelc                 C  rs   rt   )r/   r%   ro   r*   r*   r,   _is_gemini_model   ru   z _VertexAICommon._is_gemini_modelDict[str, Any]c                 C  s   i d| j i| jS )z Gets the identifying parameters.r%   )r%   _default_paramsro   r*   r*   r,   _identifying_params   s   z#_VertexAICommon._identifying_paramsc                 C  s0   | j | j| jd}| js|| j| jd |S )N)rc   re   candidate_count)rh   rf   )rc   re   rk   r-   updaterh   rf   )rp   paramsr*   r*   r,   rx      s   z_VertexAICommon._default_paramsvaluesr   Nonec                   s0   g d  fdd|  D }tdi | d S )N)rK   rL   ri   c                   s   i | ]\}}| v r||qS r*   r*   .0kvZallowed_paramsr*   r,   
<dictcomp>       z6_VertexAICommon._try_init_vertexai.<locals>.<dictcomp>r*   )itemsr   )rY   r}   r|   r*   r   r,   _try_init_vertexai   s   z"_VertexAICommon._try_init_vertexairW   rV   r4   r8   dictc                   sV   |p| j }ddi  fdd| D }i | jd|i|}|s$| jr)|d |S )Nrk   rz   c                   s   i | ]\}}  |||qS r*   )getr   Zparams_mappingr*   r,   r      r   z3_VertexAICommon._prepare_params.<locals>.<dictcomp>stop_sequences)rW   r   rx   rl   pop)rp   rW   r4   r8   r   r|   r*   r   r,   _prepare_params   s   


z_VertexAICommon._prepare_paramsr'   r&   r'   r(   )r'   rw   )r}   r   r'   r~   )NF)rW   rV   r4   r(   r8   r   r'   r   )r[   r\   r]   r<   r^   ra   rc   re   rf   rh   r   ri   rk   rl   propertyrq   r-   rv   ry   rx   r_   r   r   r*   r*   r*   r,   r`      sL   
 r`   z0.0.12z1.0z"langchain_google_vertexai.VertexAI)ZsinceZremovalZalternative_importc                   @  s   e Zd ZU dZdZded< 	 dZded< 	 ed/d
dZed0ddZ	e
d1ddZd2ddZd3ddZ			d4d5d&d'Z		d6d7d)d*Z		d6d8d-d.ZdS )9r1   z'Google Vertex AI large language models.z
text-bisonr&   r%   NrJ   tuned_model_namer'   r(   c                 C  rm   )NTr*   ro   r*   r*   r,   is_lc_serializable   rr   zVertexAI.is_lc_serializable	List[str]c                 C  s   g dS )z*Get the namespace of the langchain object.)Z	langchainZllmsrn   r*   )rY   r*   r*   r,   get_lc_namespace   s   zVertexAI.get_lc_namespacer}   r   c                 C  s&  | d}|d }t|d }| | zbddlm}m} ddlm} ddlm} |r1ddlm}	 t	|r:|}
|}n|rA|	}
|	}n|}
|}|rV|

||d< |
||d	< n|rg|
|d
|d< ||d
|d	< n|
||d< |||d	< W n ty   t  Y nw |d r|d dkrtd|S )7Validate that the python package exists in environment.r   r%   r   )CodeGenerationModelTextGenerationModel)r   )r   )GenerativeModelr<   ra   r+   rl   rk   rj   z3Only one candidate can be generated with streaming!)r   r/   r   Zvertexai.language_modelsr   r   Z vertexai.preview.language_models"vertexai.preview.generative_modelsr   r-   Zget_tuned_modelZfrom_pretrainedImportErrorr   
ValueError)rY   r}   r   r%   r5   r   r   ZPreviewCodeGenerationModelZPreviewTextGenerationModelr   Z	model_clsZpreview_model_clsr*   r*   r,   validate_environment   sJ   



zVertexAI.validate_environmenttextrN   c                 C  s4   z| j |g}W |jS  ty   t  Y |jS w )a  Get 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.
        )ra   Zcount_tokensAttributeErrorr   Ztotal_tokens)rp   r   resultr*   r*   r,   get_num_tokens  s   zVertexAI.get_num_tokensresponser"   r   c                 C  s8   z	|j |jd}W n ty   d}Y nw t|j|dS )z1Converts a stream response to a generation chunk.)
is_blockedsafety_attributesN)r   generation_info)r   r   	Exceptionr   r   )rp   r   r   r*   r*   r,   _response_to_generation.  s   
z VertexAI._response_to_generationpromptsrW   rV   r6   r7   r4   Optional[bool]r8   r   r   c                   s   |d ur|n j } jd	||d|}g }|D ]?}	|r;tdd}
 j|	f||d|D ]}|
|7 }
q-||
g qt |	gf| j|d|}| fdd|jD  qt|dS )
NrW   r4    r   )rW   r6   r4   r5   r6   c                      g | ]}  |qS r*   r   r   rro   r*   r,   
<listcomp>X      z&VertexAI._generate.<locals>.<listcomp>generationsr*   )	rl   r   r   _streamappendrD   rv   
candidatesr   )rp   r   rW   r6   r4   r8   Zshould_streamr|   r   r2   Z
generationchunkresr*   ro   r,   	_generate;  s:   



zVertexAI._generaterE   c           	        sh    j dd|i|}g }|D ]}t |f j|d|I d H }| fdd|jD  qt|dS )NrW   )r5   r6   c                   r   r*   r   r   ro   r*   r,   r   n  r   z'VertexAI._agenerate.<locals>.<listcomp>r   r*   )r   rH   rv   r   r   r   )	rp   r   rW   r6   r8   r|   r   r2   r   r*   ro   r,   
_agenerate\  s"   
zVertexAI._agenerater2   Iterator[GenerationChunk]c                 k  sh    | j d|dd|}t| |gfd| j|d|D ]}| |}|r.|j|j|| jd |V  qd S )NTr   r   )r   verboser*   )r   rD   rv   r   Zon_llm_new_tokenr   r   )rp   r2   rW   r6   r8   r|   Zstream_respr   r*   r*   r,   r   r  s*   

zVertexAI._streamr   )r'   r   r}   r   r'   r   )r   r&   r'   rN   )r   r"   r'   r   )NNN)r   r   rW   rV   r6   r7   r4   r   r8   r   r'   r   NN
r   r   rW   rV   r6   rE   r8   r   r'   r   )
r2   r&   rW   rV   r6   r7   r8   r   r'   r   )r[   r\   r]   __doc__r%   r^   r   r_   r   r   r   r   r   r   r   r   r   r*   r*   r*   r,   r1      s0   
 
7
$z-langchain_google_vertexai.VertexAIModelGardenc                   @  s   e Zd ZU dZdZded< dZded< ded< 	 dZd	ed
< 	 dZded< dZ	ded< 	 	 e
d0ddZed1ddZed1ddZd2ddZ		d3d4d$d%Zd5d(d)Zd6d+d,Z		d3d7d.d/ZdS )8VertexAIModelGardenz-Vertex AI Model Garden large language models.Nz'PredictionServiceClient'r<   z'PredictionServiceAsyncClient'async_clientr&   endpoint_idrV   allowed_model_argsr2   
prompt_argZgenerated_textrJ   
result_argr}   r   r'   c                 C  s   zddl m} ddlm}m} W n ty   t  Y nw |d s%td||d  dd}td	d
}|||d|d< |||d|d< |S )r   r   )ClientOptionsr   rK   zBA GCP project should be provided to run inference on Model Garden!rL   z-aiplatform.googleapis.com)Zapi_endpointzvertex-ai-model-garden)module)client_optionsclient_infor<   r   )	Zgoogle.api_core.client_optionsr   google.cloud.aiplatform.gapicr   r   r   r   r   r   )rY   r}   r   r   r   r   r   r*   r*   r,   r     s*   



z(VertexAIModelGarden.validate_environmentc                 C  s   | j j| j| j| jdS )N)rK   rL   endpoint)r<   endpoint_pathrK   rL   r   ro   r*   r*   r,   r     s
   z!VertexAIModelGarden.endpoint_pathc                 C  rm   )NZvertexai_model_gardenr*   ro   r*   r*   r,   rq     rr   zVertexAIModelGarden._llm_typer   r   r8   r   List['Value']c                   s   zddl m ddlm  W n ty   tdw g }|D ]}jr/fdd| D }ni }||j< || q fdd|D }|S )	Nr   )json_formatr    zIprotobuf package not found, please install it with `pip install protobuf`c                   s    i | ]\}}| j v r||qS r*   )r   r   ro   r*   r,   r     s    z8VertexAIModelGarden._prepare_request.<locals>.<dictcomp>c                   s   g | ]	} |  qS r*   )Z	ParseDict)r   instance_dict)r!   r   r*   r,   r     s    z8VertexAIModelGarden._prepare_request.<locals>.<listcomp>)	Zgoogle.protobufr   google.protobuf.struct_pb2r!   r   r   r   r   r   )rp   r   r8   	instancesr2   instanceZpredict_instancesr*   )r!   r   rp   r,   _prepare_request  s*   

z$VertexAIModelGarden._prepare_requestrW   r6   r7   r   c                 K  s.   | j |fi |}| jj| j|d}| |S )*Run the LLM on the given prompt and input.r   r   )r   r<   r=   r   _parse_responserp   r   rW   r6   r8   r   r   r*   r*   r,   r     s   
zVertexAIModelGarden._generatepredictions'Prediction'c                   s2   g }|j D ]}| fdd|D  qt|dS )Nc                   s   g | ]
}t  |d qS )r   )r   _parse_prediction)r   
predictionro   r*   r,   r     s    z7VertexAIModelGarden._parse_response.<locals>.<listcomp>r   )r   r   r   )rp   r   r   r   r*   ro   r,   r     s   


z#VertexAIModelGarden._parse_responser   c                 C  sn   t |tr|S | jr5z|| j W S  ty4   t |tr,d| j dt| d}t|t| j dw |S )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!)
isinstancer&   r   KeyErrortyper   )rp   r   Z
error_descr*   r*   r,   r     s"   

z%VertexAIModelGarden._parse_predictionrE   c                   s6   | j |fi |}| jj| j|dI dH }| |S )r   r   N)r   r   r=   r   r   r   r*   r*   r,   r     s   
zVertexAIModelGarden._agenerater   r   )r   r   r8   r   r'   r   r   )
r   r   rW   rV   r6   r7   r8   r   r'   r   )r   r   r'   r   )r   r   r'   r&   r   )r[   r\   r]   r   r<   r^   r   r   r   r   r   r   r   r   rq   r   r   r   r   r   r*   r*   r*   r,   r     s:   
 




r   )r%   r&   r'   r(   )FFN)r0   r1   r2   r3   r4   r(   r5   r(   r6   r7   r8   r   r'   r   )FN)r0   r1   r2   r&   r5   r(   r6   rE   r8   r   r'   r   ):
__future__r   concurrent.futuresr   r   typingr   r   r   r   r	   r
   r   r   Zlangchain_core._api.deprecationr   Z langchain_core.callbacks.managerr   r   Z#langchain_core.language_models.llmsr   Zlangchain_core.outputsr   r   r   Zlangchain_core.utilsr   Zpydanticr   r   r   Z&langchain_community.utilities.vertexair   r   r   r   r   r   r   Zgoogle.cloud.aiplatform.modelsr   r   r!   Z)vertexai.language_models._language_modelsr"   r#   r   r$   r   Zstream_completion_with_retryr-   r/   rD   rH   rI   r`   r1   r   r*   r*   r*   r,   <module>   sT    (

O :