tanhm07@hotmail.com commited on
Commit
f1e559a
·
1 Parent(s): f2dc38c

less structured and templates buttons

Browse files
Files changed (1) hide show
  1. app.py +250 -22
app.py CHANGED
@@ -4,55 +4,283 @@ from openai import OpenAI
4
 
5
  client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
6
 
7
- FORMAT_INSTRUCTION = """
8
- Please format your response exactly as:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  Intent:
11
  Priority:
12
  Owner:
13
- Action Required:
14
  Reasoning:
15
  Draft Reply:
 
 
 
 
 
 
16
  """
17
 
18
- def run_agent(system_prompt, email):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  try:
20
  response = client.chat.completions.create(
21
  model="gpt-4o-mini",
22
  messages=[
23
  {"role": "system", "content": system_prompt},
24
- {"role": "user", "content": email + "\n\n" + FORMAT_INSTRUCTION},
25
  ],
26
  temperature=0.4,
27
  )
28
- return response.choices[0].message.content
 
 
 
 
 
 
29
 
30
  except Exception as e:
31
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
32
 
33
- with gr.Blocks() as demo:
34
  with gr.Row():
35
- with gr.Column():
36
  gr.Markdown("## Agent Builder")
 
 
 
 
 
 
 
 
 
37
  system_prompt = gr.Textbox(
38
  label="System Prompt",
39
- lines=15,
40
- value="You are an assistant that processes emails."
 
41
  )
42
 
43
- with gr.Column():
44
- gr.Markdown("## Email Input")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  email = gr.Textbox(
46
- label="Email",
47
- lines=15,
48
- value="We have not received our shipment and this is causing major delays. Please respond urgently."
 
49
  )
50
 
51
- with gr.Column():
52
- gr.Markdown("## Output")
53
- output = gr.Markdown()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- run_btn = gr.Button("Run Agent")
56
- run_btn.click(run_agent, inputs=[system_prompt, email], outputs=output)
 
 
 
57
 
58
- demo.launch()
 
4
 
5
  client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
6
 
7
+ BASIC_PROMPT = """You are a helpful assistant that processes emails."""
8
+
9
+ STRUCTURED_PROMPT = """You are an email triage agent.
10
+
11
+ Your job is to read an email and decide how it should be handled.
12
+
13
+ For each email:
14
+ - Identify the main intent
15
+ - Classify priority as Low, Medium, or High
16
+ - Decide which team should own it
17
+ - Suggest the next action
18
+ - Draft a reply
19
+
20
+ Format your response clearly and use labels.
21
+ """
22
+
23
+ ADVANCED_PROMPT = """You are an enterprise email triage agent.
24
+
25
+ Your job is to analyze incoming emails and decide how they should be handled.
26
+
27
+ For each email:
28
+ 1. Identify the primary intent
29
+ 2. Classify priority as Low, Medium, or High
30
+ 3. Assign an owner from: Sales, Support, Finance, HR, Operations, Legal
31
+ 4. Determine the next action from: Respond, Escalate, Delegate, Request More Information
32
+ 5. Briefly explain your reasoning
33
+ 6. Draft a professional reply
34
+
35
+ Output format:
36
 
37
  Intent:
38
  Priority:
39
  Owner:
40
+ Action:
41
  Reasoning:
42
  Draft Reply:
43
+
44
+ Rules:
45
+ - Be concise and practical
46
+ - If the email is ambiguous, say so clearly
47
+ - If there is urgency, reflect that in both priority and action
48
+ - Keep the draft reply professional and appropriate for business use
49
  """
50
 
51
+ SAMPLE_EMAILS = {
52
+ "Customer Complaint": """Subject: Delayed shipment and no updates
53
+
54
+ We placed our order nearly two weeks ago and still have not received the shipment. We have also not had any meaningful update from your team despite following up twice.
55
+
56
+ This delay is now affecting our own delivery commitments to customers. Please let us know what is going on and when we can expect resolution.
57
+
58
+ Regards,
59
+ A frustrated customer""",
60
+
61
+ "Sales Inquiry": """Subject: Exploring vendors for regional rollout
62
+
63
+ Hello,
64
+
65
+ We are evaluating potential partners for a Southeast Asia rollout later this year. We would like to understand your capabilities, pricing approach, and implementation timeline.
66
+
67
+ Could someone from your team reach out to discuss next steps?
68
+
69
+ Best,
70
+ Regional Director""",
71
+
72
+ "Internal Finance": """Subject: Q3 budget clarification needed
73
+
74
+ Hi Finance team,
75
+
76
+ Can you confirm whether the additional marketing spend for Q3 has been approved? We need clarity before committing to the campaign launch next week.
77
+
78
+ Thanks,
79
+ Head of Marketing""",
80
+
81
+ "HR Issue": """Subject: Concern about manager conduct
82
+
83
+ Hello,
84
+
85
+ I would like to report a concern regarding repeated inappropriate comments made by my manager during team meetings. I am uncomfortable raising this directly with them and would appreciate guidance on how this should be handled.
86
+
87
+ Please keep this confidential.
88
+
89
+ Thank you.""",
90
+
91
+ "Urgent Escalation": """Subject: Production outage affecting client operations
92
+
93
+ Team,
94
+
95
+ A major system outage started 30 minutes ago and several enterprise clients are unable to access critical workflows. We are already receiving escalation calls.
96
+
97
+ We need immediate support and clear ownership on incident response.
98
+
99
+ Please advise urgently.""",
100
+ }
101
+
102
+
103
+ def format_output(raw_text: str) -> str:
104
+ """
105
+ Light formatting only.
106
+ We do NOT enforce a schema.
107
+ We simply make common label-based outputs easier to read.
108
+ """
109
+ if not raw_text:
110
+ return "No output received."
111
+
112
+ known_labels = [
113
+ "Intent:",
114
+ "Priority:",
115
+ "Owner:",
116
+ "Action:",
117
+ "Action Required:",
118
+ "Reasoning:",
119
+ "Draft Reply:",
120
+ ]
121
+
122
+ lines = raw_text.splitlines()
123
+ formatted_lines = []
124
+
125
+ for line in lines:
126
+ stripped = line.strip()
127
+ matched_label = next((label for label in known_labels if stripped.startswith(label)), None)
128
+
129
+ if matched_label:
130
+ value = stripped[len(matched_label):].strip()
131
+ formatted_lines.append(f"### {matched_label[:-1]}")
132
+ if value:
133
+ formatted_lines.append(value)
134
+ else:
135
+ formatted_lines.append("")
136
+ formatted_lines.append("")
137
+ else:
138
+ formatted_lines.append(line)
139
+
140
+ return "\n".join(formatted_lines).strip()
141
+
142
+
143
+ def run_agent(system_prompt, email, show_raw_output):
144
+ if not system_prompt or not system_prompt.strip():
145
+ return "Please enter a system prompt.", ""
146
+
147
+ if not email or not email.strip():
148
+ return "Please enter an email to analyze.", ""
149
+
150
  try:
151
  response = client.chat.completions.create(
152
  model="gpt-4o-mini",
153
  messages=[
154
  {"role": "system", "content": system_prompt},
155
+ {"role": "user", "content": email},
156
  ],
157
  temperature=0.4,
158
  )
159
+
160
+ raw_output = response.choices[0].message.content or ""
161
+ formatted_output = format_output(raw_output)
162
+
163
+ if show_raw_output:
164
+ return formatted_output, raw_output
165
+ return formatted_output, ""
166
 
167
  except Exception as e:
168
+ error_message = f"Error: {str(e)}"
169
+ return error_message, error_message
170
+
171
+
172
+ with gr.Blocks(title="Executive Agent Lab") as demo:
173
+ gr.Markdown("# Executive Agent Lab")
174
+ gr.Markdown(
175
+ "Build your own email triage agent by writing the system prompt, then test it on sample or custom emails."
176
+ )
177
 
 
178
  with gr.Row():
179
+ with gr.Column(scale=1):
180
  gr.Markdown("## Agent Builder")
181
+ gr.Markdown(
182
+ "Define the agent's role, rules, decision logic, and output format."
183
+ )
184
+
185
+ agent_name = gr.Textbox(
186
+ label="Agent Name (optional)",
187
+ placeholder="Email Triage Agent",
188
+ )
189
+
190
  system_prompt = gr.Textbox(
191
  label="System Prompt",
192
+ lines=18,
193
+ value=BASIC_PROMPT,
194
+ placeholder="Write the instructions for your agent here..."
195
  )
196
 
197
+ gr.Markdown("### Starter Prompts")
198
+ with gr.Row():
199
+ load_basic_btn = gr.Button("Load Basic")
200
+ load_structured_btn = gr.Button("Load Structured")
201
+ load_advanced_btn = gr.Button("Load Advanced")
202
+
203
+ with gr.Accordion("What makes a good agent prompt?", open=False):
204
+ gr.Markdown(
205
+ """- role
206
+ - task
207
+ - decision criteria
208
+ - routing rules
209
+ - output format
210
+ - tone
211
+ - handling ambiguity"""
212
+ )
213
+
214
+ with gr.Column(scale=1):
215
+ gr.Markdown("## Test Input")
216
+ gr.Markdown("Load a sample email or paste your own.")
217
+
218
+ gr.Markdown("### Sample Emails")
219
+ with gr.Row():
220
+ btn_customer = gr.Button("Customer Complaint")
221
+ btn_sales = gr.Button("Sales Inquiry")
222
+ with gr.Row():
223
+ btn_finance = gr.Button("Internal Finance")
224
+ btn_hr = gr.Button("HR Issue")
225
+ with gr.Row():
226
+ btn_urgent = gr.Button("Urgent Escalation")
227
+
228
  email = gr.Textbox(
229
+ label="Email Content",
230
+ lines=20,
231
+ value=SAMPLE_EMAILS["Customer Complaint"],
232
+ placeholder="Paste or type an email here..."
233
  )
234
 
235
+ run_btn = gr.Button("Run Agent", variant="primary")
236
+
237
+ with gr.Column(scale=1):
238
+ gr.Markdown("## Agent Output")
239
+ gr.Markdown(
240
+ "The app lightly formats common label-based outputs, but your prompt determines the structure."
241
+ )
242
+
243
+ formatted_output = gr.Markdown(
244
+ value="Run the agent to see results here."
245
+ )
246
+
247
+ show_raw_output = gr.Checkbox(
248
+ label="Show raw output",
249
+ value=False
250
+ )
251
+
252
+ raw_output = gr.Textbox(
253
+ label="Raw Output",
254
+ lines=12,
255
+ visible=True
256
+ )
257
+
258
+ def load_basic():
259
+ return BASIC_PROMPT
260
+
261
+ def load_structured():
262
+ return STRUCTURED_PROMPT
263
+
264
+ def load_advanced():
265
+ return ADVANCED_PROMPT
266
+
267
+ def load_email(sample_name):
268
+ return SAMPLE_EMAILS[sample_name]
269
+
270
+ load_basic_btn.click(fn=load_basic, outputs=system_prompt)
271
+ load_structured_btn.click(fn=load_structured, outputs=system_prompt)
272
+ load_advanced_btn.click(fn=load_advanced, outputs=system_prompt)
273
+
274
+ btn_customer.click(fn=lambda: load_email("Customer Complaint"), outputs=email)
275
+ btn_sales.click(fn=lambda: load_email("Sales Inquiry"), outputs=email)
276
+ btn_finance.click(fn=lambda: load_email("Internal Finance"), outputs=email)
277
+ btn_hr.click(fn=lambda: load_email("HR Issue"), outputs=email)
278
+ btn_urgent.click(fn=lambda: load_email("Urgent Escalation"), outputs=email)
279
 
280
+ run_btn.click(
281
+ fn=run_agent,
282
+ inputs=[system_prompt, email, show_raw_output],
283
+ outputs=[formatted_output, raw_output],
284
+ )
285
 
286
+ demo.launch(server_name="0.0.0.0", server_port=7860)