Skip to content

Commit afea450

Browse files
authored
Merge pull request #1048 from guardrails-ai/get_or_create
introduces get_or_create to fetch a guard
2 parents 6bb5155 + 3c5c04f commit afea450

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

guardrails/guard.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,3 +1383,25 @@ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional["Guard"]:
13831383
)
13841384
guard.history = Stack(*history)
13851385
return guard
1386+
1387+
# attempts to get a guard from the server
1388+
# if a name is unspecified, the guard will be created on the client
1389+
# in the future, this may create a guard on the server
1390+
@experimental
1391+
@staticmethod
1392+
def fetch_guard(
1393+
name: Optional[str] = None,
1394+
*args,
1395+
**kwargs,
1396+
):
1397+
if not name:
1398+
raise ValueError("Name must be specified to fetch a guard")
1399+
1400+
settings.use_server = True
1401+
api_key = os.environ.get("GUARDRAILS_API_KEY")
1402+
api_client = GuardrailsApiClient(api_key=api_key)
1403+
guard = api_client.fetch_guard(name)
1404+
if guard:
1405+
return Guard(name=name, *args, **kwargs)
1406+
1407+
raise ValueError(f"Guard with name {name} not found")

guardrails/hub/install.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def install(
3737
quiet: bool = True,
3838
install_local_models_confirm: Callable = default_local_models_confirm,
3939
) -> ValidatorModuleType:
40-
"""
41-
Install a validator package from a hub URI.
40+
"""Install a validator package from a hub URI.
4241
4342
Args:
4443
package_uri (str): The URI of the package to install.

tests/integration_tests/test_assets/validators/detect_pii.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ class MockDetectPII(Validator):
6060
}
6161

6262
def chunking_function(self, chunk: str):
63-
"""
64-
Use a sentence tokenizer to split the chunk into sentences.
63+
"""Use a sentence tokenizer to split the chunk into sentences.
6564
66-
Because using the tokenizer is expensive, we only use it if there
67-
is a period present in the chunk.
65+
Because using the tokenizer is expensive, we only use it if
66+
there is a period present in the chunk.
6867
"""
6968
# using the sentence tokenizer is expensive
7069
# we check for a . to avoid wastefully calling the tokenizer

0 commit comments

Comments
 (0)