Skip to content

Instantly share code, notes, and snippets.

@goofansu
Last active December 9, 2024 06:39
Show Gist options
  • Save goofansu/ee731baddc7b51db817f08137d48a95f to your computer and use it in GitHub Desktop.
Save goofansu/ee731baddc7b51db817f08137d48a95f to your computer and use it in GitHub Desktop.

Revisions

  1. Yejun Su revised this gist Dec 9, 2024. 2 changed files with 1 addition and 3 deletions.
    3 changes: 1 addition & 2 deletions client_with_guardrails.py
    Original file line number Diff line number Diff line change
    @@ -14,5 +14,4 @@
    )

    print("Role:", response.choices[0].message.role)
    print("Content:", response.choices[0].message.content)
    print("Valid?", response.guardrails['validation_passed'])
    print("Content:", response.choices[0].message.content)
    1 change: 0 additions & 1 deletion client_without_guardrails.py
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,3 @@

    print("Role:", response.choices[0].message.role)
    print("Content:", response.choices[0].message.content)
    print("Valid?", response.guardrails['validation_passed'])
  2. Yejun Su revised this gist Dec 9, 2024. 2 changed files with 18 additions and 0 deletions.
    File renamed without changes.
    18 changes: 18 additions & 0 deletions client_without_guardrails.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import os
    from openai import OpenAI

    client = OpenAI(
    base_url='https://openrouter.ai/api/v1',
    )

    response = client.chat.completions.create(
    model="anthropic/claude-3-5-haiku",
    messages=[{
    "role": "user",
    "content": "Fake a person email and name."
    }]
    )

    print("Role:", response.choices[0].message.role)
    print("Content:", response.choices[0].message.content)
    print("Valid?", response.guardrails['validation_passed'])
  3. Yejun Su revised this gist Dec 9, 2024. No changes.
  4. Yejun Su revised this gist Dec 9, 2024. No changes.
  5. Yejun Su revised this gist Dec 9, 2024. No changes.
  6. Yejun Su revised this gist Dec 9, 2024. No changes.
  7. Yejun Su revised this gist Dec 9, 2024. No changes.
  8. Yejun Su revised this gist Dec 9, 2024. No changes.
  9. Yejun Su created this gist Dec 9, 2024.
    18 changes: 18 additions & 0 deletions client.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import os
    from openai import OpenAI

    client = OpenAI(
    base_url='http://127.0.0.1:8000/guards/my-first-guard/openai/v1',
    )

    response = client.chat.completions.create(
    model="openrouter/anthropic/claude-3-5-haiku",
    messages=[{
    "role": "user",
    "content": "Fake a person email and name."
    }]
    )

    print("Role:", response.choices[0].message.role)
    print("Content:", response.choices[0].message.content)
    print("Valid?", response.guardrails['validation_passed'])
    13 changes: 13 additions & 0 deletions config.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    import os
    from guardrails import Guard
    from guardrails.hub import (
    DetectPII,
    GibberishText
    )

    guard = Guard()
    guard.name = 'my-first-guard'
    guard.use_many(
    DetectPII(pii_entities="pii", on_fail="fix"),
    GibberishText(on_fail="fix"),
    )