invincible-jha commited on
Commit
c51db37
·
verified ·
1 Parent(s): 190c369

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -7
app.py CHANGED
@@ -56,8 +56,8 @@ classifier.fit(X, np.arange(len(approved_topics)))
56
  class CommunicationExpertAgent(Agent):
57
  role: Literal["Communication Expert"] = "Communication Expert"
58
  goal: Literal["To interpret and rephrase user queries with empathy and respect"] = "To interpret and rephrase user queries with empathy and respect"
59
- backstory: Literal["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."] = \
60
- "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."
61
 
62
  async def run(self, query):
63
  sanitized_query = re.sub(r'[<>&\']', '', query)
@@ -79,8 +79,8 @@ class CommunicationExpertAgent(Agent):
79
  class ResponseExpertAgent(Agent):
80
  role: Literal["Response Expert"] = "Response Expert"
81
  goal: Literal["To provide accurate, helpful, and emotionally intelligent responses to user queries"] = "To provide accurate, helpful, and emotionally intelligent responses to user queries"
82
- backstory: Literal["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."] = \
83
- "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."
84
 
85
  async def run(self, rephrased_query):
86
  try:
@@ -94,8 +94,8 @@ class ResponseExpertAgent(Agent):
94
  class PostprocessingAgent(Agent):
95
  role: Literal["Postprocessing Expert"] = "Postprocessing Expert"
96
  goal: Literal["To enhance and finalize responses ensuring quality and completeness"] = "To enhance and finalize responses ensuring quality and completeness"
97
- backstory: Literal["You are responsible for finalizing communications, adding polite terminations, and ensuring that the responses meet the quality standards expected in customer interactions."] = \
98
- "You are responsible for finalizing communications, adding polite terminations, and ensuring that the responses meet the quality standards expected in customer interactions."
99
 
100
  def run(self, response):
101
  processed_response = self.add_disclaimers(response)
@@ -114,4 +114,97 @@ class PostprocessingAgent(Agent):
114
 
115
  class RelationshipManagerAgent(Agent):
116
  role: Literal["Relationship Manager"] = "Relationship Manager"
117
- goal: Literal["To elevate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  class CommunicationExpertAgent(Agent):
57
  role: Literal["Communication Expert"] = "Communication Expert"
58
  goal: Literal["To interpret and rephrase user queries with empathy and respect"] = "To interpret and rephrase user queries with empathy and respect"
59
+ backstory: Literal["""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."""] = \
60
+ """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."""
61
 
62
  async def run(self, query):
63
  sanitized_query = re.sub(r'[<>&\']', '', query)
 
79
  class ResponseExpertAgent(Agent):
80
  role: Literal["Response Expert"] = "Response Expert"
81
  goal: Literal["To provide accurate, helpful, and emotionally intelligent responses to user queries"] = "To provide accurate, helpful, and emotionally intelligent responses to user queries"
82
+ backstory: Literal["""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."""] = \
83
+ """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."""
84
 
85
  async def run(self, rephrased_query):
86
  try:
 
94
  class PostprocessingAgent(Agent):
95
  role: Literal["Postprocessing Expert"] = "Postprocessing Expert"
96
  goal: Literal["To enhance and finalize responses ensuring quality and completeness"] = "To enhance and finalize responses ensuring quality and completeness"
97
+ backstory: Literal["""You are responsible for finalizing communications, adding polite terminations, and ensuring that the responses meet the quality standards expected in customer interactions."""] = \
98
+ """You are responsible for finalizing communications, adding polite terminations, and ensuring that the responses meet the quality standards expected in customer interactions."""
99
 
100
  def run(self, response):
101
  processed_response = self.add_disclaimers(response)
 
114
 
115
  class RelationshipManagerAgent(Agent):
116
  role: Literal["Relationship Manager"] = "Relationship Manager"
117
+ goal: Literal["To elevate communication to embody top 1% customer service qualities"] = "To elevate communication to embody top 1% customer service qualities"
118
+ backstory: Literal["""You are an expert in elite-level communication, capable of transforming interactions to build instant rapport, trust, and confidence. Your role is to enhance both user queries and system responses to create a warm, empathetic, and highly professional interaction."""] = \
119
+ """You are an expert in elite-level communication, capable of transforming interactions to build instant rapport, trust, and confidence. Your role is to enhance both user queries and system responses to create a warm, empathetic, and highly professional interaction."""
120
+
121
+ async def rephrase_query(self, query: str) -> str:
122
+ rephrased_query = f"I understand that you're asking about {query}. "
123
+ rephrased_query += "Your question is important, and I'm here to provide you with the best possible assistance. "
124
+ rephrased_query += "To ensure I fully grasp your situation and can offer the most helpful and accurate information, "
125
+ rephrased_query += f"could you please confirm if you want to know about {query}? "
126
+ rephrased_query += "Feel free to add any additional details that might help me serve you better."
127
+ return rephrased_query
128
+
129
+ async def enhance_response(self, response: str) -> str:
130
+ enhanced_response = "I appreciate you bringing this to our attention. "
131
+ enhanced_response += response
132
+ enhanced_response += "\n\nI hope this information effectively addresses your query. "
133
+ enhanced_response += "Is there anything else about this matter that you'd like me to clarify or expand on? "
134
+ enhanced_response += "I'm here to ensure you have all the information you need to make informed decisions about your investments and interactions with Zerodha."
135
+ return enhanced_response
136
+
137
+ async def run(self, query: str, response: str = "") -> Tuple[str, str]:
138
+ rephrased_query = await self.rephrase_query(query)
139
+ if response:
140
+ enhanced_response = await self.enhance_response(response)
141
+ return rephrased_query, enhanced_response
142
+ return rephrased_query, ""
143
+
144
+ # Instantiate agents
145
+ communication_expert = CommunicationExpertAgent()
146
+ response_expert = ResponseExpertAgent()
147
+ postprocessing_agent = PostprocessingAgent()
148
+ relationship_manager = RelationshipManagerAgent()
149
+
150
+ async def handle_query(query):
151
+ try:
152
+ if not query.strip():
153
+ return "I'm sorry, but I didn't receive any query. Could you please ask a question about Zerodha's services?"
154
+
155
+ # Step 1: Rephrase the query using RelationshipManagerAgent
156
+ rephrased_query, _ = await relationship_manager.run(query)
157
+
158
+ # Step 2: Process the rephrased query with CommunicationExpertAgent
159
+ communication_expert_query = await communication_expert.run(rephrased_query)
160
+
161
+ # Step 3: Generate response using ResponseExpertAgent
162
+ initial_response = await response_expert.run(communication_expert_query)
163
+
164
+ # Step 4: Postprocess the response
165
+ postprocessed_response = postprocessing_agent.run(initial_response)
166
+
167
+ # Step 5: Enhance the response using RelationshipManagerAgent
168
+ _, final_response = await relationship_manager.run(query, postprocessed_response)
169
+
170
+ return final_response
171
+ except Exception as e:
172
+ logger.error(f"Error in handle_query: {str(e)}")
173
+ return "I apologize, but an error occurred while processing your request. Please try again or contact Zerodha support if the issue persists."
174
+
175
+ # Gradio interface setup
176
+ def setup_interface():
177
+ with gr.Blocks() as app:
178
+ gr.Markdown("# Zerodha Support Chatbot")
179
+ gr.Markdown("Ask questions about Zerodha's services, trading, account management, and more.")
180
+
181
+ with gr.Row():
182
+ query_input = gr.Textbox(label="Enter your query", placeholder="Type your question here...")
183
+ submit_button = gr.Button("Submit")
184
+
185
+ response_output = gr.Textbox(label="Response", lines=10)
186
+
187
+ submit_button.click(
188
+ fn=lambda x: asyncio.run(handle_query(x)),
189
+ inputs=[query_input],
190
+ outputs=[response_output]
191
+ )
192
+
193
+ gr.Examples(
194
+ examples=[
195
+ "How do I open a Zerodha account?",
196
+ "What are the brokerage charges for intraday trading?",
197
+ "Can you explain how to use the Kite platform?",
198
+ "I'm having trouble logging into my account. What should I do?",
199
+ "What are the margin requirements for F&O trading?"
200
+ ],
201
+ inputs=[query_input]
202
+ )
203
+
204
+ return app
205
+
206
+ app = setup_interface()
207
+
208
+ if __name__ == "__main__":
209
+ app.launch()
210
+ ```