Skip to content
Merged
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
17 changes: 17 additions & 0 deletions demo-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from guardrails import Guard
from guardrails.utils.telemetry_utils import default_otlp_tracer
from guardrails.validators import RegexMatch
import openai

guard = Guard.from_string(
validators=[RegexMatch(regex="w.*", on_fail="reask")],
reask_instructions="The response must begin with the letter 'w'",
tracer=default_otlp_tracer()
)

guard(
llm_api=openai.chat.completions.create,
prompt="Write me a paragraph about computer science"
)

print(guard.history.last.validated_output)
5 changes: 5 additions & 0 deletions docs/telemetry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. Go to grafana
2. search for opentelemetry in data sources
3. Create a token, paste in the resulting file to otel-collector-config.yml
4. docker-compose up -f telemetry/docker-compose.yml up
5. run a guard and look for traces
38 changes: 38 additions & 0 deletions guardrails/cli_dir/hub/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
from dataclasses import dataclass
from os.path import expanduser
from typing import Optional

from guardrails.cli_dir.logger import logger
from guardrails.cli_dir.server.serializeable import Serializeable


@dataclass
class Credentials(Serializeable):
id: Optional[str] = None
client_id: Optional[str] = None
client_secret: Optional[str] = None
no_metrics: Optional[bool] = False

@staticmethod
def from_rc_file() -> "Credentials":
try:
home = expanduser("~")
guardrails_rc = os.path.join(home, ".guardrailsrc")
with open(guardrails_rc) as rc_file:
lines = rc_file.readlines()
creds = {}
for line in lines:
key, value = line.split("=", 1)
creds[key.strip()] = value.strip()
rc_file.close()
return Credentials.from_dict(creds)

except FileNotFoundError as e:
logger.error(e)
logger.error(
"Guardrails Hub credentials not found!"
"You will need to sign up to use any authenticated Validators here:"
"{insert url}"
)
return Credentials()
20 changes: 20 additions & 0 deletions guardrails/cli_dir/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
import os

import coloredlogs

os.environ[
"COLOREDLOGS_LEVEL_STYLES"
] = "spam=white,faint;success=green,bold;debug=magenta;verbose=blue;notice=cyan,bold;warning=yellow;error=red;critical=background=red" # noqa
LEVELS = {
"SPAM": 5,
"VERBOSE": 15,
"NOTICE": 25,
"SUCCESS": 35,
}
for key in LEVELS:
logging.addLevelName(LEVELS.get(key), key) # type: ignore


logger = logging.getLogger("guardrails-cli")
coloredlogs.install(level="DEBUG", logger=logger)
33 changes: 33 additions & 0 deletions guardrails/cli_dir/server/serializeable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import inspect
import json
from dataclasses import InitVar, asdict, dataclass, field, is_dataclass
from json import JSONEncoder
from typing import Any, Dict


class SerializeableJSONEncoder(JSONEncoder):
def default(self, o):
if is_dataclass(o):
return asdict(o)
return super().default(o)


@dataclass
class Serializeable:
encoder: InitVar[JSONEncoder] = field(
kw_only=True, default=SerializeableJSONEncoder # type: ignore
)

@classmethod
def from_dict(cls, data: Dict[str, Any]):
annotations = inspect.get_annotations(cls)
attributes = dict.keys(annotations)
kwargs = {k: data.get(k) for k in data if k in attributes}
return cls(**kwargs) # type: ignore

@property
def __dict__(self) -> Dict[str, Any]:
return asdict(self)

def to_json(self):
return json.dumps(self, cls=self.encoder) # type: ignore
Loading