-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #609 from guardrails-ai/gaas-additions
Gaas Additions
- Loading branch information
Showing
10 changed files
with
1,149 additions
and
685 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
from typing import Optional | ||
|
||
from guard_rails_api_client import AuthenticatedClient | ||
from guard_rails_api_client.api.guard import update_guard, validate | ||
from guard_rails_api_client.models import Guard, ValidatePayload | ||
from guard_rails_api_client.types import UNSET | ||
from httpx import Timeout | ||
|
||
|
||
class GuardrailsApiClient: | ||
_client: AuthenticatedClient | ||
base_url: str | ||
api_key: str | ||
|
||
def __init__(self, base_url: Optional[str] = None, api_key: Optional[str] = None): | ||
self.base_url = ( | ||
base_url | ||
if base_url is not None | ||
else os.environ.get("GUARDRAILS_BASE_URL", "http://localhost:8000") | ||
) | ||
self.api_key = ( | ||
api_key if api_key is not None else os.environ.get("GUARDRAILS_API_KEY", "") | ||
) | ||
self._client = AuthenticatedClient( | ||
_base_url=self.base_url, | ||
_follow_redirects=True, | ||
token=self.api_key, | ||
_timeout=Timeout(300), | ||
) | ||
|
||
def upsert_guard(self, guard: Guard): | ||
update_guard.sync(guard_name=guard.name, client=self._client, body=guard) | ||
|
||
def validate( | ||
self, | ||
guard: Guard, | ||
payload: ValidatePayload, | ||
openai_api_key: Optional[str] = None, | ||
): | ||
_openai_api_key = ( | ||
openai_api_key | ||
if openai_api_key is not None | ||
else os.environ.get("OPENAI_API_KEY", UNSET) | ||
) | ||
return validate.sync( | ||
guard_name=guard.name, | ||
client=self._client, | ||
body=payload, | ||
x_openai_api_key=_openai_api_key, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.