
    ^h                         S r SSKrSSKJrJrJr  SSKJrJrJ	r	J
r
JrJrJrJrJr  SSKJrJrJrJrJrJr   " S S5      rg)z
Translates from Cohere's `/v1/rerank` input format to Bedrock's `/rerank` input format.

Why separate file? Make it easy to see how transformation works
    N)ListOptionalUnion)	*BedrockRerankBedrockRerankingConfigurationBedrockRerankConfiguration!BedrockRerankInlineDocumentSourceBedrockRerankModelConfigurationBedrockRerankQueryBedrockRerankRequestBedrockRerankSourceBedrockRerankTextDocumentBedrockRerankTextQuery)RerankBilledUnitsRerankRequestRerankResponseRerankResponseMetaRerankResponseResultRerankTokensc                   `    \ rS rSrS\\\\4      S\\   4S jr	S\
S\4S jrS\S\4S jrS	rg
)BedrockRerankConfig   	documentsreturnc                     / nU Hc  n[        U[        5      (       a*  UR                  [        [	        [        US9SS9SS95        MB  UR                  [        [	        USS9SS95        Me     U$ )zD
Transform the sources from RerankRequest format to Bedrock format.
textTEXT)textDocumenttypeINLINE)inlineDocumentSourcer   JSON)jsonDocumentr   )
isinstancestrappendr   r   r   )selfr   _sourcesdocuments       d/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/bedrock/rerank/transformation.py_transform_sources&BedrockRerankConfig._transform_sources!   s{     !H(C(('-N)B)Q!'. & '-N)1. &	 "(     request_datac                    U R                  UR                  5      n[        [        [	        UR
                  S9SS9/[        [        [        UR                  S9UR                  =(       d    [        UR                  5      S9SS9US9$ )	zD
Transform the request from RerankRequest format to Bedrock format.
r   r   )	textQueryr   )modelArn)modelConfigurationnumberOfResultsBEDROCK_RERANKING_MODEL)bedrockRerankingConfigurationr   )queriesrerankingConfigurationsources)r+   r   r   r
   r   queryr   r   r	   modeltop_nlen)r'   r.   r(   s      r*   _transform_request&BedrockRerankConfig._transform_request>   s     **<+A+AB#"4,:L:LM $>.X'F!-!3!3( %1$6$6$U#l>T>T:U	/ /$ !
 	
r-   responsec           	         [        S0 UR                  S0 5      D6n[        S0 UR                  S0 5      D6n[        X#S9nSnUR                  S5      nU(       a8  U Vs/ s H+  n[	        UR                  S5      UR                  S5      S9PM-     nnUc  [        SU 35      e[        UR                  S	5      =(       d    [        [        R                  " 5       5      UUS
9$ s  snf )z
Transform the response from Bedrock into the RerankResponse format.

example input:
{"results":[{"index":0,"relevanceScore":0.6847912669181824},{"index":1,"relevanceScore":0.5980774760246277}]}
usage)billed_unitstokensNresultsindexrelevanceScore)rE   relevance_scorez!No results found in the response=id)rH   rD   meta )
r   getr   r   r   
ValueErrorr   r%   uuiduuid4)r'   r?   _billed_units_tokensrerank_meta_resultsbedrock_resultsresults           r*   _transform_response'BedrockRerankConfig._transform_responseW   s     *FHLL",EF;gr!:;(mT9=",,y1 .
 .F	 % **W-$*JJ/?$@ .   @
KLL||D!6S%6
 	
s   2C"rJ   N)__name__
__module____qualname____firstlineno__r   r   r%   dictr   r+   r   r   r=   r   rU   __static_attributes__rJ   r-   r*   r   r      sR    eCI./	!	":
} 
AU 
2
D 
^ 
r-   r   )__doc__rM   typingr   r   r   litellm.types.llms.bedrockr   r   r   r	   r
   r   r   r   r   litellm.types.rerankr   r   r   r   r   r   r   rJ   r-   r*   <module>ra      s:     ( (
 
 
 V
 V
r-   