From 2d0e6fd3a044eddf42f4058e7a6bfcdde39fbffb Mon Sep 17 00:00:00 2001 From: Arun Date: Tue, 24 Oct 2023 01:28:18 -0700 Subject: [PATCH 1/5] Make anthropic_api_key a SecretStr Signed-off-by: Arun --- libs/langchain/langchain/llms/cohere.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/langchain/langchain/llms/cohere.py b/libs/langchain/langchain/llms/cohere.py index c902b4c047fa5..0bf6728ae52f4 100644 --- a/libs/langchain/langchain/llms/cohere.py +++ b/libs/langchain/langchain/llms/cohere.py @@ -18,7 +18,7 @@ from langchain.llms.base import LLM from langchain.llms.utils import enforce_stop_tokens from langchain.load.serializable import Serializable -from langchain.pydantic_v1 import Extra, Field, root_validator +from langchain.pydantic_v1 import Extra, Field, SecretStr, root_validator from langchain.utils import get_from_dict_or_env logger = logging.getLogger(__name__) @@ -73,7 +73,7 @@ class BaseCohere(Serializable): temperature: float = 0.75 """A non-negative float that tunes the degree of randomness in generation.""" - cohere_api_key: Optional[str] = None + cohere_api_key: Optional[SecretStr] = None stop: Optional[List[str]] = None @@ -91,8 +91,8 @@ def validate_environment(cls, values: Dict) -> Dict: "Please install it with `pip install cohere`." ) else: - cohere_api_key = get_from_dict_or_env( - values, "cohere_api_key", "COHERE_API_KEY" + cohere_api_key = SecretStr( + get_from_dict_or_env(values, "cohere_api_key", "COHERE_API_KEY") ) values["client"] = cohere.Client(cohere_api_key) values["async_client"] = cohere.AsyncClient(cohere_api_key) @@ -155,8 +155,8 @@ def _default_params(self) -> Dict[str, Any]: } @property - def lc_secrets(self) -> Dict[str, str]: - return {"cohere_api_key": "COHERE_API_KEY"} + def lc_secrets(self) -> Dict[str, SecretStr]: + return {"cohere_api_key": SecretStr("COHERE_API_KEY")} @property def _identifying_params(self) -> Dict[str, Any]: From e60806f19d8a04f2bdd2f3f528a9addc66665bc5 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Wed, 1 Nov 2023 10:00:08 -0400 Subject: [PATCH 2/5] x --- libs/langchain/langchain/llms/cohere.py | 13 ++++++++----- .../tests/integration_tests/llms/test_cohere.py | 7 +++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/libs/langchain/langchain/llms/cohere.py b/libs/langchain/langchain/llms/cohere.py index 0bf6728ae52f4..61481c1555e1b 100644 --- a/libs/langchain/langchain/llms/cohere.py +++ b/libs/langchain/langchain/llms/cohere.py @@ -74,6 +74,7 @@ class BaseCohere(Serializable): """A non-negative float that tunes the degree of randomness in generation.""" cohere_api_key: Optional[SecretStr] = None + """Cohere API key. If not provided, will be read from the environment variable.""" stop: Optional[List[str]] = None @@ -91,11 +92,13 @@ def validate_environment(cls, values: Dict) -> Dict: "Please install it with `pip install cohere`." ) else: - cohere_api_key = SecretStr( + cohere_api_key = ( get_from_dict_or_env(values, "cohere_api_key", "COHERE_API_KEY") ) - values["client"] = cohere.Client(cohere_api_key) - values["async_client"] = cohere.AsyncClient(cohere_api_key) + values["client"] = cohere.Client(api_key=cohere_api_key.get_secret_value()) + values["async_client"] = cohere.AsyncClient( + api_key=cohere_api_key.get_secret_value() + ) return values @@ -155,8 +158,8 @@ def _default_params(self) -> Dict[str, Any]: } @property - def lc_secrets(self) -> Dict[str, SecretStr]: - return {"cohere_api_key": SecretStr("COHERE_API_KEY")} + def lc_secrets(self) -> Dict[str, str]: + return {"cohere_api_key": "COHERE_API_KEY"} @property def _identifying_params(self) -> Dict[str, Any]: diff --git a/libs/langchain/tests/integration_tests/llms/test_cohere.py b/libs/langchain/tests/integration_tests/llms/test_cohere.py index 4c260982b70de..faa19958f6038 100644 --- a/libs/langchain/tests/integration_tests/llms/test_cohere.py +++ b/libs/langchain/tests/integration_tests/llms/test_cohere.py @@ -3,6 +3,7 @@ from pathlib import Path from langchain.llms.cohere import Cohere +from langchain.pydantic_v1 import SecretStr from langchain.llms.loading import load_llm from tests.integration_tests.llms.utils import assert_llm_equality @@ -14,6 +15,12 @@ def test_cohere_call() -> None: assert isinstance(output, str) +def test_cohere_api_key() -> None: + """Test that cohere api key is a secret key.""" + cohere = Cohere(cohere_api_key="1") + assert isinstance(cohere.cohere_api_key, SecretStr) + + def test_saving_loading_llm(tmp_path: Path) -> None: """Test saving/loading an Cohere LLM.""" llm = Cohere(max_tokens=10) From fa0a03fbf9a0f67227ccad98175019921a1a624d Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Wed, 1 Nov 2023 10:06:21 -0400 Subject: [PATCH 3/5] x --- libs/langchain/langchain/llms/cohere.py | 19 ++++++++----------- .../integration_tests/llms/test_cohere.py | 14 ++++++++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/libs/langchain/langchain/llms/cohere.py b/libs/langchain/langchain/llms/cohere.py index 7d62ec3465bdd..c71b14632f554 100644 --- a/libs/langchain/langchain/llms/cohere.py +++ b/libs/langchain/langchain/llms/cohere.py @@ -19,7 +19,7 @@ from langchain.llms.utils import enforce_stop_tokens from langchain.load.serializable import Serializable from langchain.pydantic_v1 import Extra, Field, SecretStr, root_validator -from langchain.utils import get_from_dict_or_env +from langchain.utils import convert_to_secret_str, get_from_dict_or_env logger = logging.getLogger(__name__) @@ -95,21 +95,18 @@ def validate_environment(cls, values: Dict) -> Dict: "Please install it with `pip install cohere`." ) else: - cohere_api_key = ( + values["cohere_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "cohere_api_key", "COHERE_API_KEY") ) - values["client"] = cohere.Client(api_key=cohere_api_key.get_secret_value()) - values["async_client"] = cohere.AsyncClient( - api_key=cohere_api_key.get_secret_value() - ) -<<<<<<< HEAD -======= client_name = values["user_agent"] - values["client"] = cohere.Client(cohere_api_key, client_name=client_name) + values["client"] = cohere.Client( + api_key=values["cohere_api_key"].get_secret_value(), + client_name=client_name, + ) values["async_client"] = cohere.AsyncClient( - cohere_api_key, client_name=client_name + api_key=values["cohere_api_key"].get_secret_value(), + client_name=client_name, ) ->>>>>>> master return values diff --git a/libs/langchain/tests/integration_tests/llms/test_cohere.py b/libs/langchain/tests/integration_tests/llms/test_cohere.py index faa19958f6038..931d2f4a97bea 100644 --- a/libs/langchain/tests/integration_tests/llms/test_cohere.py +++ b/libs/langchain/tests/integration_tests/llms/test_cohere.py @@ -2,9 +2,11 @@ from pathlib import Path +from pytest import MonkeyPatch + from langchain.llms.cohere import Cohere -from langchain.pydantic_v1 import SecretStr from langchain.llms.loading import load_llm +from langchain.pydantic_v1 import SecretStr from tests.integration_tests.llms.utils import assert_llm_equality @@ -15,10 +17,14 @@ def test_cohere_call() -> None: assert isinstance(output, str) -def test_cohere_api_key() -> None: +def test_cohere_api_key(monkeypatch: MonkeyPatch) -> None: """Test that cohere api key is a secret key.""" - cohere = Cohere(cohere_api_key="1") - assert isinstance(cohere.cohere_api_key, SecretStr) + # test initialization from init + assert isinstance(Cohere(cohere_api_key="1").cohere_api_key, SecretStr) + + # test initialization from env variable + monkeypatch.setenv("COHERE_API_KEY", "secret-api-key") + assert isinstance(Cohere().cohere_api_key, SecretStr) def test_saving_loading_llm(tmp_path: Path) -> None: From 9e7f39bab03f6b11716170d5b9caf1d13f4e8225 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Fri, 1 Mar 2024 15:17:29 -0500 Subject: [PATCH 4/5] x --- libs/langchain/langchain/llms/cohere.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 libs/langchain/langchain/llms/cohere.py diff --git a/libs/langchain/langchain/llms/cohere.py b/libs/langchain/langchain/llms/cohere.py new file mode 100644 index 0000000000000..6d1b24d8b8793 --- /dev/null +++ b/libs/langchain/langchain/llms/cohere.py @@ -0,0 +1,7 @@ +from langchain_community.llms.cohere import ( + Cohere, +) + +__all__ = [ + "Cohere", +] From 3fcdb6c272b2385250967df9a8e61b47cbc8caad Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Fri, 1 Mar 2024 15:21:34 -0500 Subject: [PATCH 5/5] x --- .../langchain_community/llms/cohere.py | 19 ++++++++++++------- .../integration_tests/llms/test_cohere.py | 7 +------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/libs/community/langchain_community/llms/cohere.py b/libs/community/langchain_community/llms/cohere.py index 18a86c4461346..91fc906ebd950 100644 --- a/libs/community/langchain_community/llms/cohere.py +++ b/libs/community/langchain_community/llms/cohere.py @@ -9,8 +9,8 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.load.serializable import Serializable -from langchain_core.pydantic_v1 import Extra, Field, root_validator -from langchain_core.utils import get_from_dict_or_env +from langchain_core.pydantic_v1 import Extra, Field, SecretStr, root_validator +from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env from tenacity import ( before_sleep_log, retry, @@ -73,7 +73,8 @@ class BaseCohere(Serializable): temperature: float = 0.75 """A non-negative float that tunes the degree of randomness in generation.""" - cohere_api_key: Optional[str] = None + cohere_api_key: Optional[SecretStr] = None + """Cohere API key. If not provided, will be read from the environment variable.""" stop: Optional[List[str]] = None @@ -94,13 +95,17 @@ def validate_environment(cls, values: Dict) -> Dict: "Please install it with `pip install cohere`." ) else: - cohere_api_key = get_from_dict_or_env( - values, "cohere_api_key", "COHERE_API_KEY" + values["cohere_api_key"] = convert_to_secret_str( + get_from_dict_or_env(values, "cohere_api_key", "COHERE_API_KEY") ) client_name = values["user_agent"] - values["client"] = cohere.Client(cohere_api_key, client_name=client_name) + values["client"] = cohere.Client( + api_key=values["cohere_api_key"].get_secret_value(), + client_name=client_name, + ) values["async_client"] = cohere.AsyncClient( - cohere_api_key, client_name=client_name + api_key=values["cohere_api_key"].get_secret_value(), + client_name=client_name, ) return values diff --git a/libs/community/tests/integration_tests/llms/test_cohere.py b/libs/community/tests/integration_tests/llms/test_cohere.py index 41687fba7a694..f6aef917ecbe1 100644 --- a/libs/community/tests/integration_tests/llms/test_cohere.py +++ b/libs/community/tests/integration_tests/llms/test_cohere.py @@ -2,16 +2,11 @@ from pathlib import Path -<<<<<<< HEAD:libs/langchain/tests/integration_tests/llms/test_cohere.py +from langchain_core.pydantic_v1 import SecretStr from pytest import MonkeyPatch -from langchain.llms.cohere import Cohere -from langchain.llms.loading import load_llm -from langchain.pydantic_v1 import SecretStr -======= from langchain_community.llms.cohere import Cohere from langchain_community.llms.loading import load_llm ->>>>>>> master:libs/community/tests/integration_tests/llms/test_cohere.py from tests.integration_tests.llms.utils import assert_llm_equality