o
    j6hF/                     @  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   task_type_unspecifiedunspecified   retrieval_queryquery   retrieval_documentdocument   semantic_similarity
similarity   classification   
clustering   question_answeringqa   fact_verificationverificationz1dict[EmbeddingTaskTypeOptions, EmbeddingTaskType]_EMBEDDING_TASK_TYPExEmbeddingTaskTypeOptionsreturnEmbeddingTaskTypec                 C  s   t | tr	|  } t|  S N)
isinstancestrlowerr&   )r'    r/   l/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/generativeai/embedding.pyto_task_typeH   s   
r1   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)r3   r5   batchitemr/   r/   r0   _batchedT   s   


r=   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/   r>   r@   rB   rD   rF   rH   rJ   r/   r/   r0   embed_contentd      	rO   #Iterable[content_types.ContentType]text_types.BatchEmbeddingDictc                 C  rM   r+   r/   rN   r/   r/   r0   rO   p   rP   ?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 r>   r@   rB   rD   rF   Nr	   EmbedContentRequestr   
to_content.0cr>   rF   rB   rD   r/   r0   	<genexpr>       
z embed_content.<locals>.<genexpr>r>   requestsc                 s      | ]}|d  V  qdS valuesNr/   rb   er/   r/   r0   re          
embeddingsr]   rk   )r   make_model_namer
   r1   r*   RETRIEVAL_DOCUMENTr8   r,   r   r-   r   r=   EMBEDDING_MAX_BATCH_SIZEr	   BatchEmbedContentsRequestbatch_embed_contentstypeto_dictextendr_   r   r`   rO   r>   r@   rB   rD   rF   rH   rJ   resultrh   r;   embedding_requestembedding_responseembedding_dictr/   rd   r0   rO   |   sZ   
'

'glm.GenerativeServiceAsyncClient | Nonec                      d S r+   r/   rN   r/   r/   r0   embed_content_async      	r   c                   r~   r+   r/   rN   r/   r/   r0   r      r    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.NrV   rW   rX   r   rY   rZ   c                 3  r[   r\   r^   ra   rd   r/   r0   re     rf   z&embed_content_async.<locals>.<genexpr>rg   c                 s  ri   rj   r/   rl   r/   r/   r0   re   (  rn   ro   r]   rk   )r   rp   r   r1   r*   rq   r8   r,   r   r-   r   r=   rr   r	   rs   rt   ru   rv   rw   r_   r   r`   rO   rx   r/   rd   r0   r      s\   


)r'   r(   r)   r*   )r3   r4   r5   r6   r)   r7   )NNNNN)r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   r)   rL   )r>   r?   r@   rQ   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   r)   rR   )r>   r?   r@   rS   rB   rC   rD   rE   rF   rG   rH   rT   rJ   rK   r)   rU   )r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   r}   rJ   rK   r)   rL   )r>   r?   r@   rQ   rB   rC   rD   rE   rF   rG   rH   r}   rJ   rK   r)   rR   )r>   r?   r@   rS   rB   rC   rD   rE   rF   rG   rH   r   rJ   rK   r)   rU   )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   DEFAULT_EMB_MODELrr   TaskTyper*   r6   r-   r(   TASK_TYPE_UNSPECIFIEDRETRIEVAL_QUERYrq   SEMANTIC_SIMILARITYCLASSIFICATION
CLUSTERINGQUESTION_ANSWERINGFACT_VERIFICATIONr&   __annotations__r1   batchedr=   AttributeErrorr2   rO   r   r/   r/   r/   r0   <module>   s    	

"
b