From b5acb91080d7c11aee70cf5fc280e558661f83f0 Mon Sep 17 00:00:00 2001 From: Anas Khan Date: Fri, 19 Jul 2024 11:23:34 -0400 Subject: [PATCH] Mask API keys for various LLM/ChatModel Modules (#13885) **Description:** - Added masking of the API Keys for the modules: - `langchain/chat_models/openai.py` - `langchain/llms/openai.py` - `langchain/llms/google_palm.py` - `langchain/chat_models/google_palm.py` - `langchain/llms/edenai.py` - Updated the modules to utilize `SecretStr` from pydantic to securely manage API key. - Added unit/integration tests - `langchain/chat_models/asure_openai.py` used the `open_api_key` that is derived from the `ChatOpenAI` Class and it was assuming `openai_api_key` is a str so we changed it to expect `SecretStr` instead. **Issue:** https://github.com/langchain-ai/langchain/issues/12165 , **Dependencies:** none, **Tag maintainer:** @eyurtsev --------- Co-authored-by: HassanA01 Co-authored-by: Aneeq Hassan Co-authored-by: kristinspenc Co-authored-by: faisalt14 Co-authored-by: Harshil-Patel28 <76663814+Harshil-Patel28@users.noreply.github.com> Co-authored-by: kristinspenc <146893228+kristinspenc@users.noreply.github.com> Co-authored-by: faisalt14 <90787271+faisalt14@users.noreply.github.com> Co-authored-by: Chester Curme --- .../tests/integration_tests/llms/test_edenai.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libs/community/tests/integration_tests/llms/test_edenai.py b/libs/community/tests/integration_tests/llms/test_edenai.py index 5fbaa9fd06c..3d8e0b9e230 100644 --- a/libs/community/tests/integration_tests/llms/test_edenai.py +++ b/libs/community/tests/integration_tests/llms/test_edenai.py @@ -9,6 +9,8 @@ clicking on the 'sandbox' toggle. You'll then need to set EDENAI_API_KEY environment variable to your api key. """ +from langchain_core.pydantic_v1 import SecretStr + from langchain_community.llms import EdenAI @@ -45,3 +47,13 @@ def test_edenai_call_with_old_params() -> None: assert llm.feature == "text" assert llm.subfeature == "generation" assert isinstance(output, str) + + +def test_api_key_is_secret_string() -> None: + llm = EdenAI(provider="openai", edenai_api_key="secret-api-key") + assert isinstance(llm.edenai_api_key, SecretStr) + + +def test_uses_actual_secret_value() -> None: + llm = EdenAI(provider="openai", edenai_api_key="secret-api-key") + assert llm.edenai_api_key.get_secret_value() == "secret-api-key"