o
    Á¿i´  ã                   @   s†   d dl mZ d dl mZ d dl mZ dZdZdZdZdZd	Z	d
Z
eeeee	e
fZdedefdd„Zdefdd„Zdeddfdd„ZdS )é    )Ú"CACHE_READ_INPUT_TOKENS_METRIC_KEY)Ú#CACHE_WRITE_INPUT_TOKENS_METRIC_KEY)ÚINPUT_TOKENS_METRIC_KEY)úfoundation-model/úcustom-model/zprovisioned-model/zimported-model/zprompt/z	endpoint/úinference-profile/zdefault-prompt-router/Úai21ÚamazonÚ	anthropicÚcohereÚmetaÚ	stabilityÚoriginal_model_idÚreturnc                 C   s   t D ]
}|| v r|  S qdS )zbIf the original model ID contains a known model provider, return it, otherwise fallback to custom.Úcustom)Ú_MODEL_PROVIDERS)r   Úprovider© r   ú^/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/llmobs/_integrations/bedrock_utils.pyÚ_fallback_provider   s
   ÿr   Úmodel_idc                 C   sÄ   | }|   d¡s"|  d¡}t|ƒdk rt|ƒ|d fS |d |d fS tD ]7}|| vr+q$|  |d¡d } |dv rT|  d¡}t|ƒdk rJt|ƒ| f  S |d |d f  S t|ƒ| f  S t|ƒd	fS )
a`  Best effort to extract and return the model provider and model name from the bedrock model ID.
    model_id can be a 1/2 period-separated string or a full AWS ARN, based on the following formats:
    1. Base model: "{model_provider}.{model_name}"
    2. Cross-region model: "{region}.{model_provider}.{model_name}"
    3. Other: Prefixed by AWS ARN "arn:aws{+region?}:bedrock:{region}:{account-id}:"
        a. Foundation model: ARN prefix + "foundation-model/{region?}.{model_provider}.{model_name}"
        b. Custom model: ARN prefix + "custom-model/{model_provider}.{model_name}"
        c. Provisioned model: ARN prefix + "provisioned-model/{model-id}"
        d. Imported model: ARN prefix + "imported-module/{model-id}"
        e. Prompt management: ARN prefix + "prompt/{prompt-id}"
        f. Sagemaker: ARN prefix + "endpoint/{model-id}"
        g. Inference profile: ARN prefix + "{application-?}inference-profile/{model-id}"
        h. Default prompt router: ARN prefix + "default-prompt-router/{prompt-id}"
    If model provider cannot be inferred from the model_id formatting, then default to "custom"
    zarn:awsÚ.é   r   éþÿÿÿéÿÿÿÿé   )r   r   r   r   )Ú
startswithÚsplitÚlenr   Ú_MODEL_TYPE_IDENTIFIERSÚrsplit)r   r   Ú
model_metaÚ
identifierr   r   r   Úparse_model_id"   s"   


r#   Úusage_metricsNc                 C   sL   t | v st| v r$|  td¡}|  t d¡}|  td¡}|| | | t< dS dS )a8  
    `input_tokens` in bedrock's response usage metadata is the number of non-cached tokens. We normalize it to mean
    the total tokens sent to the model to be consistent with other model providers.

    Args:
        usage_metrics: Dictionary containing token usage metrics that will be modified in-place
    r   N)r   r   Úgetr   )r$   Úinput_tokensÚcache_read_tokensÚcache_write_tokensr   r   r   Únormalize_input_tokensG   s   ür)   )Úddtrace.llmobs._constantsr   r   r   r   Ú_AI21Ú_AMAZONÚ
_ANTHROPICÚ_COHEREÚ_METAÚ
_STABILITYr   Ústrr   r#   Údictr)   r   r   r   r   Ú<module>   s    %