From 76d4f5d912e1344c9baf634f47e810f4764a8260 Mon Sep 17 00:00:00 2001 From: moritalous Date: Fri, 22 Nov 2024 10:40:50 +0000 Subject: [PATCH 1/3] Added URL encoding for model names that contain "/" when using application inference profile. --- src/anthropic/lib/bedrock/_client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/anthropic/lib/bedrock/_client.py b/src/anthropic/lib/bedrock/_client.py index 70d1561a..b0cd8116 100644 --- a/src/anthropic/lib/bedrock/_client.py +++ b/src/anthropic/lib/bedrock/_client.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import urllib.parse from typing import Any, Union, Mapping, TypeVar from typing_extensions import Self, override @@ -47,6 +48,7 @@ def _prepare_options(input_options: FinalRequestOptions) -> FinalRequestOptions: raise RuntimeError("Expected dictionary json_data for post /completions endpoint") model = options.json_data.pop("model", None) + model = urllib.parse.quote(model, safe="") stream = options.json_data.pop("stream", False) if stream: options.url = f"/model/{model}/invoke-with-response-stream" From cf2238308b33e2a9f56017199f8f42d3789a574e Mon Sep 17 00:00:00 2001 From: moritalous Date: Fri, 22 Nov 2024 13:14:32 +0000 Subject: [PATCH 2/3] add testcase for Bedrock's Application Profile --- tests/lib/test_bedrock.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/lib/test_bedrock.py b/tests/lib/test_bedrock.py index c3af6875..fcfe1113 100644 --- a/tests/lib/test_bedrock.py +++ b/tests/lib/test_bedrock.py @@ -91,3 +91,36 @@ async def test_messages_retries_async(respx_mock: MockRouter) -> None: calls[1].request.url == "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-sonnet-20240229-v1:0/invoke" ) + +@pytest.mark.respx() +def test_application_inference_profile(respx_mock: MockRouter) -> None: + respx_mock.post(re.compile(r"https://bedrock-runtime\.us-east-1\.amazonaws\.com/model/.*/invoke")).mock( + side_effect=[ + httpx.Response(500, json={"error": "server error"}, headers={"retry-after-ms": "10"}), + httpx.Response(200, json={"foo": "bar"}), + ] + ) + + sync_client.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Say hello there!", + } + ], + model="arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/jf2sje1c0jnb", + ) + + calls = cast("list[MockRequestCall]", respx_mock.calls) + + assert len(calls) == 2 + + assert ( + calls[0].request.url + == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn%3Aaws%3Abedrock%3Aus-east-1%3A123456789012%3Aapplication-inference-profile%2Fjf2sje1c0jnb/invoke" + ) + assert ( + calls[1].request.url + == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn%3Aaws%3Abedrock%3Aus-east-1%3A123456789012%3Aapplication-inference-profile%2Fjf2sje1c0jnb/invoke" + ) \ No newline at end of file From 3feb8a6a20d002f26bd3b72a7b778088c7860045 Mon Sep 17 00:00:00 2001 From: moritalous Date: Fri, 22 Nov 2024 14:24:23 +0000 Subject: [PATCH 3/3] fix lint error and test error --- src/anthropic/lib/bedrock/_client.py | 2 +- tests/lib/test_bedrock.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/anthropic/lib/bedrock/_client.py b/src/anthropic/lib/bedrock/_client.py index b0cd8116..89c601d9 100644 --- a/src/anthropic/lib/bedrock/_client.py +++ b/src/anthropic/lib/bedrock/_client.py @@ -48,7 +48,7 @@ def _prepare_options(input_options: FinalRequestOptions) -> FinalRequestOptions: raise RuntimeError("Expected dictionary json_data for post /completions endpoint") model = options.json_data.pop("model", None) - model = urllib.parse.quote(model, safe="") + model = urllib.parse.quote(str(model), safe=":") stream = options.json_data.pop("stream", False) if stream: options.url = f"/model/{model}/invoke-with-response-stream" diff --git a/tests/lib/test_bedrock.py b/tests/lib/test_bedrock.py index fcfe1113..bb29f106 100644 --- a/tests/lib/test_bedrock.py +++ b/tests/lib/test_bedrock.py @@ -118,9 +118,9 @@ def test_application_inference_profile(respx_mock: MockRouter) -> None: assert ( calls[0].request.url - == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn%3Aaws%3Abedrock%3Aus-east-1%3A123456789012%3Aapplication-inference-profile%2Fjf2sje1c0jnb/invoke" + == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn:aws:bedrock:us-east-1:123456789012:application-inference-profile%2Fjf2sje1c0jnb/invoke" ) assert ( calls[1].request.url - == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn%3Aaws%3Abedrock%3Aus-east-1%3A123456789012%3Aapplication-inference-profile%2Fjf2sje1c0jnb/invoke" + == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn:aws:bedrock:us-east-1:123456789012:application-inference-profile%2Fjf2sje1c0jnb/invoke" ) \ No newline at end of file