Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openai key leakages #12473

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libs/langchain/langchain/chat_models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,10 @@ def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
# every reply is primed with <im_start>assistant
num_tokens += 3
return num_tokens

def __repr_args__(self) -> "ReprArgs":
return [
(key, value)
for key, value in super().__repr_args__()
if key != "openai_api_key"
]
7 changes: 7 additions & 0 deletions libs/langchain/langchain/embeddings/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,10 @@ async def aembed_query(self, text: str) -> List[float]:
"""
embeddings = await self.aembed_documents([text])
return embeddings[0]

def __repr_args__(self) -> "ReprArgs":
return [
(key, value)
for key, value in super().__repr_args__()
if key != "openai_api_key"
]
13 changes: 13 additions & 0 deletions libs/langchain/tests/unit_tests/chains/test_key_leakage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate
from langchain.chat_models.openai import ChatOpenAI


@pytest.mark.requires("openai")
def test_openai_key_leakage():
key = "sk-TESTKEY"
input = ChatPromptTemplate.from_messages([SystemMessagePromptTemplate.from_template("Foobar")])
chat = ChatOpenAI(openai_api_key=key)
chain = input | chat
assert key not in str(chain)
assert key not in repr(chain)
8 changes: 8 additions & 0 deletions libs/langchain/tests/unit_tests/chat_models/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ def mock_create(*args: Any, **kwargs: Any) -> Any:
res = llm.predict("bar")
assert res == "Bar Baz"
assert completed


@pytest.mark.requires("openai")
def test_openai_chat_key_leakage():
key = "sk-TESTKEY"
chat = ChatOpenAI(openai_api_key=key)
assert key not in str(chat)
assert key not in repr(chat)
8 changes: 8 additions & 0 deletions libs/langchain/tests/unit_tests/embeddings/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ def test_openai_incorrect_field() -> None:
with pytest.warns(match="not default parameter"):
llm = OpenAIEmbeddings(foo="bar")
assert llm.model_kwargs == {"foo": "bar"}


@pytest.mark.requires("openai")
def test_openai_embeddings_key_leakage():
key = "sk-TESTKEY"
embedder = OpenAIEmbeddings(openai_api_key=key)
assert key not in str(embedder)
assert key not in repr(embedder)
Loading