o
    ưi"                     @   s   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
Z
 d dlmZ ddlmZ ddlmZmZ er@d dl	m
Z d d	lmZ erHd d
lmZ G dd deeeeZdS )    N)TYPE_CHECKINGListOptionalTuplecast)Response)Logging)BasePassthroughConfig   )
BaseAWSLLM)BedrockEventStreamDecoderBaseBedrockModelInfo)CostResponseTypes)URLc                   @   s  e Zd ZdededefddZdedefddZd	ee d
ee dededee dede	def fddZ
	d#dededee d	edee de	eee f fddZdededededededed fddZdee dee fddZdee dd dedededed fd!d"ZdS )$BedrockPassthroughConfigendpointrequest_datareturnc                 C   s   d|v S )Nstream )selfr   r   r   r   f/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/bedrock/passthrough/transformation.pyis_streaming_request   s   z-BedrockPassthroughConfig.is_streaming_requestmodel_idc                 C   sH   ddl m} ddl}d| d}||}|d|}|r"|dS |S )aJ  
        Encode model_id (especially ARNs) for use in Bedrock endpoints.
        
        ARNs contain special characters like colons and slashes that need to be
        properly URL-encoded when used in HTTP request paths. For example:
        arn:aws:bedrock:us-east-1:123:application-inference-profile/abc123
        becomes:
        arn:aws:bedrock:us-east-1:123:application-inference-profile%2Fabc123
        
        Args:
            model_id: The model ID or ARN to encode
            
        Returns:
            The encoded model_id suitable for use in endpoint URLs
        r   )CommonUtilsNz/model/z	/conversez/model/([^/]+)/   )Zlitellm.passthrough.utilsr   reZ"encode_bedrock_runtime_modelid_arnsearchgroup)r   r   r   r   Ztemp_endpointZencoded_temp_endpointZencoded_model_id_matchr   r   r   _encode_model_id_for_endpoint   s   

z6BedrockPassthroughConfig._encode_model_id_for_endpointapi_baseapi_keymodelrequest_query_paramslitellm_paramsr   c                 C   s   |  }|dd }| j|||d}	|d}
| j||
|	dd\}}|d ur:dd l}| |}|dd| d	|}| |||pAi |fS )
Nr   )optional_paramsr"   r   aws_bedrock_runtime_endpointZruntime)r    r&   aws_region_nameZendpoint_typer   zmodel/[^/]+/zmodel//)copygetZ_get_aws_region_nameZget_runtime_endpointr   r   subZ
format_url)r   r    r!   r"   r   r#   r$   r%   r   r'   r&   Zendpoint_url_r   Zencoded_model_idr   r   r   get_complete_url:   s&   	

	
z)BedrockPassthroughConfig.get_complete_urlNheadersc                 C   s"   |  }| jd|||pi ||dS )NZbedrock)Zservice_namer.   r%   r   r    r"   )r)   Z_sign_request)r   r.   r$   r   r    r"   r%   r   r   r   sign_request`   s   z%BedrockPassthroughConfig.sign_requestcustom_llm_providerhttpx_responselogging_objr   c                 C   s   ddl m} ddlm}m}	 ddlm}
 d|v rd| }nd|v r&d| }nd S |
j|||d	}|d u r<td
| |j	|dddg||	 |i i d||d
}|S )Nr   )encoding)LlmProvidersModelResponse)ProviderConfigManagerinvokezinvoke/conversez	converse/)providerr"   z$No provider config found for model: userz no-message-pass-through-endpoint)Zrolecontent )
r"   messagesZraw_responsemodel_responser2   r%   r$   r!   r   r3   )
Zlitellmr3   litellm.types.utilsr4   r5   Zlitellm.utilsr6   Zget_provider_chat_config
ValueErrorZtransform_response)r   r"   r0   r1   r   r2   r   r3   r4   r5   r6   Zchat_config_modelZprovider_chat_configZlitellm_model_responser   r   r   logging_non_streaming_responser   s6   	


z7BedrockPassthroughConfig.logging_non_streaming_response	raw_bytesc                 C   sT   ddl m} g }| }|D ]}|| |D ]}| |}|d ur&|| qq|S )Nr   )EventStreamBuffer)Zbotocore.eventstreamrC   Zadd_dataZ_parse_message_from_eventappend)r   rB   rC   
all_chunksZevent_stream_bufferchunkeventmessager   r   r   _convert_raw_bytes_to_str_lines   s   


z8BedrockPassthroughConfig._convert_raw_bytes_to_str_linesrE   litellm_logging_objLiteLLMLoggingObjc                 C   s*  ddl m}m} ddlm} ddlm}	 ddlm}
 ddl	m
}m} g }d|v rD|	|}|du r;td	| d
| |||ddd}nd|v rQ|d|ddd}ndS |D ]/}t|}|j|d}t|trv|tt|rv|t||}n	t||r~|}nqU|| qUt|dkr|
||d}|S dS )z
        1. Convert all_chunks to a ModelResponseStream
        2. combine model_response_stream to model_response
        3. Return the model_response
        r   ).convert_generic_chunk_to_model_response_stream%generic_chunk_has_all_required_fields) get_bedrock_event_stream_decoder)AmazonInvokeConfig)stream_chunk_builder)GenericStreamingChunkModelResponseStreamr7   NzInvalid invoke provider: z, for model: TF)invoke_providerr"   Zsync_streamZ	json_moder8   )Z
chunk_data)chunksr2   )Z,litellm.litellm_core_utils.streaming_handlerrL   rM   Zlitellm.llms.bedrock.chatrN   ZKlitellm.llms.bedrock.chat.invoke_transformations.base_invoke_transformationrO   Zlitellm.mainrP   r?   rQ   rR   Zget_bedrock_invoke_providerr@   jsonloadsZ_chunk_parser
isinstancedictr   rD   len)r   rE   rJ   r"   r0   r   rL   rM   rN   rO   rP   rQ   rR   Zall_translated_chunksrS   objrF   rH   Ztranslated_chunkZ	chunk_objr>   r   r   r   handle_logging_collected_chunks   s`   


z8BedrockPassthroughConfig.handle_logging_collected_chunks)N)__name__
__module____qualname__strrX   boolr   r   r   r   r-   bytesr/   r   r   rA   r   rI   r[   r   r   r   r   r      sz    

,

+r   )rU   typingr   r   r   r   r   Zhttpxr   Z*litellm.litellm_core_utils.litellm_loggingr   Z0litellm.llms.base_llm.passthrough.transformationr	   Zbase_aws_llmr   Zcommon_utilsr   r   rK   r?   r   r   r   r   r   r   r   <module>   s    
