Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/cohere/manually_maintained/cohere_aws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
import tarfile
import tempfile
import time
import typing
from typing import Any, Dict, List, Optional, Union

from ..lazy_aws_deps import lazy_boto3, lazy_botocore, lazy_sagemaker
from .chat import Chat, StreamingChat
from .classification import Classification, Classifications
from .embeddings import Embeddings
from .error import CohereError
from .generation import Generations, StreamingGenerations
from .chat import Chat, StreamingChat
from .mode import Mode
from .rerank import Reranking
from .summary import Summary
from .mode import Mode
import typing
from ..lazy_aws_deps import lazy_boto3, lazy_botocore, lazy_sagemaker


class Client:
def __init__(
Expand All @@ -27,7 +28,7 @@ def __init__(
`aws configure set region us-west-2` or override it with `region_name` parameter.
"""
self.mode = mode
if os.environ.get('AWS_DEFAULT_REGION') is None:
if os.environ.get('AWS_DEFAULT_REGION') is None and aws_region is not None:
os.environ['AWS_DEFAULT_REGION'] = aws_region

if self.mode == Mode.SAGEMAKER:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_aws_client_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ def test_default_mode_is_sagemaker(self) -> None:
sig = inspect.signature(Client.__init__)
self.assertEqual(sig.parameters["mode"].default, Mode.SAGEMAKER)

def test_missing_region_does_not_write_none_to_environment(self) -> None:
"""No aws_region + unset AWS_DEFAULT_REGION should not crash before boto3 resolves defaults."""
mock_boto3 = MagicMock()
mock_sagemaker = MagicMock()

with patch("cohere.manually_maintained.cohere_aws.client.lazy_boto3", return_value=mock_boto3), \
patch("cohere.manually_maintained.cohere_aws.client.lazy_sagemaker", return_value=mock_sagemaker), \
patch.dict(os.environ, {}, clear=True):

from cohere.manually_maintained.cohere_aws.client import Client

Client()

self.assertNotIn("AWS_DEFAULT_REGION", os.environ)
self.assertEqual(mock_boto3.client.call_args_list[0].kwargs["region_name"], None)


class TestEmbedV4Params(unittest.TestCase):
"""Fix 3: embed() should accept output_dimension and embedding_types,
Expand Down