forked from phoenix/litellm-mirror
Merge pull request #4613 from petermuller/main
Fix: use Bedrock region from environment variables before other region definitions
This commit is contained in:
commit
bb6b2c6872
2 changed files with 90 additions and 10 deletions
|
@ -605,15 +605,13 @@ def init_bedrock_client(
|
||||||
) = params_to_check
|
) = params_to_check
|
||||||
|
|
||||||
### SET REGION NAME
|
### SET REGION NAME
|
||||||
if region_name:
|
region_name = (
|
||||||
pass
|
litellm_aws_region_name # prefer environment variables before inline region parameters
|
||||||
elif aws_region_name:
|
or standard_aws_region_name
|
||||||
region_name = aws_region_name
|
or region_name
|
||||||
elif litellm_aws_region_name:
|
or aws_region_name
|
||||||
region_name = litellm_aws_region_name
|
)
|
||||||
elif standard_aws_region_name:
|
if not region_name:
|
||||||
region_name = standard_aws_region_name
|
|
||||||
else:
|
|
||||||
raise BedrockError(
|
raise BedrockError(
|
||||||
message="AWS region not set: set AWS_REGION_NAME or AWS_REGION env variable or in .env file",
|
message="AWS region not set: set AWS_REGION_NAME or AWS_REGION env variable or in .env file",
|
||||||
status_code=401,
|
status_code=401,
|
||||||
|
|
|
@ -10,7 +10,7 @@ sys.path.insert(
|
||||||
0, os.path.abspath("../..")
|
0, os.path.abspath("../..")
|
||||||
) # Adds the parent directory to the system path
|
) # Adds the parent directory to the system path
|
||||||
import litellm
|
import litellm
|
||||||
from litellm import completion
|
from litellm import completion, AuthenticationError
|
||||||
from litellm import RateLimitError
|
from litellm import RateLimitError
|
||||||
|
|
||||||
# Huggingface - Expensive to deploy models and keep them running. Maybe we can try doing this via baseten??
|
# Huggingface - Expensive to deploy models and keep them running. Maybe we can try doing this via baseten??
|
||||||
|
@ -656,6 +656,88 @@ def bedrock_test_completion():
|
||||||
# bedrock_test_completion()
|
# bedrock_test_completion()
|
||||||
|
|
||||||
|
|
||||||
|
def test_bedrock_embedding_default_region():
|
||||||
|
"""
|
||||||
|
If no regions are specified in config or in environment, then throw an error
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
litellm.embedding(
|
||||||
|
model="bedrock/amazon.titan-embed-text-v1",
|
||||||
|
input="Hello, world!"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
assert isinstance(e, AuthenticationError)
|
||||||
|
assert "AWS region not set" in str(e)
|
||||||
|
|
||||||
|
|
||||||
|
# test_bedrock_embedding_default_region()
|
||||||
|
|
||||||
|
|
||||||
|
def test_bedrock_embedding_config_region(mocker):
|
||||||
|
"""
|
||||||
|
When region is specified inline or in config, use that region
|
||||||
|
"""
|
||||||
|
expected_region = "us-east-1"
|
||||||
|
mock_client = mocker.patch("boto3.client")
|
||||||
|
try:
|
||||||
|
litellm.embedding(
|
||||||
|
model="bedrock/amazon.titan-embed-text-v1",
|
||||||
|
input="Hello, world!",
|
||||||
|
aws_region_name=expected_region
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
pass # expected serialization exception because AWS client was replaced with a Mock
|
||||||
|
assert mock_client.call_args.kwargs["region_name"] == expected_region
|
||||||
|
|
||||||
|
|
||||||
|
# test_bedrock_embedding_config_region()
|
||||||
|
|
||||||
|
|
||||||
|
def test_bedrock_embedding_environment_region(mocker):
|
||||||
|
"""
|
||||||
|
When region is specified in an environment variable, use that region
|
||||||
|
"""
|
||||||
|
expected_region = "us-east-1"
|
||||||
|
os.environ["AWS_REGION_NAME"] = expected_region
|
||||||
|
mock_client = mocker.patch("boto3.client")
|
||||||
|
try:
|
||||||
|
litellm.embedding(
|
||||||
|
model="bedrock/amazon.titan-embed-text-v1",
|
||||||
|
input="Hello, world!"
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
pass # expected serialization exception because AWS client was replaced with a Mock
|
||||||
|
del os.environ["AWS_REGION_NAME"] # cleanup
|
||||||
|
assert mock_client.call_args.kwargs["region_name"] == expected_region
|
||||||
|
|
||||||
|
|
||||||
|
# test_bedrock_embedding_environment_region()
|
||||||
|
|
||||||
|
|
||||||
|
def test_bedrock_embedding_config_environment_region(mocker):
|
||||||
|
"""
|
||||||
|
If region is supplied in both the config and the environment variable, use the one
|
||||||
|
defined in the environment variable
|
||||||
|
"""
|
||||||
|
expected_region = "us-east-1"
|
||||||
|
unexpected_region = "us-west-2"
|
||||||
|
os.environ["AWS_REGION_NAME"] = expected_region
|
||||||
|
mock_client = mocker.patch("boto3.client")
|
||||||
|
try:
|
||||||
|
litellm.embedding(
|
||||||
|
model="bedrock/amazon.titan-embed-text-v1",
|
||||||
|
input="Hello, world!",
|
||||||
|
aws_region_name=unexpected_region
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
pass # expected serialization exception because AWS client was replaced with a Mock
|
||||||
|
del os.environ["AWS_REGION_NAME"] # cleanup
|
||||||
|
assert mock_client.call_args.kwargs["region_name"] == expected_region
|
||||||
|
|
||||||
|
|
||||||
|
# test_bedrock_embedding_config_environment_region()
|
||||||
|
|
||||||
|
|
||||||
# OpenAI Chat Completion
|
# OpenAI Chat Completion
|
||||||
def openai_test_completion():
|
def openai_test_completion():
|
||||||
litellm.OpenAIConfig(max_tokens=10)
|
litellm.OpenAIConfig(max_tokens=10)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue