o
    ß­jÞ6  ã                	   @  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&   úW/var/www/html/CropPilot/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.Contant` 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_retreiver`,
            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().
    Nz‡Invalid configuration: Please set either 'inline_passages' or 'semantic_retriever_config', but not both. Received for inline_passages: z, and for semantic_retriever: Ú.éÿÿÿÿzžInvalid 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_retreiver=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_retreiver`,
            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.TextServiceClient` 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 )aX  
    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_retreiver`,
            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.TextServiceClient` 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ö