Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HernanGatta committed Aug 29, 2023
1 parent 2c7454b commit 16db125
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions python-package/src/promptguard/promptguard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
from promptguard.authentication import get_api_key
from promptguard.configuration import get_server_config

# Global requests session to leverage connection pooling to in turn avoid
# establishing a new connection for each request to the service.
_session: Optional[requests.Session] = None
_sessionLock: threading.Lock = threading.Lock()

# Protects the global requests session when creating it for the first time.
_session_lock: threading.Lock = threading.Lock()


@dataclass
Expand Down Expand Up @@ -45,8 +49,8 @@ def sanitize(
timeout: Optional[int] = None,
) -> SanitizeResponse:
"""
Takes in a list of text prompts and returns a list of
sanitized texts with PII redacted from it.
Takes in a list of text prompts and returns a list of sanitized texts with
PII redacted from it.
Parameters
----------
Expand All @@ -56,8 +60,8 @@ def sanitize(
Returns
-------
SanitizeResponse
The anonymzied version of input_texts without PII and
a secret entropy value.
The anonymzied version of input_texts without PII and a secret entropy
value.
"""
response = _send_request_to_promptguard_service(
endpoint="sanitize",
Expand Down Expand Up @@ -89,16 +93,21 @@ def desanitize(
timeout: Optional[int] = None,
) -> DesanitizeResponse:
"""
Takes in a sanitized response and returns the desanitized
text with PII added back to it.
Takes in a sanitized response and returns the desanitized text with PII
added back to it.
Parameters
----------
sanitized_text : str
Sanitized response that you want to be desanitized.
secure_context : str
Secret entropy value that should have been returned by
the call to `sanitize`.
Secret entropy value that should have been returned by the call to
`sanitize`.
retries : int, optional
The number of retries to submit a request to the service before giving
up when errors occur.
timeout : int, optional
The number of seconds to wait until a request to the service times out.
Returns
-------
Expand Down Expand Up @@ -127,29 +136,34 @@ def _send_request_to_promptguard_service(
timeout: Optional[int] = None,
) -> str:
"""
Helper method which takes in the name of the endpoint, and a
payload dictionary, and converts it into the form needed to send
the request to the Promptguard service. Returns the response
recieved if its successful, and raises an error otherwise.
Helper method which takes in the name of the endpoint, and a payload
dictionary, and converts it into the form needed to send the request to the
Promptguard service. Returns the response recieved if its successful, and
raises an error otherwise.
Parameters
----------
endpoint : str
The name of the endpoint you are trying to hit
The name of the endpoint you are trying to hit.
payload : dict
The payload of the request as a dictionary
The payload of the request as a dictionary.
retries : int, optional
The number of retries to submit a request to the service before giving
up when errors occur.
timeout : int, optional
The number of seconds to wait until a request to the service times out.
Returns
-------
str
The response body returned by the request, only returned
if the request was successful
The response body returned by the request, only returned if the request
was successful.
"""

global _session
global _sessionLock
global _session_lock

with _sessionLock:
with _session_lock:
if _session is None:
_session = requests.Session()
_session.mount("httpa://", HTTPAAdapter(_get_default_validators()))
Expand Down

0 comments on commit 16db125

Please sign in to comment.