o
    ZhF/                     @  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_documentZ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#   T/var/www/html/lang_env/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   


r1   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#   r2   r4   r6   r8   r:   r<   r>   r#   r#   r$   embed_contentd      	rC   #Iterable[content_types.ContentType]text_types.BatchEmbeddingDictc                 C  rA   r   r#   rB   r#   r#   r$   rC   p   rD   ?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 r2   r4   r6   r8   r:   Nr	   EmbedContentRequestr   
to_content.0cr2   r:   r6   r8   r#   r$   	<genexpr>       
z embed_content.<locals>.<genexpr>r2   requestsc                 s      | ]}|d  V  qdS valuesNr#   rV   er#   r#   r$   rY          
embeddingsrQ   r_   )r   make_model_namer
   r%   r   RETRIEVAL_DOCUMENTr,   r    r   r!   r   r1   EMBEDDING_MAX_BATCH_SIZEr	   BatchEmbedContentsRequestbatch_embed_contentstypeto_dictextendrS   r   rT   rC   r2   r4   r6   r8   r:   r<   r>   resultr\   r/   Zembedding_requestZembedding_responseZembedding_dictr#   rX   r$   rC   |   sZ   
'

'glm.GenerativeServiceAsyncClient | Nonec                      d S r   r#   rB   r#   r#   r$   embed_content_async      	rp   c                   ro   r   r#   rB   r#   r#   r$   rp      rq    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.NrJ   rK   rL   r   rM   rN   c                 3  rO   rP   rR   rU   rX   r#   r$   rY     rZ   z&embed_content_async.<locals>.<genexpr>r[   c                 s  r]   r^   r#   r`   r#   r#   r$   rY   (  rb   rc   rQ   r_   )r   rd   r   r%   r   re   r,   r    r   r!   r   r1   rf   r	   rg   rh   ri   rj   rk   rS   r   rT   rC   rl   r#   rX   r$   rp      s\   


)r   r   r   r   )r'   r(   r)   r*   r   r+   )NNNNN)r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r   r@   )r2   r3   r4   rE   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r   rF   )r2   r3   r4   rG   r6   r7   r8   r9   r:   r;   r<   rH   r>   r?   r   rI   )r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   rn   r>   r?   r   r@   )r2   r3   r4   rE   r6   r7   r8   r9   r:   r;   r<   rn   r>   r?   r   rF   )r2   r3   r4   rG   r6   r7   r8   r9   r:   r;   r<   rr   r>   r?   r   rI   )0
__future__r   	itertoolstypingr   r   r   r   r   r   Zgoogle.ai.generativelanguageZaiZgenerativelanguageZglmZgoogle.generativeair	   Zgoogle.generativeai.clientr
   r   Zgoogle.generativeai.typesr   r   r   r   ZDEFAULT_EMB_MODELrf   ZTaskTyper   r*   r!   r   ZTASK_TYPE_UNSPECIFIEDZRETRIEVAL_QUERYre   ZSEMANTIC_SIMILARITYZCLASSIFICATIONZ
CLUSTERINGZQUESTION_ANSWERINGZFACT_VERIFICATIONr   __annotations__r%   Zbatchedr1   AttributeErrorr&   rC   rp   r#   r#   r#   r$   <module>   s    	

"
b