o
    Zh'Q                     @   sT  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlm  mZ ddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlm Z  e!e"Z#dZ$dej% Z&dZ'dZ(dZ)e*dZ+eG dd dZ,eG dd dZ-eG dd dZ.eG dd dZ/de/ddfddZ0de/fddZ1e/ a2G d d! d!ej3Z4deej3 fd"d#Z5dej6fd$d%Z7					dod&eej3 d'ee8 d(eee8ef  d)eej9j: d*ee8 dee8ef fd+d,Z;					dod&eej3 d'ee8 d(eee8ef  d)eej9j: d*ee8 defd-d.Z<				dpd&eej3 d'ee8 d(eee8ef  d)eej9j: d*ee8 defd/d0Z=d1ej6de	e- fd2d3Z>d4e8d1ej6dee- fd5d6Z?ddd7d4ee8 d8ee8 d1ej6de-fd9d:Z@d4e8d1ej6ddfd;d<ZAd4e8d1ej6de	e. fd=d>ZBd4e8d?e8d1ej6dee. fd@dAZCddddBd4e8d?ee8 d8ee8 dCeee8ef  d1ej6de.fdDdEZDd4e8d?e8d1ej6ddfdFdGZEddHd4e8d?e8dIe
e8 dJee
ee8ef   d1ej6de
ejF fdKdLZGd4e8d?e8dMe8d1ej6ddf
dNdOZHdPddQd4e8dRe8dSeIdTeee8ef  d1ej6de
ejJ fdUdVZKdPddQd4e8d?e8dRe8dSeIdTeee8ef  d1ej6de
ejJ fdWdXZLeG dYdZ dZZMeG d[d\ d\ZNeG d]d^ d^eOZPejQjRjSg dd_d`e8dae
e8 dbeIdce
ejT ddeeU d1ejdeNfdedfZVdgejWde8fdhdiZXdCee8ef de
ejY fdjdkZZdleee8ef  de
ej[ fdmdnZ\dS )qzTemporary high-level library of the Google GenerativeAI API.

The content of this file should eventually go into the Python package
google.generativeai.
    N)	dataclass)AnyDictIteratorListMutableSequenceOptional)GenerativeServiceAsyncClient)GenerativeServiceClient)client_options)
exceptions)gapic_v1)credentialsr   )timestamp_pb2z!generativelanguage.googleapis.comz
langchain/   z
models/aqad   z;^corpora/([^/]+?)(/documents/([^/]+?)(/chunks/([^/]+?))?)?$c                   @   s   e Zd ZU eed< dZee ed< dZee ed< dddZe	dedd fd	d
Z
defddZdefddZdefddZdefddZdefddZdS )
EntityName	corpus_idNdocument_idchunk_idreturnc                 C   s*   | j d ur| jd u rtd|  d S d S )Nz&Chunk must have document ID but found )r   r   
ValueErrorself r   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_genai/_genai_extension.py__post_init__*   s   zEntityName.__post_init__encodedc                 C   s<   t |}|std| | |d|d|ddS )NzInvalid entity name:          r   r   r   )_NAME_REGEXmatchr   group)clsr   Zmatchedr   r   r   from_str.   s   
zEntityName.from_strc                 C   sL   d| j  }| jd u r|S |d| j 7 }| jd u r|S |d| j 7 }|S )Nzcorpora/z/documents/z/chunks/r!   r   namer   r   r   __repr__:   s   

zEntityName.__repr__c                 C   s   t | S N)reprr   r   r   r   __str__D   s   zEntityName.__str__c                 C   s
   | j d u S r*   )r   r   r   r   r   	is_corpusG      
zEntityName.is_corpusc                 C   s   | j d uo	| jd u S r*   )r   r   r   r   r   r   is_documentJ   s   zEntityName.is_documentc                 C   s
   | j d uS r*   )r   r   r   r   r   is_chunkM   r.   zEntityName.is_chunk)r   N)__name__
__module____qualname__str__annotations__r   r   r   r   classmethodr&   r)   r,   boolr-   r/   r0   r   r   r   r   r   $   s   
 

r   c                   @   sh   e Zd ZU eed< ee ed< eej ed< eej ed< edefddZ	e
dejdd fd	d
ZdS )Corpusr(   display_namecreate_timeupdate_timer   c                 C      t | j}|jS r*   r   r&   r(   r   r'   r   r   r   r   X      zCorpus.corpus_idcc                 C   s   | |j |j|j|jdS )Nr(   r9   r:   r;   r@   )r%   r?   r   r   r   from_corpus]   s   zCorpus.from_corpusN)r1   r2   r3   r4   r5   r   r   	Timestamppropertyr   r6   genair8   rA   r   r   r   r   r8   Q   s   
 r8   c                   @   s   e Zd ZU eed< ee ed< eej ed< eej ed< eee	j
  ed< edefddZedefd	d
Zede	jdd fddZdS )Documentr(   r9   r:   r;   custom_metadatar   c                 C   r<   r*   r=   r'   r   r   r   r   o   r>   zDocument.corpus_idc                 C   s"   t | j}t|jtsJ |jS r*   )r   r&   r(   
isinstancer   r4   r'   r   r   r   r   t   s   zDocument.document_iddc                 C   s   | |j |j|j|j|jdS )Nr(   r9   r:   r;   rF   rI   )r%   rH   r   r   r   from_documentz   s   zDocument.from_documentN)r1   r2   r3   r4   r5   r   r   rB   r   rD   CustomMetadatarC   r   r   r6   rE   rJ   r   r   r   r   rE   g   s   
 rE   c                   @   sT   e Zd ZU dZeZeed< eZ	eed< e
Zeed< dZeed< dZeej ed< dS )	Configa  Global configuration for Google Generative AI API.

    Normally, the defaults should work fine. Use this to pass Google Auth credentials
    such as using a service account. Refer to for auth credentials documentation:
    https://developers.google.com/identity/protocols/oauth2/service-account#creatinganaccount.

    Attributes:
        api_endpoint: The Google Generative API endpoint address.
        user_agent: The user agent to use for logging.
        page_size: For paging RPCs, how many entities to return per RPC.
        testing: Are the unit tests running?
        auth_credentials: For setting credentials such as using service accounts.
    api_endpoint
user_agent	page_sizeFtestingNauth_credentials)r1   r2   r3   __doc___DEFAULT_API_ENDPOINTrM   r4   r5   _USER_AGENTrN   _DEFAULT_PAGE_SIZErO   intrP   r7   rQ   r   r   Credentialsr   r   r   r   rL      s   
 rL   configr   c                 C   s   | a dS )zASet global defaults for operations with Google Generative AI API.N_config)rX   r   r   r   
set_config   s   r[   c                   C   s   t S r*   rY   r   r   r   r   
get_config   s   r\   c                
   @   s|   e Zd ZdZedefddZedefddZdeddfd	d
Z	ddededdfddZ
dededededdf
ddZdS )TestCredentialszCredentials that do not provide any authentication information.

    Useful for unit tests where the credentials are not used.
    r   c                 C      dS )z/Returns `False`, test credentials never expire.Fr   r   r   r   r   expired      zTestCredentials.expiredc                 C   r^   )z2Returns `True`, test credentials are always valid.Tr   r   r   r   r   valid   r`   zTestCredentials.validrequestNc                 C   s
   t d)zZRaises :class:``InvalidOperation``, test credentials cannot be
        refreshed.
        z%Test credentials cannot be refreshed.)r   InvalidOperation)r   rb   r   r   r   refresh   s   
zTestCredentials.refreshheaderstokenc                 C   s   |dur	t ddS )zAnonymous credentials do nothing to the request.

        The optional ``token`` argument is not supported.

        Raises:
            google.auth.exceptions.InvalidValue: If a token was specified.
        Nz&Test credentials don't support tokens.)r   ZInvalidValue)r   re   rf   r   r   r   apply   s   
zTestCredentials.applymethodurlc                 C   r^   )z+Test credentials do nothing to the request.Nr   )r   rb   rh   ri   re   r   r   r   before_request   s    zTestCredentials.before_requestr*   )r1   r2   r3   rR   rC   r7   r_   ra   r   rd   rg   rj   r   r   r   r   r]      s    "r]   c                   C   s   t jrt S t jrt jS dS )a  Returns credential from config if set or fake credentials for unit testing.

    If _config.testing is True, a fake credential is returned.
    Otherwise, we are in a real environment and will use credentials if provided
    or None is returned.

    If None is passed to the clients later on, the actual credentials will be
    inferred by the rules specified in google.auth package.
    N)rZ   rP   r]   rQ   r   r   r   r   _get_credentials   s
   
rk   c                  C   s*   t  } tj| tjjtdtjt	j
ddS )NrN   )rM   )r   client_infor   )rk   rD   RetrieverServiceClientr   rm   
ClientInforT   client_options_libClientOptionsrZ   rM   )r   r   r   r   build_semantic_retriever   s   rr   r   api_keyr   rm   	transportc                 C   s   dt ji}|r|jdi | | s|r||d< n| s|st } |r#|ntjjtd}| |tj	di ||d}dd |
 D S )NrM   rs   rl   )r   rm   r   rt   c                 S   s   i | ]\}}|d ur||qS r*   r   ).0kvr   r   r   
<dictcomp>  s    z#_prepare_config.<locals>.<dictcomp>r   )rZ   rM   updaterk   r   rm   ro   rT   rp   rq   items)r   rs   r   rm   rt   Zformatted_client_optionsrX   r   r   r   _prepare_config   s"   

r{   c                 C       t | ||||d}tdi |S N)r   rs   r   rt   rm   r   )r{   v1betaGenerativeServiceClientr   rs   r   rm   rt   rX   r   r   r   build_generative_service     r   c                 C   r|   r}   )r{   "v1betaGenerativeServiceAsyncClientr   r   r   r   build_generative_async_service  r   r   clientc                 c   s,    |  tjtjdD ]}t|V  qd S )N)rO   )list_corporarD   ZListCorporaRequestrZ   rO   r8   rA   )r   corpusr   r   r   r   (  s   r   r   c              
   C   st   z| tjtt| dd}t|W S  ty9 } zt|t	j
s# td|  d|  W Y d }~d S d }~ww )Nr   r(   zCorpus  not found: )
get_corpusrD   ZGetCorpusRequestr4   r   r8   rA   	ExceptionrG   gapi_exceptionZPermissionDenied_loggerwarning)r   r   r   er   r   r   r   2  s   r   )r   r9   r9   c                 C   sV   | d urt t| d}nd }|pdtj  }|tjtj||dd}t|S )Nr   	Untitled )r(   r9   )r   )	r4   r   datetimenowcreate_corpusrD   ZCreateCorpusRequestr8   rA   )r   r9   r   r(   new_display_nameZ
new_corpusr   r   r   r   D  s   
r   c                 C   s"   | tjtt| ddd d S )Nr   Tr(   force)delete_corpusrD   ZDeleteCorpusRequestr4   r   )r   r   r   r   r   r   [  s   r   c                 c   s6    | tjtt| dtdD ]}t|V  qd S )Nr   )parentrO   )list_documentsrD   ZListDocumentsRequestr4   r   rU   rE   rJ   )r   r   documentr   r   r   r   e  s   r   r   c                 C   s|   z| tjtt| |dd}t|W S  ty= } zt|t	j
s$ td| d|  d|  W Y d }~d S d }~ww )Nr   r   r   z	Document z in corpus r   )get_documentrD   ZGetDocumentRequestr4   r   rE   rJ   r   rG   r   ZNotFoundr   r   )r   r   r   r   r   r   r   r   r   r  s   r   )r   r9   metadatar   c           	   	   C   sv   |d urt t| |d}nd }|pdtj  }|rt|nd }|tjt t| dtj|||dd}t	|S )Nr   r   r   )r(   r9   rF   )r   r   )
r4   r   r   r   _convert_to_metadatacreate_documentrD   ZCreateDocumentRequestrE   rJ   )	r   r   r9   r   r   r(   r   Znew_metadatasZnew_documentr   r   r   r     s   	
	r   c                 C   s$   | tjtt| |ddd d S )Nr   Tr   )delete_documentrD   ZDeleteDocumentRequestr4   r   )r   r   r   r   r   r   r     s   r   )	metadatastextsr   c              	   C   s  |d u rdd |D }t |t |kr"tdt | dt | dtt| |d}g }tj|g d}t||D ]5\}}	|jtj	|tj
tj|dt|	d	d
 t |jtkrm||}
|t|
j tj|g d}q8t |jdkr||}
|t|
j |S )Nc                 S   s   g | ]}i qS r   r   )ru   _r   r   r   
<listcomp>  s    z&batch_create_chunk.<locals>.<listcomp>zmetadatas's length z and texts's length z are mismatchedr   )r   requests)string_value)datarF   )r   chunkr   )lenr   r4   r   rD   ZBatchCreateChunksRequestzipr   appendZCreateChunkRequestChunkZ	ChunkDatar   _MAX_REQUEST_PER_CHUNKZbatch_create_chunksextendlistchunks)r   r   r   r   r   Zdoc_nameZcreated_chunksZbatch_requesttextr   responser   r   r   batch_create_chunk  sH   



r   r   c              	   C   s$   | tjtt| ||dd d S )Nr!   r   )delete_chunkrD   ZDeleteChunkRequestr4   r   )r   r   r   r   r   r   r   r     s   r      )rv   filterqueryrv   r   c                 C   s0   | tjtt| d|t||d}t|jS )Nr   r(   r   Zmetadata_filtersZresults_count)query_corpusrD   ZQueryCorpusRequestr4   r   _convert_filterr   relevant_chunks)r   r   rv   r   r   r   r   r   r   r     s   
r   c                 C   s2   | tjtt| |d|t||d}t|jS )Nr   r   )query_documentrD   ZQueryDocumentRequestr4   r   r   r   r   )r   r   r   rv   r   r   r   r   r   r   r   
  s   	
r   c                   @   s   e Zd ZU eed< eed< dS )Passager   idN)r1   r2   r3   r4   r5   r   r   r   r   r     s   
 r   c                   @   s.   e Zd ZU eed< ee ed< ee ed< dS )GroundedAnsweranswerattributed_passagesanswerable_probabilityN)	r1   r2   r3   r4   r5   r   r   r   floatr   r   r   r   r   $  s   
 r   c                   @   s>   e Zd ZU ejjed< eed< eej	 ed< defddZ
dS )GenerateAnswerErrorfinish_reasonfinish_messagesafety_ratingsr   c                 C   s   d| j  d| j d| j S )Nzfinish_reason: z finish_message: z safety ratings: r   r   r   r   r   r   r   r,   1  s   
zGenerateAnswerError.__str__N)r1   r2   r3   rD   	CandidateFinishReasonr5   r4   r   ZSafetyRatingr,   r   r   r   r   r   +  s
   
 r   )answer_stylesafety_settingstemperaturepromptpassagesr   r   r   c                 C   s   | tjtjtj| dgdgt|||tjdd t|D dd}|jj	tj
jjkr<t|j}t|jj	||jjdt|jjjdksGJ t|jjjd	 jd
d |jjD |jdS )Nr   partsc                 S   s4   g | ]\}}t jt|t jt j|d gddqS )r   r   )r   content)rD   ZGroundingPassager4   ContentPart)ru   indexr   r   r   r   r   N  s    z#generate_answer.<locals>.<listcomp>)r   )contentsmodelr   r   r   Zinline_passagesr   r   r   c                 S   s8   g | ]}t |jjd krt|jjd  j|jjjdqS )r   )r   r   )r   r   r   r   r   Z	source_idZgrounding_passageZ
passage_id)ru   Zpassager   r   r   r   e  s    )r   r   r   )generate_answerrD   GenerateAnswerRequestr   r   _DEFAULT_GENERATE_SERVICE_MODELZGroundingPassages	enumerater   r   r   r   STOP_get_finish_messager   r   r   r   r   r   r   Zgrounding_attributionsr   )r   r   r   r   r   r   r   r   r   r   r   r   9  s<   
r   	candidatec                 C   s<   t jjjdt jjjdt jjjdi}| j}||vrdS || S )Nz'Maximum token in context window reachedzBlocked because of safetyzBlocked because of recitationzUnexpected generation error)rD   r   r   Z
MAX_TOKENSZSAFETYZ
RECITATIONr   )r   Zfinish_messagesr   r   r   r   r   s  s   


r   c                 C   sl   g }|   D ]-\}}t|trtj||d}nt|ttfr&tj||d}ntd| d|| q|S )N)keyr   )r   numeric_valuezMetadata value  is not supported)	rz   rG   r4   rD   rK   r   rV   r   r   )r   csr   valuer?   r   r   r   r     s   
r   fsc                 C   s   | d u rg S t | tsJ g }|  D ]9\}}t |tr'tjtjjj|d}nt |tt	fr9tjtjjj|d}nt
d| d|tj||gd q|S )N)	operationr   )r   r   zFilter value r   )r   Z
conditions)rG   dictrz   r4   rD   	ConditionOperatorEQUALr   rV   r   r   MetadataFilter)r   filtersr   r   	conditionr   r   r   r     s    


r   )NNNNN)NNNN)]rR   r   loggingredataclassesr   typingr   r   r   r   r   r   Zgoogle.ai.generativelanguageZaiZgenerativelanguagerD   Zlangchain_coreZ#google.ai.generativelanguage_v1betar	   r   r
   r~   Zgoogle.api_corer   rp   r   r   r   Zgoogle.authr   Zgoogle.protobufr   	getLoggerr1   r   rS   __version__rT   rU   r   r   compiler"   r   r8   rE   rL   r[   r\   rZ   rW   r]   rk   rn   rr   r4   rm   ro   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   rV   ZRelevantChunkr   r   r   r   r   r   r   ZAnswerStyleZABSTRACTIVEZSafetySettingr   r   r   r   rK   r   r   r   r   r   r   r   <module>   s    

,%

















4



: (