Skip to content

Commit a1e519c

Browse files
Assign api_key on init call
1 parent b07c90b commit a1e519c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,7 @@ cython_debug/
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
.idea/
161161

162+
.vscode/
163+
162164
.pdm-python
163165
.python-version

src/llmwhisperer/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import requests
2424

25+
from llmwhisperer.utils import LLMWhispererUtils
26+
2527
BASE_URL = "https://llmwhisperer-api.unstract.com/v1"
2628

2729

@@ -110,6 +112,9 @@ def __init__(
110112

111113
if api_key == "":
112114
self.api_key = os.getenv("LLMWHISPERER_API_KEY", "")
115+
else:
116+
self.api_key = api_key
117+
self.logger.debug("api_key set to %s", LLMWhispererUtils.redact_key(self.api_key))
113118

114119
self.api_timeout = api_timeout
115120

src/llmwhisperer/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class LLMWhispererUtils:
2+
@staticmethod
3+
def redact_key(api_key: str, reveal_length=4) -> str:
4+
"""Hides sensitive information partially. Useful for logging keys.
5+
6+
Args:
7+
api_key (str): API key to redact
8+
9+
Returns:
10+
str: Redacted API key
11+
"""
12+
if not isinstance(api_key, str):
13+
raise ValueError("API key must be a string")
14+
15+
if reveal_length < 0:
16+
raise ValueError("Reveal length must be a non-negative integer")
17+
18+
redacted_length = max(len(api_key) - reveal_length, 0)
19+
revealed_part = api_key[:reveal_length]
20+
redacted_part = "x" * redacted_length
21+
return revealed_part + redacted_part

0 commit comments

Comments
 (0)