mgbam commited on
Commit
2796c54
·
verified ·
1 Parent(s): 08ad5e6

Update agents/agents.py

Browse files
Files changed (1) hide show
  1. agents/agents.py +157 -139
agents/agents.py CHANGED
@@ -14,6 +14,7 @@ ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
14
  class TopicAgent:
15
  def generate_outline(self, topic, duration, difficulty):
16
  if not openai_client:
 
17
  return self._mock_outline(topic, duration, difficulty)
18
 
19
  try:
@@ -45,6 +46,7 @@ class TopicAgent:
45
  )
46
  return json.loads(response.choices[0].message.content)
47
  except Exception as e:
 
48
  return self._mock_outline(topic, duration, difficulty)
49
 
50
  def _mock_outline(self, topic, duration, difficulty):
@@ -93,6 +95,7 @@ class TopicAgent:
93
  class ContentAgent:
94
  def generate_content(self, outline):
95
  if not openai_client:
 
96
  return self._mock_content(outline)
97
 
98
  try:
@@ -122,11 +125,12 @@ class ContentAgent:
122
  }
123
  ],
124
  temperature=0.4,
125
- max_tokens=3000,
126
  response_format={"type": "json_object"}
127
  )
128
  return json.loads(response.choices[0].message.content)
129
  except Exception as e:
 
130
  return self._mock_content(outline)
131
 
132
  def _mock_content(self, outline):
@@ -173,6 +177,7 @@ class ContentAgent:
173
  class SlideAgent:
174
  def generate_slides(self, content):
175
  if not openai_client:
 
176
  return self._professional_slides(content)
177
 
178
  try:
@@ -202,6 +207,7 @@ class SlideAgent:
202
  )
203
  return response.choices[0].message.content
204
  except Exception as e:
 
205
  return self._professional_slides(content)
206
 
207
  def _professional_slides(self, content):
@@ -245,157 +251,169 @@ graph TD
245
  A[Current Costs] --> B[Potential Savings]
246
  C[Implementation Costs] --> D[Net ROI]
247
  B --> D
 
 
248
  Document current process costs
249
-
250
  Estimate efficiency gains
251
-
252
  Calculate net ROI
253
-
254
- Q&A
255
- Let's discuss your specific challenges
256
  """
257
-
258
  class CodeAgent:
259
- def generate_code(self, content):
260
- if not openai_client:
261
- return self._professional_code(content)
262
-
263
- try:
264
- response = openai_client.chat.completions.create(
265
- model="gpt-4-turbo",
266
- messages=[
267
- {
268
- "role": "system",
269
- "content": (
270
- "You are an enterprise solutions architect. Create professional-grade code labs with: "
271
- "1) Production-ready patterns 2) Comprehensive documentation "
272
- "3) Enterprise security practices 4) Scalable architectures. "
273
- "Use Python with the latest best practices."
274
- )
275
- },
276
- {
277
- "role": "user",
278
- "content": (
279
- f"Create a professional code lab for: {json.dumps(content)}. "
280
- "Include: Setup instructions, business solution patterns, "
281
- "enterprise integration examples, and security best practices."
282
- )
283
- }
284
- ],
285
- temperature=0.3,
286
- max_tokens=2500
287
- )
288
- return response.choices[0].message.content
289
- except Exception as e:
290
- return self._professional_code(content)
 
 
291
 
292
- def _professional_code(self, content):
293
- return f"""# Enterprise-Grade Prompt Engineering Lab
294
- Business Solution Framework
295
- python
 
296
  class PromptOptimizer:
297
- def __init__(self, model="gpt-4-turbo"):
298
- self.model = model
299
- self.pattern_library = {{
300
- "financial_analysis": "Extract key metrics from financial reports",
301
- "customer_service": "Resolve tier-2 support tickets"
302
- }}
303
-
304
- def optimize_prompt(self, business_case):
305
- # Implement enterprise optimization logic
306
- return f"Business-optimized prompt for {{business_case}}"
307
-
308
- def calculate_roi(self, current_cost, expected_efficiency):
309
- return current_cost * expected_efficiency
310
-
311
- # Example usage
312
- optimizer = PromptOptimizer()
313
- print(optimizer.calculate_roi(500000, 0.35)) # $175,000 savings
314
 
315
- Security Best Practices
316
- python
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  def secure_prompt_handling(user_input):
318
- # Implement OWASP security standards
319
- sanitized = sanitize_input(user_input)
320
- validate_business_context(sanitized)
321
- return apply_enterprise_guardrails(sanitized)
322
-
323
- Integration Pattern: CRM System
324
- python
 
 
325
  def integrate_with_salesforce(prompt, salesforce_data):
326
- # Enterprise integration example
327
- enriched_prompt = f"{{prompt}} using {{salesforce_data}}"
328
- return call_ai_api(enriched_prompt)
329
  """
330
-
331
  class DesignAgent:
332
- def generate_design(self, slide_content):
333
- if not openai_client:
334
- return None
335
-
336
- try:
337
- response = openai_client.images.generate(
338
- model="dall-e-3",
339
- prompt=(
340
- f"Professional corporate slide background for '{slide_content[:200]}' workshop. "
341
- "Modern business style, clean lines, premium gradient, boardroom appropriate. "
342
- "Include abstract technology elements in corporate colors."
343
- ),
344
- n=1,
345
- size="1024x1024"
346
- )
347
- return response.data[0].url
348
- except Exception as e:
349
- return None
350
-
 
 
351
  class VoiceoverAgent:
352
- def __init__(self):
353
- self.api_key = ELEVENLABS_API_KEY
354
- self.voice_id = "21m00Tcm4TlvDq8ikWAM" # Default voice ID
355
- self.model = "eleven_monolingual_v1"
 
 
 
 
 
356
 
357
- def generate_voiceover(self, text, voice_id=None):
358
- if not self.api_key:
359
- return None
360
-
361
- try:
362
- voice = voice_id if voice_id else self.voice_id
363
-
364
- url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice}"
365
- headers = {
366
- "Accept": "audio/mpeg",
367
- "Content-Type": "application/json",
368
- "xi-api-key": self.api_key
369
- }
370
- data = {
371
- "text": text,
372
- "model_id": self.model,
373
- "voice_settings": {
374
- "stability": 0.7,
375
- "similarity_boost": 0.8,
376
- "style": 0.5,
377
- "use_speaker_boost": True
378
- }
379
  }
380
- response = requests.post(url, json=data, headers=headers)
381
-
382
- if response.status_code == 200:
383
- return response.content
384
- return None
385
- except Exception as e:
386
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
387
 
388
- def get_voices(self):
389
- if not self.api_key:
390
- return []
391
-
392
- try:
393
- url = "https://api.elevenlabs.io/v1/voices"
394
- headers = {"xi-api-key": self.api_key}
395
- response = requests.get(url, headers=headers)
396
-
397
- if response.status_code == 200:
398
- return response.json().get("voices", [])
399
- return []
400
- except Exception as e:
401
- return []
 
14
  class TopicAgent:
15
  def generate_outline(self, topic, duration, difficulty):
16
  if not openai_client:
17
+ print("OpenAI API not set - using enhanced mock data for outline.")
18
  return self._mock_outline(topic, duration, difficulty)
19
 
20
  try:
 
46
  )
47
  return json.loads(response.choices[0].message.content)
48
  except Exception as e:
49
+ print(f"Error generating outline with OpenAI: {e}. Using mock data.")
50
  return self._mock_outline(topic, duration, difficulty)
51
 
52
  def _mock_outline(self, topic, duration, difficulty):
 
95
  class ContentAgent:
96
  def generate_content(self, outline):
97
  if not openai_client:
98
+ print("OpenAI API not set - using enhanced mock data for content.")
99
  return self._mock_content(outline)
100
 
101
  try:
 
125
  }
126
  ],
127
  temperature=0.4,
128
+ max_tokens=4000,
129
  response_format={"type": "json_object"}
130
  )
131
  return json.loads(response.choices[0].message.content)
132
  except Exception as e:
133
+ print(f"Error generating content with OpenAI: {e}. Using mock data.")
134
  return self._mock_content(outline)
135
 
136
  def _mock_content(self, outline):
 
177
  class SlideAgent:
178
  def generate_slides(self, content):
179
  if not openai_client:
180
+ print("OpenAI API not set - using enhanced mock data for slides.")
181
  return self._professional_slides(content)
182
 
183
  try:
 
207
  )
208
  return response.choices[0].message.content
209
  except Exception as e:
210
+ print(f"Error generating slides with OpenAI: {e}. Using mock data.")
211
  return self._professional_slides(content)
212
 
213
  def _professional_slides(self, content):
 
251
  A[Current Costs] --> B[Potential Savings]
252
  C[Implementation Costs] --> D[Net ROI]
253
  B --> D
254
+ Use code with caution.
255
+ Python
256
  Document current process costs
 
257
  Estimate efficiency gains
 
258
  Calculate net ROI
259
+ Q&A: Let's discuss your specific challenges
 
 
260
  """
 
261
  class CodeAgent:
262
+ def generate_code(self, content):
263
+ if not openai_client:
264
+ print("OpenAI API not set - using enhanced mock data for code.")
265
+ return self._professional_code(content)
266
+ Generated code
267
+ try:
268
+ response = openai_client.chat.completions.create(
269
+ model="gpt-4-turbo",
270
+ messages=[
271
+ {
272
+ "role": "system",
273
+ "content": (
274
+ "You are an enterprise solutions architect. Create professional-grade code labs with: "
275
+ "1) Production-ready patterns 2) Comprehensive documentation "
276
+ "3) Enterprise security practices 4) Scalable architectures. "
277
+ "Use Python with the latest best practices."
278
+ )
279
+ },
280
+ {
281
+ "role": "user",
282
+ "content": (
283
+ f"Create a professional code lab for: {json.dumps(content)}. "
284
+ "Include: Setup instructions, business solution patterns, "
285
+ "enterprise integration examples, and security best practices."
286
+ )
287
+ }
288
+ ],
289
+ temperature=0.3,
290
+ max_tokens=2500
291
+ )
292
+ return response.choices[0].message.content
293
+ except Exception as e:
294
+ print(f"Error generating code with OpenAI: {e}. Using mock data.")
295
+ return self._professional_code(content)
296
 
297
+ def _professional_code(self, content):
298
+ return """
299
+ Use code with caution.
300
+ --- Enterprise-Grade Prompt Engineering Lab ---
301
+ --- Business Solution Framework ---
302
  class PromptOptimizer:
303
+ """A class to optimize prompts and calculate ROI."""
304
+ def init(self, model="gpt-4-turbo"):
305
+ self.model = model
306
+ self.pattern_library = {
307
+ "financial_analysis": "Extract key metrics from financial reports",
308
+ "customer_service": "Resolve tier-2 support tickets"
309
+ }
310
+ Generated code
311
+ def optimize_prompt(self, business_case):
312
+ \"\"\"Implement enterprise optimization logic.\"\"\"
313
+ return f"Business-optimized prompt for {business_case}"
 
 
 
 
 
 
314
 
315
+ def calculate_roi(self, current_cost, expected_efficiency):
316
+ \"\"\"Calculate ROI based on cost and efficiency gain.\"\"\"
317
+ return current_cost * expected_efficiency
318
+ Use code with caution.
319
+ --- Security Best Practices ---
320
+ def _sanitize_input(user_input):
321
+ """Placeholder for input sanitization logic."""
322
+ # In a real implementation, use a library like bleach to prevent XSS
323
+ return user_input
324
+ def _validate_business_context(sanitized_input):
325
+ """Placeholder for validating input against business rules."""
326
+ return True
327
+ def _apply_enterprise_guardrails(sanitized_input):
328
+ """Placeholder for applying enterprise-level safety guardrails."""
329
+ return sanitized_input
330
  def secure_prompt_handling(user_input):
331
+ """Implement OWASP security standards for prompt handling."""
332
+ sanitized = _sanitize_input(user_input)
333
+ if _validate_business_context(sanitized):
334
+ return _apply_enterprise_guardrails(sanitized)
335
+ raise ValueError("Input failed business context validation.")
336
+ --- Integration Pattern: CRM System ---
337
+ def _call_ai_api(enriched_prompt):
338
+ """Placeholder for the actual AI API call."""
339
+ return f"AI Response for: {enriched_prompt}"
340
  def integrate_with_salesforce(prompt, salesforce_data):
341
+ """Enterprise integration example with a CRM."""
342
+ enriched_prompt = f"{prompt} using data: {salesforce_data}"
343
+ return _call_ai_api(enriched_prompt)
344
  """
 
345
  class DesignAgent:
346
+ def generate_design(self, slide_content):
347
+ if not openai_client:
348
+ print("OpenAI API not set - skipping design generation.")
349
+ return None
350
+ Generated code
351
+ try:
352
+ response = openai_client.images.generate(
353
+ model="dall-e-3",
354
+ prompt=(
355
+ f"Professional corporate slide background for '{slide_content[:200]}' workshop. "
356
+ "Modern business style, clean lines, premium gradient, boardroom appropriate. "
357
+ "Include abstract technology elements in corporate colors."
358
+ ),
359
+ n=1,
360
+ size="1024x1024"
361
+ )
362
+ return response.data[0].url
363
+ except Exception as e:
364
+ print(f"Error generating design with OpenAI: {e}. Skipping design generation.")
365
+ return None
366
+ Use code with caution.
367
  class VoiceoverAgent:
368
+ def init(self):
369
+ self.api_key = ELEVENLABS_API_KEY
370
+ self.voice_id = "21m00Tcm4TlvDq8ikWAM" # Default voice ID for "Rachel"
371
+ self.model = "eleven_monolingual_v1"
372
+ Generated code
373
+ def generate_voiceover(self, text, voice_id=None):
374
+ if not self.api_key:
375
+ print("ElevenLabs API key not set - skipping voiceover generation.")
376
+ return None
377
 
378
+ try:
379
+ voice = voice_id if voice_id else self.voice_id
380
+
381
+ url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice}"
382
+ headers = {
383
+ "Accept": "audio/mpeg",
384
+ "Content-Type": "application/json",
385
+ "xi-api-key": self.api_key
386
+ }
387
+ data = {
388
+ "text": text,
389
+ "model_id": self.model,
390
+ "voice_settings": {
391
+ "stability": 0.7,
392
+ "similarity_boost": 0.8,
393
+ "style": 0.5,
394
+ "use_speaker_boost": True
 
 
 
 
 
395
  }
396
+ }
397
+ response = requests.post(url, json=data, headers=headers)
398
+ response.raise_for_status() # Raise an exception for bad status codes
399
+ return response.content
400
+
401
+ except requests.exceptions.RequestException as e:
402
+ print(f"Error generating voiceover with ElevenLabs: {e}")
403
+ return None
404
+
405
+ def get_voices(self):
406
+ if not self.api_key:
407
+ print("ElevenLabs API key not set - cannot fetch voices.")
408
+ return []
409
+
410
+ try:
411
+ url = "https://api.elevenlabs.io/v1/voices"
412
+ headers = {"xi-api-key": self.api_key}
413
+ response = requests.get(url, headers=headers)
414
+ response.raise_for_status()
415
+ return response.json().get("voices", [])
416
 
417
+ except requests.exceptions.RequestException as e:
418
+ print(f"Error fetching voices from ElevenLabs: {e}")
419
+ return []