o
    Zh6                  	   @  sn  U d dl mZ d dlZd dlmZ d dlZd dlmZmZmZm	Z	m
Z
 d dlmZ d dlm  mZ d dlmZ d dlmZ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ej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'Z(de)d< dLd d!Z*eej+e,e"ej-f ej-f fZ.eej/ee. e	e"ej-f f Z0dMd%d&Z1ee"ej2ej2ej3ej3f Z4G d'd( d(eZ5ee4e5ej6f Z7dNd*d+Z8dOd0d1Z9edddddd2dPdAdBZ:eddddddddCdQdHdIZ;eddddddddCdQdJdKZ<dS )R    )annotationsN)Iterable)Anyr   UnionMappingOptional)	TypedDict)protos)get_default_generative_client#get_default_generative_async_client)model_types)helper_types)safety_types)content_types)retriever_types)MetadataFilterz
models/aqaZanswer_style_unspecifiedunspecified   Zanswer_style_abstractiveZabstractive   Zanswer_style_extractiveZ
extractive   Zanswer_style_verboseverbosez%dict[AnswerStyleOptions, AnswerStyle]_ANSWER_STYLESxAnswerStyleOptionsreturnAnswerStylec                 C  s   t | tr	|  } t|  S N)
isinstancestrlowerr   )r    r    Q/var/www/html/lang_env/lib/python3.10/site-packages/google/generativeai/answer.pyto_answer_style?   s   
r"   sourceGroundingPassagesOptionsprotos.GroundingPassagesc                 C  s   t | tjr| S t | tstdt| j dg }t | tr#|  } t	| D ]2\}}t |tj
r7|| q't |trL|\}}||t|d q'|t|t|d q'tj|dS )a  
    Converts the `source` into a `protos.GroundingPassage`. A `GroundingPassages` contains a list of
    `protos.GroundingPassage` objects, which each contain a `protos.Content` and a string `id`.

    Args:
        source: `Content` or a `GroundingPassagesOptions` that will be converted to protos.GroundingPassages.

    Return:
        `protos.GroundingPassages` to be passed into `protos.GenerateAnswer`.
    zdInvalid input: The 'source' argument must be an instance of 'GroundingPassagesOptions'. Received a 'z' object instead.)idcontent)passages)r   r	   GroundingPassagesr   	TypeErrortype__name__r   items	enumerateGroundingPassageappendtupler   
to_contentr   )r#   r(   ndatar&   r'   r    r    r!   _make_grounding_passagesR   s"   


r5   c                   @  s6   e Zd ZU ded< ded< ded< ded< d	ed
< dS )SemanticRetrieverConfigDictSourceNameTyper#   content_types.ContentsTypequeryz"Optional[Iterable[MetadataFilter]]Zmetadata_filterzOptional[int]Zmax_chunks_countzOptional[float]Zminimum_relevance_scoreN)r,   
__module____qualname____annotations__r    r    r    r!   r6   z   s   
 r6   
str | Nonec                 C  s2   t | tr| S t | tjtjtjtjfr| jS d S r   )r   r   r   Corpusr	   Documentname)r#   r    r    r!   _maybe_get_source_name   s   
rA   SemanticRetrieverConfigOptionsr9   r8   protos.SemanticRetrieverConfigc                 C  s   t | tjr| S t| }|d urd|i} nt | tr#t| d | d< ntdt| j d|  | d d u r;|| d< nt | d trKt	
| d | d< t| S )Nr#   zlInvalid input: Failed to create a 'protos.SemanticRetrieverConfig' from the provided source. Received type: z, Received value: r9   )r   r	   SemanticRetrieverConfigrA   dictr*   r+   r,   r   r   r2   )r#   r9   r@   r    r    r!   _make_semantic_retriever_config   s&   



rF   )modelinline_passagessemantic_retrieveranswer_stylesafety_settingstemperaturerG   model_types.AnyModelNameOptionscontentsrH   GroundingPassagesOptions | NonerI   %SemanticRetrieverConfigOptions | NonerJ   AnswerStyle | NonerK   (safety_types.SafetySettingOptions | NonerL   float | Noneprotos.GenerateAnswerRequestc              	   C  s   t | } t|}|rt|}|dur$|dur$td| d| d|dur-t|}n|dur9t||d }nt	d| d| d|rJt
|}tj| ||||||dS )a  
    constructs a protos.GenerateAnswerRequest object by organizing the input parameters for the API call to generate a grounded answer from the model.

    Args:
        model: Name of the model used to generate the grounded response.
        contents: Content of the current conversation with the model. For single-turn query, this is a
            single question to answer. For multi-turn queries, this is a repeated field that contains
            conversation history and the last `Content` in the list containing the question.
        inline_passages: Grounding passages (a list of `Content`-like objects or `(id, content)` pairs,
            or a `protos.GroundingPassages`) to send inline with the request. Exclusive with `semantic_retriever`,
            one must be set, but not both.
        semantic_retriever: A Corpus, Document, or `protos.SemanticRetrieverConfig` to use for grounding. Exclusive with
             `inline_passages`, one must be set, but not both.
        answer_style: Style for grounded answers.
        safety_settings: Safety settings for generated output.
        temperature: The temperature for randomness in the output.

    Returns:
        Call for protos.GenerateAnswerRequest().
    NzInvalid configuration: Please set either 'inline_passages' or 'semantic_retriever_config', but not both. Received for inline_passages: z, and for semantic_retriever: .zInvalid configuration: Either 'inline_passages' or 'semantic_retriever_config' must be provided, but currently both are 'None'. Received for inline_passages: rG   rN   rH   rI   rK   rL   rJ   )r   Zmake_model_namer   Zto_contentsr   Znormalize_safety_settings
ValueErrorr5   rF   r*   r"   r	   GenerateAnswerRequest)rG   rN   rH   rI   rJ   rK   rL   r    r    r!   _make_generate_answer_request   sD   



rZ   )rG   rH   rI   rJ   rK   rL   clientrequest_optionsr[   "glm.GenerativeServiceClient | Noner\   &helper_types.RequestOptionsType | Nonec        	      	   C  sF   |du ri }|du rt  }t| ||||||d}	|j|	fi |}
|
S )a  Calls the GenerateAnswer API and returns a `types.Answer` containing the response.

    You can pass a literal list of text chunks:

    >>> from google.generativeai import answer
    >>> answer.generate_answer(
    ...     content=question,
    ...     inline_passages=splitter.split(document)
    ... )

    Or pass a reference to a retreiver Document or Corpus:

    >>> from google.generativeai import answer
    >>> from google.generativeai import retriever
    >>> my_corpus = retriever.get_corpus('my_corpus')
    >>> genai.generate_answer(
    ...     content=question,
    ...     semantic_retriever=my_corpus
    ... )


    Args:
        model: Which model to call, as a string or a `types.Model`.
        contents: The question to be answered by the model, grounded in the
                provided source.
        inline_passages: Grounding passages (a list of `Content`-like objects or (id, content) pairs,
            or a `protos.GroundingPassages`) to send inline with the request. Exclusive with `semantic_retriever`,
            one must be set, but not both.
        semantic_retriever: A Corpus, Document, or `protos.SemanticRetrieverConfig` to use for grounding. Exclusive with
             `inline_passages`, one must be set, but not both.
        answer_style: Style in which the grounded answer should be returned.
        safety_settings: Safety settings for generated output. Defaults to None.
        temperature: Controls the randomness of the output.
        client: If you're not relying on a default client, you pass a `glm.GenerativeServiceClient` instead.
        request_options: Options for the request.

    Returns:
        A `types.Answer` containing the model's text answer response.
    NrW   )r
   rZ   generate_answerrG   rN   rH   rI   rJ   rK   rL   r[   r\   requestresponser    r    r!   r_      s   3
r_   c        	      	     sN   |du ri }|du rt  }t| ||||||d}	|j|	fi |I dH }
|
S )a^  
    Calls the API and returns a `types.Answer` containing the answer.

    Args:
        model: Which model to call, as a string or a `types.Model`.
        contents: The question to be answered by the model, grounded in the
                provided source.
        inline_passages: Grounding passages (a list of `Content`-like objects or (id, content) pairs,
            or a `protos.GroundingPassages`) to send inline with the request. Exclusive with `semantic_retriever`,
            one must be set, but not both.
        semantic_retriever: A Corpus, Document, or `protos.SemanticRetrieverConfig` to use for grounding. Exclusive with
             `inline_passages`, one must be set, but not both.
        answer_style: Style in which the grounded answer should be returned.
        safety_settings: Safety settings for generated output. Defaults to None.
        temperature: Controls the randomness of the output.
        client: If you're not relying on a default client, you pass a `glm.GenerativeServiceClient` instead.

    Returns:
        A `types.Answer` containing the model's text answer response.
    NrW   )r   rZ   r_   r`   r    r    r!   generate_answer_async9  s     
rc   )r   r   r   r   )r#   r$   r   r%   )r   r=   )r#   rB   r9   r8   r   rC   )rG   rM   rN   r8   rH   rO   rI   rP   rJ   rQ   rK   rR   rL   rS   r   rT   )rG   rM   rN   r8   rH   rO   rI   rP   rJ   rQ   rK   rR   rL   rS   r[   r]   r\   r^   )=
__future__r   dataclassescollections.abcr   	itertoolstypingr   r   r   r   Ztyping_extensionsr   Zgoogle.ai.generativelanguageZaiZgenerativelanguageZglmZgoogle.generativeair	   Zgoogle.generativeai.clientr
   r   Zgoogle.generativeai.typesr   r   r   r   r   Z)google.generativeai.types.retriever_typesr   ZDEFAULT_ANSWER_MODELrY   r   intr   r   ZANSWER_STYLE_UNSPECIFIEDZABSTRACTIVEZ
EXTRACTIVEVERBOSEr   r<   r"   r/   r1   ZContentTypeZGroundingPassageOptionsr)   r$   r5   r>   r?   r7   r6   rD   rB   rA   rF   rZ   r_   rc   r    r    r    r!   <module>   s   	


#

DJ