o
    j6h6                  	   @  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/aqaanswer_style_unspecifiedunspecified   answer_style_abstractiveabstractive   answer_style_extractive
extractive   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&   i/var/www/html/chefvision.cloud.itp360.com/venv/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"   


r;   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]]metadata_filterzOptional[int]max_chunks_countzOptional[float]minimum_relevance_scoreN)r2   
__module____qualname____annotations__r&   r&   r&   r'   r<   z   s   
 r<   
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   
rJ   SemanticRetrieverConfigOptionsr?   r>   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: r?   )r#   r	   SemanticRetrieverConfigrJ   dictr0   r1   r2   r$   r   r8   )r)   r?   rI   r&   r&   r'   _make_semantic_retriever_config   s&   



rO   )modelinline_passagessemantic_retrieveranswer_stylesafety_settingstemperaturerP   model_types.AnyModelNameOptionscontentsrQ   GroundingPassagesOptions | NonerR   %SemanticRetrieverConfigOptions | NonerS   AnswerStyle | NonerT   (safety_types.SafetySettingOptions | NonerU   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: rP   rW   rQ   rR   rT   rU   rS   )r   make_model_namer   to_contentsr   normalize_safety_settings
ValueErrorr;   rO   r0   r(   r	   GenerateAnswerRequest)rP   rW   rQ   rR   rS   rT   rU   r&   r&   r'   _make_generate_answer_request   sD   



rf   )rP   rQ   rR   rS   rT   rU   clientrequest_optionsrg   "glm.GenerativeServiceClient | Nonerh   &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.
    Nr`   )r
   rf   generate_answerrP   rW   rQ   rR   rS   rT   rU   rg   rh   requestresponser&   r&   r'   rk      s   3
rk   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.
    Nr`   )r   rf   rk   rl   r&   r&   r'   generate_answer_async9  s     
ro   )r   r   r    r!   )r)   r*   r    r+   )r    rF   )r)   rK   r?   r>   r    rL   )rP   rV   rW   r>   rQ   rX   rR   rY   rS   rZ   rT   r[   rU   r\   r    r]   )rP   rV   rW   r>   rQ   rX   rR   rY   rS   rZ   rT   r[   rU   r\   rg   ri   rh   rj   )=
__future__r   dataclassescollections.abcr   	itertoolstypingr   r   r   r   typing_extensionsr   google.ai.generativelanguageaigenerativelanguageglmgoogle.generativeair	   google.generativeai.clientr
   r   google.generativeai.typesr   r   r   r   r   )google.generativeai.types.retriever_typesr   DEFAULT_ANSWER_MODELre   r!   intr$   r   ANSWER_STYLE_UNSPECIFIEDABSTRACTIVE
EXTRACTIVEVERBOSEr   rE   r(   r5   r7   ContentTypeGroundingPassageOptionsr/   r*   r;   rG   rH   r=   r<   rM   rK   rJ   rO   rf   rk   ro   r&   r&   r&   r'   <module>   s   	


#

DJ