Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ import numpy as np
|
|
8 |
from sklearn.feature_extraction.text import CountVectorizer
|
9 |
from sklearn.naive_bayes import MultinomialNB
|
10 |
import asyncio
|
|
|
|
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -20,8 +22,8 @@ except Exception as e:
|
|
20 |
logger.error(f"Failed to initialize InferenceClient: {e}")
|
21 |
sys.exit(1)
|
22 |
|
23 |
-
# Shared context for
|
24 |
-
SHARED_CONTEXT = """You are
|
25 |
|
26 |
Key points about Zerodha:
|
27 |
1. India's largest discount broker, known for innovative technology and low-cost trading.
|
@@ -85,6 +87,59 @@ def post_process_response(response):
|
|
85 |
|
86 |
return response
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
# Main function
|
89 |
async def zerodha_support(message, history):
|
90 |
try:
|
@@ -95,9 +150,29 @@ async def zerodha_support(message, history):
|
|
95 |
|
96 |
sanitized_message = redact_sensitive_info(sanitized_message)
|
97 |
|
98 |
-
#
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
if not check_response_content(response):
|
103 |
response += "\n\nPlease note that I cannot provide specific investment advice or guarantee returns. For personalized guidance, please consult with a qualified financial advisor."
|
@@ -118,7 +193,7 @@ demo = gr.ChatInterface(
|
|
118 |
chatbot=gr.Chatbot(height=600),
|
119 |
textbox=gr.Textbox(placeholder="Ask your question about Zerodha here...", container=False, scale=7),
|
120 |
title="Zerodha Support Assistant",
|
121 |
-
description="Ask questions about Zerodha's services, trading, account management, and more. Our system ensures respectful and empathetic responses.",
|
122 |
theme="soft",
|
123 |
examples=[
|
124 |
"How do I open a Zerodha account?",
|
|
|
8 |
from sklearn.feature_extraction.text import CountVectorizer
|
9 |
from sklearn.naive_bayes import MultinomialNB
|
10 |
import asyncio
|
11 |
+
from crewai import Agent as CrewAgent, Task, Crew
|
12 |
+
import autogen
|
13 |
|
14 |
# Set up logging
|
15 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
22 |
logger.error(f"Failed to initialize InferenceClient: {e}")
|
23 |
sys.exit(1)
|
24 |
|
25 |
+
# Shared context for both agents
|
26 |
+
SHARED_CONTEXT = """You are part of a multi-agent system designed to provide respectful, empathetic, and accurate support for Zerodha, a leading Indian financial services company. Your role is crucial in ensuring all interactions uphold the highest standards of customer service while maintaining Zerodha's excellent reputation.
|
27 |
|
28 |
Key points about Zerodha:
|
29 |
1. India's largest discount broker, known for innovative technology and low-cost trading.
|
|
|
87 |
|
88 |
return response
|
89 |
|
90 |
+
# CrewAI and AutoGen setup
|
91 |
+
communication_expert_crew = CrewAgent(
|
92 |
+
role='Communication Expert',
|
93 |
+
goal='Interpret and rephrase user queries with empathy and respect',
|
94 |
+
backstory="""You are an expert in communication, specializing in understanding and rephrasing queries to ensure they are interpreted in the most positive and constructive light. Your role is crucial in setting the tone for respectful and empathetic interactions.""",
|
95 |
+
verbose=True,
|
96 |
+
allow_delegation=False,
|
97 |
+
tools=[generate_response]
|
98 |
+
)
|
99 |
+
|
100 |
+
response_expert_crew = CrewAgent(
|
101 |
+
role='Response Expert',
|
102 |
+
goal='Provide accurate, helpful, and emotionally intelligent responses to user queries',
|
103 |
+
backstory="""You are an expert in Zerodha's services and policies, with a keen ability to provide comprehensive and empathetic responses. Your role is to ensure that all user queries are addressed accurately while maintaining a respectful and supportive tone.""",
|
104 |
+
verbose=True,
|
105 |
+
allow_delegation=False,
|
106 |
+
tools=[generate_response]
|
107 |
+
)
|
108 |
+
|
109 |
+
communication_expert_autogen = autogen.AssistantAgent(
|
110 |
+
name="Communication_Expert",
|
111 |
+
system_message=SHARED_CONTEXT + """
|
112 |
+
As the Communication Expert, your primary role is to interpret user queries with the utmost respect and empathy. You should:
|
113 |
+
1. Rephrase the user's query to ensure it's understood in the most positive and constructive light.
|
114 |
+
2. Identify and highlight any emotional subtext or concerns in the query.
|
115 |
+
3. Frame the query in a way that invites a supportive and informative response.
|
116 |
+
4. Ensure that any potential complaints or frustrations are acknowledged respectfully.
|
117 |
+
|
118 |
+
Your output should be a rephrased version of the user's query that maintains its original intent while setting the stage for an empathetic and respectful response.""",
|
119 |
+
llm_config={"config_list": [{"model": "gpt-3.5-turbo"}]}
|
120 |
+
)
|
121 |
+
|
122 |
+
response_expert_autogen = autogen.AssistantAgent(
|
123 |
+
name="Response_Expert",
|
124 |
+
system_message=SHARED_CONTEXT + """
|
125 |
+
As the Response Expert, your role is to provide accurate, helpful, and emotionally intelligent responses to user queries. You should:
|
126 |
+
1. Address the user's question or concern directly and comprehensively.
|
127 |
+
2. Maintain a tone of respect and empathy throughout your response.
|
128 |
+
3. Provide clear, factual information about Zerodha's services and policies.
|
129 |
+
4. When discussing financial matters, include appropriate disclaimers and encourage users to seek professional advice for complex decisions.
|
130 |
+
5. For complaints or concerns, acknowledge them respectfully and provide constructive guidance or escalation paths.
|
131 |
+
6. Always uphold Zerodha's reputation for transparency and user-centric service.
|
132 |
+
|
133 |
+
Your output should be a complete, informative response that addresses the user's query while demonstrating empathy and respect.""",
|
134 |
+
llm_config={"config_list": [{"model": "gpt-3.5-turbo"}]}
|
135 |
+
)
|
136 |
+
|
137 |
+
user_proxy = autogen.UserProxyAgent(
|
138 |
+
name="User_Proxy",
|
139 |
+
human_input_mode="NEVER",
|
140 |
+
max_consecutive_auto_reply=1
|
141 |
+
)
|
142 |
+
|
143 |
# Main function
|
144 |
async def zerodha_support(message, history):
|
145 |
try:
|
|
|
150 |
|
151 |
sanitized_message = redact_sensitive_info(sanitized_message)
|
152 |
|
153 |
+
# Use crewAI for initial query rephrasing
|
154 |
+
rephrase_task = Task(
|
155 |
+
description=f"Rephrase the following user query with empathy and respect: '{sanitized_message}'",
|
156 |
+
agent=communication_expert_crew
|
157 |
+
)
|
158 |
+
|
159 |
+
crew = Crew(
|
160 |
+
agents=[communication_expert_crew],
|
161 |
+
tasks=[rephrase_task],
|
162 |
+
verbose=2
|
163 |
+
)
|
164 |
+
|
165 |
+
rephrased_query = crew.kickoff()
|
166 |
+
|
167 |
+
# Use AutoGen for generating the response
|
168 |
+
async def get_autogen_response():
|
169 |
+
await user_proxy.a_initiate_chat(
|
170 |
+
response_expert_autogen,
|
171 |
+
message=f"Please provide a respectful and empathetic response to the following query: '{rephrased_query}'"
|
172 |
+
)
|
173 |
+
return response_expert_autogen.last_message()["content"]
|
174 |
+
|
175 |
+
response = await get_autogen_response()
|
176 |
|
177 |
if not check_response_content(response):
|
178 |
response += "\n\nPlease note that I cannot provide specific investment advice or guarantee returns. For personalized guidance, please consult with a qualified financial advisor."
|
|
|
193 |
chatbot=gr.Chatbot(height=600),
|
194 |
textbox=gr.Textbox(placeholder="Ask your question about Zerodha here...", container=False, scale=7),
|
195 |
title="Zerodha Support Assistant",
|
196 |
+
description="Ask questions about Zerodha's services, trading, account management, and more. Our multi-agent system ensures respectful and empathetic responses.",
|
197 |
theme="soft",
|
198 |
examples=[
|
199 |
"How do I open a Zerodha account?",
|