From 3e6611a12ef1cbae11541fe020b468306312893b Mon Sep 17 00:00:00 2001 From: Billy Trend Date: Thu, 21 Mar 2024 08:54:52 +0000 Subject: [PATCH 1/3] Fall back on env variable when api_key=None --- src/cohere/client.py | 10 ++++++++-- tests/test_async_client.py | 4 ++++ tests/test_client.py | 4 ++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/cohere/client.py b/src/cohere/client.py index 44a72e346..337327e7f 100644 --- a/src/cohere/client.py +++ b/src/cohere/client.py @@ -59,7 +59,7 @@ def fn(*args, **kwargs): class Client(BaseCohere): def __init__( self, - api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("CO_API_KEY"), + api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None, *, base_url: typing.Optional[str] = os.getenv("CO_API_URL"), environment: ClientEnvironment = ClientEnvironment.PRODUCTION, @@ -67,6 +67,9 @@ def __init__( timeout: typing.Optional[float] = 60, httpx_client: typing.Optional[httpx.Client] = None, ): + if api_key is None: + api_key = os.getenv("CO_API_KEY") + BaseCohere.__init__( self, base_url=base_url, @@ -130,7 +133,7 @@ def __init__( class AsyncClient(AsyncBaseCohere): def __init__( self, - api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("CO_API_KEY"), + api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None, *, base_url: typing.Optional[str] = os.getenv("CO_API_URL"), environment: ClientEnvironment = ClientEnvironment.PRODUCTION, @@ -138,6 +141,9 @@ def __init__( timeout: typing.Optional[float] = 60, httpx_client: typing.Optional[httpx.AsyncClient] = None, ): + if api_key is None: + api_key = os.getenv("CO_API_KEY") + AsyncBaseCohere.__init__( self, base_url=base_url, diff --git a/tests/test_async_client.py b/tests/test_async_client.py index 0b22f3fb5..17e8bdcff 100644 --- a/tests/test_async_client.py +++ b/tests/test_async_client.py @@ -15,6 +15,10 @@ class TestClient(unittest.IsolatedAsyncioTestCase): def setUp(self) -> None: self.co = cohere.AsyncClient(timeout=10000) + async def token_falls_back_on_env_variable(self) -> None: + co = cohere.AsyncClient(api_key=None) + co = cohere.AsyncClient(None) + async def test_chat(self) -> None: chat = await self.co.chat( chat_history=[ diff --git a/tests/test_client.py b/tests/test_client.py index 3ba192c69..4bd49cbd5 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -13,6 +13,10 @@ class TestClient(unittest.TestCase): + def token_falls_back_on_env_variable(self) -> None: + co = cohere.Client(api_key=None) + co = cohere.Client(None) + def test_chat(self) -> None: chat = co.chat( chat_history=[ From fe8c7e20f425e1ae5b1e7deced821557bd0da3b5 Mon Sep 17 00:00:00 2001 From: Billy Trend Date: Thu, 21 Mar 2024 08:56:02 +0000 Subject: [PATCH 2/3] 5.0.2 --- pyproject.toml | 2 +- src/cohere/core/client_wrapper.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6d26d6c66..0d55a01f7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "cohere" -version = "5.0.1" +version = "5.0.2" description = "" readme = "README.md" authors = [] diff --git a/src/cohere/core/client_wrapper.py b/src/cohere/core/client_wrapper.py index d80d74047..0c05be3ca 100644 --- a/src/cohere/core/client_wrapper.py +++ b/src/cohere/core/client_wrapper.py @@ -23,7 +23,7 @@ def get_headers(self) -> typing.Dict[str, str]: headers: typing.Dict[str, str] = { "X-Fern-Language": "Python", "X-Fern-SDK-Name": "cohere", - "X-Fern-SDK-Version": "5.0.1", + "X-Fern-SDK-Version": "5.0.2", } if self._client_name is not None: headers["X-Client-Name"] = self._client_name From d517f9f53e7b1bc125277c5601e914c90b62ed9c Mon Sep 17 00:00:00 2001 From: Billy Trend Date: Thu, 21 Mar 2024 09:01:20 +0000 Subject: [PATCH 3/3] Add test_ --- tests/test_async_client.py | 6 +++--- tests/test_client.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_async_client.py b/tests/test_async_client.py index 17e8bdcff..31e269c48 100644 --- a/tests/test_async_client.py +++ b/tests/test_async_client.py @@ -15,9 +15,9 @@ class TestClient(unittest.IsolatedAsyncioTestCase): def setUp(self) -> None: self.co = cohere.AsyncClient(timeout=10000) - async def token_falls_back_on_env_variable(self) -> None: - co = cohere.AsyncClient(api_key=None) - co = cohere.AsyncClient(None) + async def test_token_falls_back_on_env_variable(self) -> None: + cohere.AsyncClient(api_key=None) + cohere.AsyncClient(None) async def test_chat(self) -> None: chat = await self.co.chat( diff --git a/tests/test_client.py b/tests/test_client.py index 4bd49cbd5..cdf2d2f88 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -13,9 +13,9 @@ class TestClient(unittest.TestCase): - def token_falls_back_on_env_variable(self) -> None: - co = cohere.Client(api_key=None) - co = cohere.Client(None) + def test_token_falls_back_on_env_variable(self) -> None: + cohere.Client(api_key=None) + cohere.Client(None) def test_chat(self) -> None: chat = co.chat(