
    rh6              	      &   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c 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 jH                  e jH                  d e jH                  de jH                  de jH                  e jJ                  e jJ                  de jJ                  de jJ                  de jJ                  e jL                  e jL                  de jL                  de jL                  de jL                  e jN                  e jN                  de jN                  de jN                  de jN                  Z(de)d<   d'dZ*eejV                  e,e"ejZ                  f   ejZ                  f   fZ.eej^                  ee.   e	e"ejZ                  f   f   Z0d(dZ1ee"ejd                  ejd                  ejf                  ejf                  f   Z4 G d de      Z5ee4e5ejl                  f   Z7d)d Z8	 	 	 	 	 	 d*d!Z9edddddd"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+d#Z:edddddddd$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d,d%Z;edddddddd$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d,d&Z<y)-    )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_STYLESc                T    t        | t              r| j                         } t        |    S N)
isinstancestrlowerr   )xs    m/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/google/generativeai/answer.pyto_answer_styler&   ?   s"    !SGGI!    c                f   t        | t        j                        r| S t        | t              s"t	        dt        |       j                   d      g }t        | t              r| j                         } t        |       D ]  \  }}t        |t        j                        r|j                  |       2t        |t              r-|\  }}|j                  |t        j                  |      d       o|j                  t        |      t        j                  |      d        t        j                  |      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"   )sourcer+   ndatar)   r*   s         r%   _make_grounding_passagesr9   R   s    &&223fh'rswx~s  tI  tI  sJ  J[  \
 	
 H&'"V$ W4dF334OOD!e$KBOO2-2J2J72STUOO3q6m6N6Nt6TUVW ##X66r'   c                  @    e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   y)SemanticRetrieverConfigDictSourceNameTyper6   content_types.ContentsTypequeryz"Optional[Iterable[MetadataFilter]]metadata_filterzOptional[int]max_chunks_countzOptional[float]minimum_relevance_scoreN)r/   
__module____qualname____annotations__ r'   r%   r;   r;   z   s     %%77##,,r'   r;   c                    t        | t              r| S t        | t        j                  t        j                  t        j
                  t        j
                  f      r| j                  S y r    )r!   r"   r   Corpusr
   Documentname)r6   s    r%   _maybe_get_source_namerJ      sM    &#	''8P8PRXRaRab
 {{r'   c                   t        | t        j                        r| S t        |       }|d|i} nFt        | t              rt        | d         | d<   n$t        dt        |       j                   d|        | d   || d<   n.t        | d   t              rt        j                  | d         | d<   t        j                  |       S )Nr6   zlInvalid input: Failed to create a 'protos.SemanticRetrieverConfig' from the provided source. Received type: z, Received value: r>   )r!   r
   SemanticRetrieverConfigrJ   dictr-   r.   r/   r"   r   r5   )r6   r>   rI   s      r%   _make_semantic_retriever_configrN      s     &&889!&)DD!	FD	!1&2BCx"6l334 5%h(
 	
 gw	F7OS	)'226'?Cw))&11r'   )modelinline_passagessemantic_retrieveranswer_stylesafety_settingstemperaturec           	     f   t        j                  |       } t        j                  |      }|rt	        j
                  |      }||t        d| d| d      |t        |      }n$|t        ||d         }nt        d| d| d      |rt        |      }t        j                  | ||||||      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().
    zInvalid 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: rO   contentsrP   rQ   rS   rT   rR   )r   make_model_namer   to_contentsr   normalize_safety_settings
ValueErrorr9   rN   r-   r&   r
   GenerateAnswerRequest)rO   rY   rP   rQ   rR   rS   rT   s          r%   _make_generate_answer_requestr_      s    < ''.E((2H&@@Q"'9'E--<,==[\n[oopr
 	
 
	$2?C		'<=OQYZ\Q]^--<,==[\n[oopr
 	

 &|4'''-'! r'   )rO   rP   rQ   rR   rS   rT   clientrequest_optionsc        	   	     p    |i }|
t               }t        | ||||||      }	 |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.
    rX   )r   r_   generate_answerrO   rY   rP   rQ   rR   rS   rT   r`   ra   requestresponses              r%   rc   rc      sZ    f ~.0+'-'!G &v%%gAAHOr'   c        	   	        K   |i }|
t               }t        | ||||||      }	 |j                  |	fi | d{   }
|
S 7 w)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.
    NrX   )r   r_   rc   rd   s              r%   generate_answer_asyncrh   9  sg     @ ~46+'-'!G ,V++GGGGHO Hs   9AAA)r$   AnswerStyleOptionsreturnAnswerStyle)r6   GroundingPassagesOptionsrj   zprotos.GroundingPassages)rj   z
str | None)r6   SemanticRetrieverConfigOptionsr>   r=   rj   zprotos.SemanticRetrieverConfig)rO   model_types.AnyModelNameOptionsrY   r=   rP   GroundingPassagesOptions | NonerQ   %SemanticRetrieverConfigOptions | NonerR   AnswerStyle | NonerS   (safety_types.SafetySettingOptions | NonerT   float | Nonerj   zprotos.GenerateAnswerRequest)rO   rn   rY   r=   rP   ro   rQ   rp   rR   rq   rS   rr   rT   rs   r`   z"glm.GenerativeServiceClient | Nonera   z&helper_types.RequestOptionsType | None)=
__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_MODELr^   rk   intr"   ri   ANSWER_STYLE_UNSPECIFIEDABSTRACTIVE
EXTRACTIVEVERBOSEr   rD   r&   r2   r4   ContentTypeGroundingPassageOptionsr,   rl   r9   rG   rH   r<   r;   rL   rm   rJ   rN   r_   rc   rh   rE   r'   r%   <module>r      s   #  $  : : ' * * & 2 2 2 3 5 D# **663[01 9((+*N*N9{++9  D D9 ;77	9
 [449 {9  7 79 ;**9 K229 {9 {559 +((9 ,,9 {9 K//9  {""!95 ( 
sM,E,E'E!FHaHaa  !
$%C***+-  7F 		0H0H&//Y
-) - "'
""$" 2*2%2 $2: .B7;@D'+@D $?*? )? 5	?
 >? %? >? ? "?H .B7;@D'+@D $15>BE*E )E 5	E
 >E %E >E E /E <ET .B7;@D'+@D $15>B2*2 )2 5	2
 >2 %2 >2 2 /2 <2r'   