Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,18 +36,57 @@ Key points about Zerodha:
|
|
| 36 |
|
| 37 |
Always prioritize user safety, ethical investing practices, and transparent communication. Never provide information that could mislead users or bring disrepute to Zerodha."""
|
| 38 |
|
| 39 |
-
# Guardrail functions
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
async def generate_response(prompt):
|
| 43 |
try:
|
| 44 |
-
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
logger.error(f"Error generating response: {e}")
|
| 47 |
return "I apologize, but I'm having trouble generating a response at the moment. Please try again later."
|
| 48 |
|
| 49 |
def post_process_response(response):
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# CrewAI setup
|
| 53 |
communication_expert_crew = Agent(
|
|
@@ -91,24 +130,28 @@ async def zerodha_support(message, history, username):
|
|
| 91 |
verbose=2
|
| 92 |
)
|
| 93 |
|
| 94 |
-
rephrased_query = crew.kickoff()
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"Error in CrewAI rephrasing: {e}")
|
| 97 |
rephrased_query = sanitized_message # Fallback to original message if rephrasing fails
|
| 98 |
|
| 99 |
# Generate response using Response Expert
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
if not check_response_content(response):
|
| 114 |
response += "\n\nPlease note that I cannot provide specific investment advice or guarantee returns. For personalized guidance, please consult with a qualified financial advisor."
|
|
|
|
| 36 |
|
| 37 |
Always prioritize user safety, ethical investing practices, and transparent communication. Never provide information that could mislead users or bring disrepute to Zerodha."""
|
| 38 |
|
| 39 |
+
# Guardrail functions
|
| 40 |
+
def sanitize_input(input_text):
|
| 41 |
+
return re.sub(r'[<>&\']', '', input_text)
|
| 42 |
+
|
| 43 |
+
approved_topics = ['account opening', 'trading', 'fees', 'platforms', 'funds', 'regulations', 'support']
|
| 44 |
+
vectorizer = CountVectorizer()
|
| 45 |
+
classifier = MultinomialNB()
|
| 46 |
+
|
| 47 |
+
X = vectorizer.fit_transform(approved_topics)
|
| 48 |
+
y = np.arange(len(approved_topics))
|
| 49 |
+
classifier.fit(X, y)
|
| 50 |
+
|
| 51 |
+
def is_relevant_topic(query):
|
| 52 |
+
query_vector = vectorizer.transform([query])
|
| 53 |
+
prediction = classifier.predict(query_vector)
|
| 54 |
+
return prediction[0] in range(len(approved_topics))
|
| 55 |
+
|
| 56 |
+
def redact_sensitive_info(text):
|
| 57 |
+
text = re.sub(r'\b\d{10,12}\b', '[REDACTED]', text)
|
| 58 |
+
text = re.sub(r'[A-Z]{5}[0-9]{4}[A-Z]', '[REDACTED]', text)
|
| 59 |
+
return text
|
| 60 |
+
|
| 61 |
+
def check_response_content(response):
|
| 62 |
+
unauthorized_patterns = [
|
| 63 |
+
r'\b(guarantee|assured|certain)\b.*\b(returns|profit)\b',
|
| 64 |
+
r'\b(buy|sell)\b.*\b(specific stocks?|shares?)\b'
|
| 65 |
+
]
|
| 66 |
+
return not any(re.search(pattern, response, re.IGNORECASE) for pattern in unauthorized_patterns)
|
| 67 |
+
|
| 68 |
+
def check_confidence(response):
|
| 69 |
+
uncertain_phrases = ["I'm not sure", "It's possible", "I don't have enough information"]
|
| 70 |
+
return not any(phrase.lower() in response.lower() for phrase in uncertain_phrases)
|
| 71 |
|
| 72 |
async def generate_response(prompt):
|
| 73 |
try:
|
| 74 |
+
response = await client.text_generation(prompt, max_new_tokens=500, temperature=0.7)
|
| 75 |
+
return response
|
| 76 |
except Exception as e:
|
| 77 |
logger.error(f"Error generating response: {e}")
|
| 78 |
return "I apologize, but I'm having trouble generating a response at the moment. Please try again later."
|
| 79 |
|
| 80 |
def post_process_response(response):
|
| 81 |
+
response = re.sub(r'\b(stupid|dumb|idiotic|foolish)\b', 'mistaken', response, flags=re.IGNORECASE)
|
| 82 |
+
|
| 83 |
+
if not re.search(r'(Thank you|Is there anything else|Hope this helps|Let me know if you need more information)\s*$', response, re.IGNORECASE):
|
| 84 |
+
response += "\n\nIs there anything else I can help you with regarding Zerodha's services?"
|
| 85 |
+
|
| 86 |
+
if re.search(r'\b(invest|trade|buy|sell|market)\b', response, re.IGNORECASE):
|
| 87 |
+
response += "\n\nPlease note that this information is for educational purposes only and should not be considered as financial advice. Always do your own research and consider consulting with a qualified financial advisor before making investment decisions."
|
| 88 |
+
|
| 89 |
+
return response
|
| 90 |
|
| 91 |
# CrewAI setup
|
| 92 |
communication_expert_crew = Agent(
|
|
|
|
| 130 |
verbose=2
|
| 131 |
)
|
| 132 |
|
| 133 |
+
rephrased_query = await crew.kickoff()
|
| 134 |
except Exception as e:
|
| 135 |
logger.error(f"Error in CrewAI rephrasing: {e}")
|
| 136 |
rephrased_query = sanitized_message # Fallback to original message if rephrasing fails
|
| 137 |
|
| 138 |
# Generate response using Response Expert
|
| 139 |
+
try:
|
| 140 |
+
response_task = Task(
|
| 141 |
+
description=f"Provide an accurate and helpful response to the user query: '{rephrased_query}'",
|
| 142 |
+
agent=response_expert_crew
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
crew = Crew(
|
| 146 |
+
agents=[response_expert_crew],
|
| 147 |
+
tasks=[response_task],
|
| 148 |
+
verbose=2
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
response = await crew.kickoff()
|
| 152 |
+
except Exception as e:
|
| 153 |
+
logger.error(f"Error in CrewAI response generation: {e}")
|
| 154 |
+
response = "I apologize, but I'm having trouble generating a response at the moment. Please try again later."
|
| 155 |
|
| 156 |
if not check_response_content(response):
|
| 157 |
response += "\n\nPlease note that I cannot provide specific investment advice or guarantee returns. For personalized guidance, please consult with a qualified financial advisor."
|