Skip to content

Commit

Permalink
Community[patch]use secret str in Tavily and HuggingFaceInferenceEmbe…
Browse files Browse the repository at this point in the history
…ddings (#16109)

So the api keys don't show up in repr's 

Still need to do tests
  • Loading branch information
hinthornw authored Jan 17, 2024
1 parent f3601b0 commit e5cf1e2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions libs/community/langchain_community/embeddings/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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"
Expand Down Expand Up @@ -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."""
Expand All @@ -297,7 +297,7 @@ def _default_api_url(self) -> str:

@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.
Expand Down
8 changes: 4 additions & 4 deletions libs/community/langchain_community/utilities/tavily_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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"
Expand All @@ -16,7 +16,7 @@
class TavilySearchAPIWrapper(BaseModel):
"""Wrapper for Tavily Search API."""

tavily_api_key: str
tavily_api_key: SecretStr

class Config:
"""Configuration for this pydantic object."""
Expand Down Expand Up @@ -45,7 +45,7 @@ def raw_results(
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,
Expand Down Expand Up @@ -126,7 +126,7 @@ async def raw_results_async(
# 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions libs/community/tests/unit_tests/utilities/test_tavily.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions libs/langchain/langchain/smith/evaluation/runner_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def get_aggregate_feedback(
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)
Expand Down

0 comments on commit e5cf1e2

Please sign in to comment.