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.
Use Guardrails AI with OpenRouter (you need to set OPENAI_API_KEY environment variable to the OpenRouter API key)
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)
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)
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"),
)
@goofansu
Copy link
Author

goofansu commented Dec 9, 2024

Output:

  • With Guardrails AI
Role: None
Content: Here's a fictional email and name:

Name: <PERSON>
Email: <EMAIL_ADDRESS>

Please note that this is a completely made-up example and does not represent a real person.
  • Without Guardrails AI
Role: assistant
Content: Here's a randomly generated fictional name and email:

Name: Emily Rodriguez
Email: [email protected]

Please note that this is a completely fabricated example and not a real person.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment