Last active
December 9, 2024 06:39
-
-
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)
This file contains hidden or 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
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) |
This file contains hidden or 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
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) |
This file contains hidden or 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
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"), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: