
    5P@i90              
         d dl mZ d dlZd dlmZmZ d dlZd dlmZmZm	Z	 d dl
mc 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j2                  ZddZeddddddddd		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZeddddddddd		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d dZej@                   ejB                  d       G d dejD                                      Z"	 d!	 	 	 	 	 d"dZ#	 d!	 	 	 	 	 	 	 d#dZ$e	 d!	 	 	 	 	 	 	 d$d       Z%e	 d!	 	 	 	 	 	 	 d%d       Z%	 d!	 	 	 	 	 	 	 d&dZ%y# e$ r  e	d      ZddZY w xY w)'    )annotationsN)IterableSequence)r   overloadTypeVar)get_default_text_client)string_utils)
text_types)model_types)models)safety_typeszmodels/text-bison-001d   Tc              #     K   |dk  rt        d|       g }| D ](  }|j                  |       t        |      |k(  s#| g }* |r| y y w)N   z Batch size `n` must be >1, got: )
ValueErrorappendlen)iterablenbatchitems       b/home/www/lebenam.kofcorporation.com/venv/lib/python3.12/site-packages/google/generativeai/text.py_batchedr   (   sd     q5?sCDD 	DLL5zQ		 K s
   :AAc                    t        | t              rt        j                  |       S t        | t              rt        j                  |       S t        d       y)aP  
    Creates a `glm.TextPrompt` object based on the provided prompt input.

    Args:
        prompt: The prompt input, either a string or a dictionary.

    Returns:
        glm.TextPrompt: A TextPrompt object containing the prompt text.

    Raises:
        TypeError: If the provided prompt is neither a string nor a dictionary.
    )textz.Expected string or dictionary for text prompt.N)
isinstancestrglm
TextPromptdict	TypeErrorprompts    r   _make_text_promptr%   6   s?     &#~~6**	FD	!~~f%%BC    	modelr$   temperaturecandidate_countmax_output_tokenstop_ptop_ksafety_settingsstop_sequencesc        	            t        j                  |       } t        |      }t        j                  |d      }t        |t              r|g}|rt        |      }t        j                  | ||||||||	      S )a  
    Creates a `glm.GenerateTextRequest` object based on the provided parameters.

    This function generates a `glm.GenerateTextRequest` object with the specified
    parameters. It prepares the input parameters and creates a request that can be
    used for generating text using the chosen model.

    Args:
        model: The model to use for text generation.
        prompt: The prompt for text generation. Defaults to None.
        temperature: The temperature for randomness in generation. Defaults to None.
        candidate_count: The number of candidates to consider. Defaults to None.
        max_output_tokens: The maximum number of output tokens. Defaults to None.
        top_p: The nucleus sampling probability threshold. Defaults to None.
        top_k: The top-k sampling parameter. Defaults to None.
        safety_settings: Safety settings for generated text. Defaults to None.
        stop_sequences: Stop sequences to halt text generation. Can be a string
             or iterable of strings. Defaults to None.

    Returns:
        `glm.GenerateTextRequest`: A `GenerateTextRequest` object configured with the specified parameters.
    r#   old)harm_category_setr'   )
r   make_model_namer%   r   normalize_safety_settingsr   r   listr   GenerateTextRequestr'   s	            r   _make_generate_text_requestr7   K   s    D ''.Ef-F"<<5O .#&()n-""'+'%
 
r&   )	r(   r)   r*   r+   r,   r-   r.   r/   clientc        
        D    t        | ||||||||	      }
t        |	|
      S )aH  Calls the API and returns a `types.Completion` containing the response.

    Args:
        model: Which model to call, as a string or a `types.Model`.
        prompt: Free-form input text given to the model. Given a prompt, the model will
                generate text that completes the input text.
        temperature: Controls the randomness of the output. Must be positive.
            Typical values are in the range: `[0.0,1.0]`. Higher values produce a
            more random and varied response. A temperature of zero will be deterministic.
        candidate_count: The **maximum** number of generated response messages to return.
            This value must be between `[1, 8]`, inclusive. If unset, this
            will default to `1`.

            Note: Only unique candidates are returned. Higher temperatures are more
            likely to produce unique candidates. Setting `temperature=0.0` will always
            return 1 candidate regardless of the `candidate_count`.
        max_output_tokens: Maximum number of tokens to include in a candidate. Must be greater
                           than zero. If unset, will default to 64.
        top_k: The API uses combined [nucleus](https://arxiv.org/abs/1904.09751) and top-k sampling.
            `top_k` sets the maximum number of tokens to sample from on each step.
        top_p: The API uses combined [nucleus](https://arxiv.org/abs/1904.09751) and top-k sampling.
            `top_p` configures the nucleus sampling. It sets the maximum cumulative
            probability of tokens to sample from.
            For example, if the sorted probabilities are
            `[0.5, 0.2, 0.1, 0.1, 0.05, 0.05]` a `top_p` of `0.8` will sample
            as `[0.625, 0.25, 0.125, 0, 0, 0]`.
        safety_settings: A list of unique `types.SafetySetting` instances for blocking unsafe content.
           These will be enforced on the `prompt` and
           `candidates`. There should not be more than one
           setting for each `types.SafetyCategory` type. The API will block any prompts and
           responses that fail to meet the thresholds set by these settings. This list
           overrides the default settings for each `SafetyCategory` specified in the
           safety_settings. If there is no `types.SafetySetting` for a given
           `SafetyCategory` provided in the list, the API will use the default safety
           setting for that category.
        stop_sequences: A set of up to 5 character sequences that will stop output generation.
          If specified, the API will stop at the first appearance of a stop
          sequence. The stop sequence will not be included as part of the response.
        client: If you're not relying on a default client, you pass a `glm.TextServiceClient` instead.

    Returns:
        A `types.Completion` containing the model's text completion response.
    r'   )r8   request)r7   _generate_response)r(   r$   r)   r*   r+   r,   r-   r.   r/   r8   r:   s              r   generate_textr<      s9    p *'+'%
G VW==r&   F)initc                      e Zd Zd Zy)
Completionc                    |j                         D ]  \  }}t        | ||        d | _        | j                  r| j                  d   d   | _        y y )Nr   output)itemssetattrresult
candidates)selfkwargskeyvalues       r   __init__zCompletion.__init__   sQ     ,,. 	&JCD#u%	& ??//!,X6DK r&   N)__name__
__module____qualname__rJ    r&   r   r?   r?      s    7r&   r?   c                ,   |
t               }|j                  |       }t        |      j                  |      }t	        j
                  |d         |d<   t	        j                  |d         |d<   t	        j                  |d         |d<   t        dd|i|S )a  
    Generates a response using the provided `glm.GenerateTextRequest` and client.

    Args:
        request: The text generation request.
        client: The client to use for text generation. Defaults to None, in which
            case the default text client is used.

    Returns:
        `Completion`: A `Completion` object with the generated text and response information.
    filterssafety_feedbackrE   _clientrN   )	r   r<   typeto_dictr   convert_filters_to_enums convert_safety_feedback_to_enumsconvert_candidate_enumsr?   )r:   r8   responses      r   r;   r;      s     ~(*##G,HH~%%h/H&??@STHY"."O"O"##H *AA(<BXYH\1f111r&   c                    t        j                  |       }|
t               }|j                  t	        j
                  |d|i            }t        |      j                  |      S )Nr   )r(   r$   )r   get_base_model_namer   count_text_tokensr   CountTextTokensRequestrS   rT   )r(   r$   r8   
base_modelrD   s        r   r[   r[      s]    
 ++E2J~(*%%""VV<LMF <''r&   c                     y NrN   r(   r   r8   s      r   generate_embeddingsra          r&   c                     y r_   rN   r`   s      r   ra   ra     rb   r&   c                   t        j                  |       } |
t               }t        |t              rOt        j                  | |      }|j                  |      }t        |      j                  |      }|d   d   |d<   |S dg i}t        |t              D ]b  }t        j                  | |      }|j                  |      }t        |      j                  |      }|d   j                  d |d   D               d |S )a  Calls the API to create an embedding for the text passed in.

    Args:
        model: Which model to call, as a string or a `types.Model`.

        text: Free-form input text given to the model. Given a string, the model will
              generate an embedding based on the input text.

        client: If you're not relying on a default client, you pass a `glm.TextServiceClient` instead.

    Returns:
        Dictionary containing the embedding (list of float values) for the input text.
    )r(   r   	embeddingrI   )r(   textsc              3  &   K   | ]	  }|d      yw)rI   NrN   ).0es     r   	<genexpr>z&generate_embeddings.<locals>.<genexpr>:  s     &Xaqz&Xs   
embeddings)r   r3   r   r   r   r   EmbedTextRequest
embed_textrS   rT   r   EMBEDDING_MAX_BATCH_SIZEBatchEmbedTextRequestbatch_embed_textextend)r(   r   r8   embedding_requestembedding_responseembedding_dictrD   r   s           r   ra   ra     s	   $ ''.E~(*$00u4H#../@A0199:LM&4[&A'&J{#  r"d$<= 	YE # 9 9U S!'!8!89J!K!"45==>PQN;&&&X>,;W&XX	Y r&   )r   zIterable[T]r   intreturnzIterable[list[T]])r$   zstr | dict[str, str]rv   zglm.TextPrompt)r(   model_types.AnyModelNameOptionsr$   z
str | Noner)   float | Noner*   
int | Noner+   ry   r,   ry   r-   ry   r.   (safety_types.SafetySettingOptions | Noner/   str | Iterable[str] | Nonerv   glm.GenerateTextRequest)r(   rw   r$   r   r)   rx   r*   ry   r+   ry   r,   rx   r-   rx   r.   rz   r/   r{   r8   glm.TextServiceClient | Nonerv   ztext_types.Completionr_   )r:   r|   r8   glm.TextServiceClientrv   r?   )r(   rw   r$   r   r8   r}   rv   ztext_types.TokenCount)r(    model_types.BaseModelNameOptionsr   r   r8   r~   rv   ztext_types.EmbeddingDict)r(   r   r   zSequence[str]r8   r~   rv   ztext_types.BatchEmbeddingDict)r(   r   r   zstr | Sequence[str]r8   r~   rv   z8text_types.EmbeddingDict | text_types.BatchEmbeddingDict)&
__future__r   dataclassescollections.abcr   r   	itertoolstypingr   r   google.ai.generativelanguageaigenerativelanguager   google.generativeai.clientr   google.generativeair	   google.generativeai.typesr
   r   r   r   DEFAULT_TEXT_MODELrn   batchedr   AttributeErrorr   r%   r7   r<   prettyprint	dataclassr?   r;   r[   ra   rN   r&   r   <module>r      s   #  .  . . * * > , 0 1 & 2,    H$D. .@ $"&$(@D156*6 6 	6
  6 "6 6 6 >6 /6 6v .@ $"&$(@D15+/D>*D> D> 	D>
  D> "D> D> D> >D> /D> )D> D>N E"7&& 7 # 7 GK2$2.C22@ ,0(*(( )( 	(" 
 %)+
 " 	 
 
 %)+
 " #	 
 %)&+&
& "& >	&e  As   D- -E E