Skip to content

The agent engineering platform.

Community[patch]use secret str in Tavily and HuggingFaceInferenceEmbeddings (#16109)

William FHe5cf1e2414a2
diff --git a/libs/community/langchain_community/embeddings/huggingface.py b/libs/community/langchain_community/embeddings/huggingface.py
index 84a568866f..068aafa1fe 100644
--- a/libs/community/langchain_community/embeddings/huggingface.py
+++ b/libs/community/langchain_community/embeddings/huggingface.py
@@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional
import requests
from langchain_core.embeddings import Embeddings
-from langchain_core.pydantic_v1 import BaseModel, Extra, Field
+from langchain_core.pydantic_v1 import BaseModel, Extra, Field, SecretStr
DEFAULT_MODEL_NAME = "sentence-transformers/all-mpnet-base-v2"
DEFAULT_INSTRUCT_MODEL = "hkunlp/instructor-large"
@@ -275,7 +275,7 @@ class HuggingFaceInferenceAPIEmbeddings(BaseModel, Embeddings):
Requires a HuggingFace Inference API key and a model name.
"""
- api_key: str
+ api_key: SecretStr
"""Your API key for the HuggingFace Inference API."""
model_name: str = "sentence-transformers/all-MiniLM-L6-v2"
"""The name of the model to use for text embeddings."""
@@ -297,7 +297,7 @@ class HuggingFaceInferenceAPIEmbeddings(BaseModel, Embeddings):
@property
def _headers(self) -> dict:
- return {"Authorization": f"Bearer {self.api_key}"}
+ return {"Authorization": f"Bearer {self.api_key.get_secret_value()}"}
def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Get the embeddings for a list of texts.
diff --git a/libs/community/langchain_community/utilities/tavily_search.py b/libs/community/langchain_community/utilities/tavily_search.py
index 54cd0810cc..97dc45363f 100644
--- a/libs/community/langchain_community/utilities/tavily_search.py
+++ b/libs/community/langchain_community/utilities/tavily_search.py
@@ -7,7 +7,7 @@ from typing import Dict, List, Optional
import aiohttp
import requests
-from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
+from langchain_core.pydantic_v1 import BaseModel, Extra, SecretStr, root_validator
from langchain_core.utils import get_from_dict_or_env
TAVILY_API_URL = "https://api.tavily.com"
@@ -16,7 +16,7 @@ TAVILY_API_URL = "https://api.tavily.com"
class TavilySearchAPIWrapper(BaseModel):
"""Wrapper for Tavily Search API."""
- tavily_api_key: str
+ tavily_api_key: SecretStr
class Config:
"""Configuration for this pydantic object."""
@@ -45,7 +45,7 @@ class TavilySearchAPIWrapper(BaseModel):
include_images: Optional[bool] = False,
) -> Dict:
params = {
- "api_key": self.tavily_api_key,
+ "api_key": self.tavily_api_key.get_secret_value(),
"query": query,
"max_results": max_results,
"search_depth": search_depth,
@@ -126,7 +126,7 @@ class TavilySearchAPIWrapper(BaseModel):
# Function to perform the API call
async def fetch() -> str:
params = {
- "api_key": self.tavily_api_key,
+ "api_key": self.tavily_api_key.get_secret_value(),
"query": query,
"max_results": max_results,
"search_depth": search_depth,
diff --git a/libs/community/tests/unit_tests/embeddings/test_huggingface.py b/libs/community/tests/unit_tests/embeddings/test_huggingface.py
new file mode 100644
index 0000000000..22f91acb8a
--- /dev/null
+++ b/libs/community/tests/unit_tests/embeddings/test_huggingface.py
@@ -0,0 +1,7 @@
+from langchain_community.embeddings.huggingface import HuggingFaceInferenceAPIEmbeddings
+
+
+def test_hugginggface_inferenceapi_embedding_documents_init() -> None:
+ """Test huggingface embeddings."""
+ embedding = HuggingFaceInferenceAPIEmbeddings(api_key="abcd123")
+ assert "abcd123" not in repr(embedding)
diff --git a/libs/community/tests/unit_tests/utilities/test_tavily.py b/libs/community/tests/unit_tests/utilities/test_tavily.py
new file mode 100644
index 0000000000..751bca0cca
--- /dev/null
+++ b/libs/community/tests/unit_tests/utilities/test_tavily.py
@@ -0,0 +1,7 @@
+from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper
+
+
+def test_api_wrapper_api_key_not_visible() -> None:
+ """Test that an exception is raised if the API key is not present."""
+ wrapper = TavilySearchAPIWrapper(tavily_api_key="abcd123")
+ assert "abcd123" not in repr(wrapper)
diff --git a/libs/langchain/langchain/smith/evaluation/runner_utils.py b/libs/langchain/langchain/smith/evaluation/runner_utils.py
index 6a98e4a73e..c79b0fc4c8 100644
--- a/libs/langchain/langchain/smith/evaluation/runner_utils.py
+++ b/libs/langchain/langchain/smith/evaluation/runner_utils.py
@@ -97,6 +97,7 @@ class TestResult(dict):
for col in df.columns
if col.startswith("inputs.")
or col.startswith("outputs.")
+ or col in {"input", "output"}
or col.startswith("reference")
]
return df.describe(include="all").drop(to_drop, axis=1)