From 8348c671a925afe1ef70b1d6675fc619dbb48a62 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 8 May 2024 15:25:52 -0700 Subject: [PATCH 1/3] fix - cost tracking - looking up bedrock pricing --- litellm/utils.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index 88e3952337b..9ca2cdff0d5 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -4161,8 +4161,25 @@ def cost_per_token( model_with_provider_and_region in model_cost_ref ): # use region based pricing, if it's available model_with_provider = model_with_provider_and_region - if model_with_provider in model_cost_ref: + + model_without_prefix = model + model_parts = model.split("/") + if len(model_parts) > 1: + model_without_prefix = model_parts[1] + else: + model_without_prefix = model + + if model in model_cost_ref: # Option 1. use model passed, model="gpt-4" + model = model + elif ( + model_with_provider in model_cost_ref + ): # Option 2. use model with provider, model = "openai/gpt-4" model = model_with_provider + elif ( + model_without_prefix in model_cost_ref + ): # Option 3. if user passed model="bedrock/anthropic.claude-3", use model="anthropic.claude-3" + model = model_without_prefix + # see this https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models print_verbose(f"Looking up model={model} in model_cost_map") if model in model_cost_ref: @@ -9423,7 +9440,9 @@ def get_secret( else: secret = os.environ.get(secret_name) try: - secret_value_as_bool = ast.literal_eval(secret) if secret is not None else None + secret_value_as_bool = ( + ast.literal_eval(secret) if secret is not None else None + ) if isinstance(secret_value_as_bool, bool): return secret_value_as_bool else: From 282b8d0ae448bf56b29cd6f5dd3f6c0a6b687f38 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 8 May 2024 15:26:53 -0700 Subject: [PATCH 2/3] test bedrock pricing --- litellm/tests/test_completion_cost.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/litellm/tests/test_completion_cost.py b/litellm/tests/test_completion_cost.py index fecd53e1937..35e0496fb46 100644 --- a/litellm/tests/test_completion_cost.py +++ b/litellm/tests/test_completion_cost.py @@ -231,14 +231,17 @@ def test_cost_bedrock_pricing(): assert cost == predicted_cost -@pytest.mark.skip(reason="AWS disabled our access") def test_cost_bedrock_pricing_actual_calls(): litellm.set_verbose = True model = "anthropic.claude-instant-v1" messages = [{"role": "user", "content": "Hey, how's it going?"}] - response = litellm.completion(model=model, messages=messages) - assert response._hidden_params["region_name"] is not None + response = litellm.completion( + model=model, messages=messages, mock_response="hello cool one" + ) + + print("response", response) cost = litellm.completion_cost( + model="bedrock/anthropic.claude-instant-v1", completion_response=response, messages=[{"role": "user", "content": "Hey, how's it going?"}], ) From 33d6caa8895518c546fd43ee4256f78320adf7cc Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 8 May 2024 15:51:30 -0700 Subject: [PATCH 3/3] fix completion cost test --- litellm/utils.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index 9ca2cdff0d5..ccb6c70e7d7 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -4168,13 +4168,18 @@ def cost_per_token( model_without_prefix = model_parts[1] else: model_without_prefix = model - - if model in model_cost_ref: # Option 1. use model passed, model="gpt-4" - model = model - elif ( + """ + Code block that formats model to lookup in litellm.model_cost + Option1. model = "bedrock/ap-northeast-1/anthropic.claude-instant-v1". This is the most accurate since it is region based. Should always be option 1 + Option2. model = "openai/gpt-4" - model = provider/model + Option3. model = "anthropic.claude-3" - model = model + """ + if ( model_with_provider in model_cost_ref ): # Option 2. use model with provider, model = "openai/gpt-4" model = model_with_provider + elif model in model_cost_ref: # Option 1. use model passed, model="gpt-4" + model = model elif ( model_without_prefix in model_cost_ref ): # Option 3. if user passed model="bedrock/anthropic.claude-3", use model="anthropic.claude-3"