-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaurdrails.py
117 lines (97 loc) · 3.38 KB
/
gaurdrails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import logging
import anthropic
from dotenv import load_dotenv
import asyncio
import os
import json
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
load_dotenv()
# Guardrails configuration
GUARDRAILS_CONFIG = {
"blocking": [
{
"name": "profanity_filter",
"function": "check_profanity"
},
{
"name": "sentiment_analysis",
"function": "analyze_sentiment"
}
],
"non_blocking": [
{
"name": "log_conversation",
"function": "log_to_database"
},
{
"name": "update_user_stats",
"function": "update_stats"
}
]
}
class Guardrails:
@staticmethod
async def check_profanity(text):
# Simulated profanity check
print("Checking profanity...")
profane_words = ["badword1", "badword2"]
return not any(word in text.lower() for word in profane_words)
@staticmethod
async def analyze_sentiment(text):
# Simulated sentiment analysis
return True
@staticmethod
async def log_to_database(text):
print(f"Logged to DB: {text[:20]}...")
return True
@staticmethod
async def update_stats(text):
print(f"Updated user stats: {len(text)} characters")
return True
async def apply_guardrails(text, guardrail_type):
tasks = []
for guardrail in GUARDRAILS_CONFIG[guardrail_type]:
func = getattr(Guardrails, guardrail['function'])
tasks.append(func(text))
results = await asyncio.gather(*tasks)
return all(results) if guardrail_type == "blocking" else True
# """
# todo:
# improve latency by batching the streaming text to identify profanity/gaurd
# """
async def process_message(message: str, conversation_history: list) -> str:
# Apply blocking guardrails
if not await apply_guardrails(message, "blocking"):
return "I'm sorry, but I can't process that message due to content restrictions."
conversation_history.append({"role": "user", "content": message})
client = anthropic.AsyncAnthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))
try:
logging.debug("Sending message to Anthropic API")
response = await client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1000,
temperature=0.7,
messages=conversation_history
)
logging.debug("Received response from Anthropic API")
ai_message = response.content[0].text
conversation_history.append({"role": "assistant", "content": ai_message})
if not await apply_guardrails(ai_message, "blocking"):
return "I'm sorry, but I can't process that message due to content restrictions."
# Apply non-blocking guardrails
asyncio.create_task(apply_guardrails(ai_message, "non_blocking"))
return ai_message
except Exception as e:
logging.error(f"Error in process_message: {str(e)}")
raise
async def main():
conversation_history = []
while True:
user_input = input("You: ")
if user_input.lower() in ['exit', 'quit', 'bye']:
print("Goodbye!")
break
response = await process_message(user_input, conversation_history)
print(f"AI: {response}")
if __name__ == "__main__":
asyncio.run(main())