a
    bgCL                  	   @  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rd 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Z1dddddZ2dddddZ3d0ddddddddddZ4d1dddd ddd!d"d#Z5G d$d% d%eZ6G d&d' d'e6Z7ed(d)d*d+G d,d de7eZ8ed(d)d-d+G d.d/ d/e6eZ9dS )2    )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strbool)
model_namereturnc                 C  s   d| v S )z/Return True if the model name is a Codey model.code r'   r*   r*   o/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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VertexAIList[Union[str, 'Image']]"Optional[CallbackManagerForLLMRun]r   )llmpromptstream	is_geminirun_managerkwargsr(   c                   s>   t  |d}|d	ddddd fdd}|||fi |S )
*Use tenacity to retry the completion call.r7   Fr1   r&   r   r4   r6   r8   r(   c                   sN   |r j j| |dS r2 j j| d fi |S  j j| d fi |S d S )N)r5   generation_configr   )clientZgenerate_contentZpredict_streamingpredictr4   r6   r8   r3   r5   r*   r,   _completion_with_retry?   s    z5completion_with_retry.<locals>._completion_with_retry)Fr   )r3   r4   r5   r6   r7   r8   retry_decoratorrA   r*   r@   r,   completion_with_retry4   s
    	 rD   'Optional[AsyncCallbackManagerForLLMRun])r3   r4   r6   r7   r8   r(   c                   sB   t  |d}|d
ddddd fdd}|||fi |I d	H S )r9   r:   Fr%   r&   r   r;   c                   s4   |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?   r3   r*   r,   _acompletion_with_retryY   s
    z7acompletion_with_retry.<locals>._acompletion_with_retryN)FrB   )r3   r4   r6   r7   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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   )rO   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   )__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ddddZeddddZedddd Zed!dd"d#Zed!dd$d%Zed&d'd(d)d*Zd0d+ddd,d-d.d/ZdS )1_VertexAICommonNz'_LanguageModel'r=   client_previewr%   r'   g        floattemperature   rN   max_output_tokensgffffff?top_p(   top_kTrR   r   credentials   nFr&   	streamingr(   c                 C  s   dS )Nvertexair*   selfr*   r*   r,   	_llm_type   s    z_VertexAICommon._llm_typec                 C  s
   t | jS N)r-   r'   ro   r*   r*   r,   r-      s    z_VertexAICommon.is_codey_modelc                 C  s
   t | jS rr   )r/   r'   ro   r*   r*   r,   _is_gemini_model   s    z _VertexAICommon._is_gemini_modelz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,   rt      s    z_VertexAICommon._default_paramsr   Nonevaluesr(   c                   s0   g d  fdd|  D }tf 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>)itemsr   )rY   r{   rx   r*   r   r,   _try_init_vertexai   s    z"_VertexAICommon._try_init_vertexairV   dict)rW   r5   r8   r(   c                   sV   |p| j }ddi  fdd| D }i | jd|i|}|sH| jrR|d |S )Nrk   rv   c                   s   i | ]\}}  |||qS r*   )getr|   Zparams_mappingr*   r,   r      r   z3_VertexAICommon._prepare_params.<locals>.<dictcomp>stop_sequences)rW   r   rt   rl   pop)rp   rW   r5   r8   r   rx   r*   r   r,   _prepare_params   s    


z_VertexAICommon._prepare_params)NF)r[   r\   r]   r=   r^   ra   rc   re   rf   rh   r   ri   rk   rl   propertyrq   r-   rs   ru   rt   r_   r   r   r*   r*   r*   r,   r`      s2   
  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
dZedd	ddZ	e
dddddZdddddZdddddZd,dddddd d!d"d#Zd-ddd$dd d%d&d'Zd.ddddd(d)d*d+ZdS )/r0   z'Google Vertex AI large language models.z
text-bisonr%   r'   NrJ   tuned_model_namer&   rm   c                 C  s   dS )NTr*   ro   r*   r*   r,   is_lc_serializable   s    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   rz   c                 C  s,  | d}|d }t|d }| | zddlm}m} ddlm} ddlm} |rbddlm}	 t	|rt|}
|}n|r|	}
|	}n|}
|}|r|

||d< |
||d	< n>|r|
|d
|d< ||d
|d	< n|
||d< |||d	< W n ty   t  Y n0 |d r(|d dkr(t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'   r6   r   r   ZPreviewCodeGenerationModelZPreviewTextGenerationModelr   Z	model_clsZpreview_model_clsr*   r*   r,   validate_environment   sF    


zVertexAI.validate_environmentrN   )textr(   c                 C  s2   z| j |g}W n ty*   t  Y n0 |jS )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_tokensr"   r   )responser(   c                 C  s8   z|j |jd}W n ty(   d}Y n0 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_generationrV   r2   zOptional[bool]r   r   )promptsrW   r7   r5   r8   r(   c                   s   |d ur|n j } jf ||d|}g }|D ]~}	|rvtdd}
 j|	f||d|D ]}|
|7 }
qZ||
g q0t |	gf| j|d|}| fdd|jD  q0t|dS )	NrW   r5    r   )rW   r7   r5   r6   r7   c                   s   g | ]}  |qS r*   r   r}   rro   r*   r,   
<listcomp>X  r   z&VertexAI._generate.<locals>.<listcomp>generations)	rl   r   r   _streamappendrD   rs   
candidatesr   )rp   r   rW   r7   r5   r8   Zshould_streamrx   r   r4   Z
generationchunkresr*   ro   r,   	_generate;  s:    


zVertexAI._generaterE   r   rW   r7   r8   r(   c           	        sf    j f d|i|}g }|D ]>}t |f j|d|I d H }| fdd|jD  qt|dS )NrW   )r6   r7   c                   s   g | ]}  |qS r*   r   r   ro   r*   r,   r   n  r   z'VertexAI._agenerate.<locals>.<listcomp>r   )r   rH   rs   r   r   r   )	rp   r   rW   r7   r8   rx   r   r4   r   r*   ro   r,   
_agenerate\  s     zVertexAI._ageneratezIterator[GenerationChunk])r4   rW   r7   r8   r(   c                 k  sf   | j f |dd|}t| |gfd| j|d|D ],}| |}|rZ|j|j|| jd |V  q4d S )NTr   r   )r   verbose)r   rD   rs   r   Zon_llm_new_tokenr   r   )rp   r4   rW   r7   r8   rx   Zstream_respr   r*   r*   r,   r   r  s&    

zVertexAI._stream)NNN)NN)NN)r[   r\   r]   __doc__r'   r^   r   r_   r   r   r   r   r   r   r   r   r   r*   r*   r*   r,   r0      s*   
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
dddddZeddddZeddddZddddddZd.dd	ddd d!d"d#Zd$d d%d&d'Zddd(d)d*Zd/dd	d+dd d!d,d-ZdS )0VertexAIModelGardenz-Vertex AI Model Garden large language models.Nz'PredictionServiceClient'r=   z'PredictionServiceAsyncClient'async_clientr%   endpoint_idrV   allowed_model_argsr4   
prompt_argZgenerated_textrJ   
result_argr   rz   c                 C  s   z ddl m} ddlm}m} W n ty8   t  Y n0 |d sJt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_environmentrm   c                 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  s   dS )NZvertexai_model_gardenr*   ro   r*   r*   r,   rq     s    zVertexAIModelGarden._llm_typer   r   zList['Value'])r   r8   r(   c                   s   zddl m ddlm  W n ty6   tdY n0 g }|D ]:}jrb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   	instancesr4   instanceZpredict_instancesr*   )r!   r   rp   r,   _prepare_request  s(    


z$VertexAIModelGarden._prepare_requestr2   r   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   r7   r8   r   r   r*   r*   r,   r     s    zVertexAIModelGarden._generatez'Prediction')predictionsr(   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_response)r   r(   c                 C  st   t |tr|S | jrpz|| j W S  tyn   t |trZd| j dt| d}t|nt| j dY n0 |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                   s4   | 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._agenerate)NN)NN)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     s0   


    r   )FFN)FN):
__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`   r0   r   r*   r*   r*   r,   <module>   sR   (     O :