o
    : hF/                     @  s  U d dl mZ d dl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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 d d
lmZ dZdZejZeeeef Zi ejejd ejdejdejej ej dej dej dej ej!ej!dej!dej!dej!ej"ej"dej"dej"dej"ej#ej#dej#dej#ej$ej$dej$dej$dej%dej%dej%ej%ej%dej&d ej&d!ej&ej&ej&iZ'd"e(d#< dPd(d)Z)zej*Z+W n e,y   ed*Z-dQd0d1Z+Y nw e					dRdSdAdBZ.e					dRdTdEdBZ.					dRdUdIdBZ.e					dRdVdKdLZ/e					dRdWdMdLZ/					dRdXdOdLZ/dS )Y    )annotationsN)AnyIterableoverloadTypeVarUnionMapping)protos)get_default_generative_client)#get_default_generative_async_client)helper_types)model_types)
text_types)content_typeszmodels/embedding-001d   Ztask_type_unspecifiedunspecified   Zretrieval_queryquery   Zretrieval_documentdocument   Zsemantic_similarityZ
similarity   Zclassification   Z
clustering   Zquestion_answeringZqa   Zfact_verificationZverificationz1dict[EmbeddingTaskTypeOptions, EmbeddingTaskType]_EMBEDDING_TASK_TYPExEmbeddingTaskTypeOptionsreturnEmbeddingTaskTypec                 C  s   t | tr	|  } t|  S N)
isinstancestrlowerr   )r    r$   e/var/www/html/figdemos/bartoux_crm/venv/lib/python3.10/site-packages/google/generativeai/embedding.pyto_task_typeH   s   
r&   TiterableIterable[T]nintIterable[list[T]]c                 c  sZ    |dk rt d| dg }| D ]}|| t||kr#|V  g }q|r+|V  d S d S )Nr   zKInvalid input: The batch size 'n' must be a positive integer. You entered: z'. Please enter a number greater than 0.)
ValueErrorappendlen)r(   r*   batchitemr$   r$   r%   _batchedT   s   


r2   model model_types.BaseModelNameOptionscontentcontent_types.ContentType	task_typeEmbeddingTaskTypeOptions | Nonetitle
str | Noneoutput_dimensionality
int | Noneclient"glm.GenerativeServiceClient | Nonerequest_options&helper_types.RequestOptionsType | Nonetext_types.EmbeddingDictc                 C     d S r    r$   r3   r5   r7   r9   r;   r=   r?   r$   r$   r%   embed_contentd      	rD   #Iterable[content_types.ContentType]text_types.BatchEmbeddingDictc                 C  rB   r    r$   rC   r$   r$   r%   rD   p   rE   ?content_types.ContentType | Iterable[content_types.ContentType]glm.GenerativeServiceClient8text_types.EmbeddingDict | text_types.BatchEmbeddingDictc                   sX  t   |du ri }|du rt }r&ttjur&td d dr4dk r4td dr:tt|trt|t	t
fsdg i} fdd	|D }t|tD ]'}	tj |	d
}
|j|
fi |}t||}|d dd	 |d D  q[|S tj t|d}
|j|
fi |}t||}|d d |d< |S )a  Calls the API to create embeddings for content passed in.

    Args:
        model:
            Which [model](https://ai.google.dev/models/gemini#embedding) to
            call, as a string or a `types.Model`.

        content:
            Content to embed.

        task_type:
            Optional task type for which the embeddings will be used. Can only
            be set for `models/embedding-001`.

        title:
            An optional title for the text. Only applicable when task_type is
            `RETRIEVAL_DOCUMENT`.

        output_dimensionality:
            Optional reduced dimensionality for the output embeddings. If set,
            excessive values from the output embeddings will be truncated from
            the end.

        request_options:
            Options for the request.

    Return:
        Dictionary containing the embedding (list of float values) for the
        input content.
    NsInvalid task type: When a title is specified, the task must be of a 'retrieval document' type. Received task type:  and title: .r   QInvalid value: `output_dimensionality` must be a non-negative integer. Received: 	embeddingc                 3  *    | ]}t j t|d V  qdS r3   r5   r7   r9   r;   Nr	   EmbedContentRequestr   
to_content.0cr3   r;   r7   r9   r$   r%   	<genexpr>       
z embed_content.<locals>.<genexpr>r3   requestsc                 s      | ]}|d  V  qdS valuesNr$   rW   er$   r$   r%   rZ          
embeddingsrR   r`   )r   make_model_namer
   r&   r   RETRIEVAL_DOCUMENTr-   r!   r   r"   r   r2   EMBEDDING_MAX_BATCH_SIZEr	   BatchEmbedContentsRequestbatch_embed_contentstypeto_dictextendrT   r   rU   rD   r3   r5   r7   r9   r;   r=   r?   resultr]   r0   Zembedding_requestZembedding_responseZembedding_dictr$   rY   r%   rD   |   sZ   
'

'glm.GenerativeServiceAsyncClient | Nonec                      d S r    r$   rC   r$   r$   r%   embed_content_async      	rq   c                   rp   r    r$   rC   r$   r$   r%   rq      rr    glm.GenerativeServiceAsyncClientc                   sf  t   |du ri }|du rt }r'ttjur'td d dr5dk r5td dr;tt|trt|t	t
fsdg i} fdd	|D }t|tD ]*}	tj |	d
}
|j|
fi |I dH }t||}|d dd	 |d D  q\|S tj t|d}
|j|
fi |I dH }t||}|d d |d< |S )z?Calls the API to create async embeddings for content passed in.NrK   rL   rM   r   rN   rO   c                 3  rP   rQ   rS   rV   rY   r$   r%   rZ     r[   z&embed_content_async.<locals>.<genexpr>r\   c                 s  r^   r_   r$   ra   r$   r$   r%   rZ   (  rc   rd   rR   r`   )r   re   r   r&   r   rf   r-   r!   r   r"   r   r2   rg   r	   rh   ri   rj   rk   rl   rT   r   rU   rD   rm   r$   rY   r%   rq      s\   


)r   r   r   r   )r(   r)   r*   r+   r   r,   )NNNNN)r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   r   rA   )r3   r4   r5   rF   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   r   rG   )r3   r4   r5   rH   r7   r8   r9   r:   r;   r<   r=   rI   r?   r@   r   rJ   )r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   ro   r?   r@   r   rA   )r3   r4   r5   rF   r7   r8   r9   r:   r;   r<   r=   ro   r?   r@   r   rG   )r3   r4   r5   rH   r7   r8   r9   r:   r;   r<   r=   rs   r?   r@   r   rJ   )0
__future__r   	itertoolstypingr   r   r   r   r   r   google.ai.generativelanguageaigenerativelanguageglmgoogle.generativeair	   google.generativeai.clientr
   r   google.generativeai.typesr   r   r   r   ZDEFAULT_EMB_MODELrg   TaskTyper   r+   r"   r   TASK_TYPE_UNSPECIFIEDRETRIEVAL_QUERYrf   SEMANTIC_SIMILARITYCLASSIFICATION
CLUSTERINGQUESTION_ANSWERINGFACT_VERIFICATIONr   __annotations__r&   Zbatchedr2   AttributeErrorr'   rD   rq   r$   r$   r$   r%   <module>   s    	

"
b