Nikita Makarov commited on
Commit
84c7402
·
1 Parent(s): 96e89cb
Files changed (1) hide show
  1. src/radio_agent.py +90 -34
src/radio_agent.py CHANGED
@@ -219,9 +219,12 @@ class RadioAgent:
219
  model=self.config.nebius_model,
220
  messages=[{"role": "user", "content": prompt}],
221
  temperature=0.9,
222
- max_tokens=100 #200
223
  )
224
- text = response.choices[0].message.content.strip()
 
 
 
225
  llm_logger.info(
226
  "INTRO | model=%s | prompt=%r | response=%r",
227
  self.config.nebius_model,
@@ -249,9 +252,12 @@ class RadioAgent:
249
  model=self.config.nebius_model,
250
  messages=[{"role": "user", "content": prompt}],
251
  temperature=0.9,
252
- max_tokens=100 #200
253
  )
254
- text = response.choices[0].message.content.strip()
 
 
 
255
  llm_logger.info(
256
  "OUTRO | model=%s | prompt=%r | response=%r",
257
  self.config.nebius_model,
@@ -432,9 +438,12 @@ class RadioAgent:
432
  model=self.config.nebius_model,
433
  messages=[{"role": "user", "content": prompt}],
434
  temperature=0.9,
435
- max_tokens=100 #400
436
  )
437
- text = response.choices[0].message.content.strip()
 
 
 
438
 
439
  # Check if response references RAG context
440
  rag_used = False
@@ -451,7 +460,7 @@ class RadioAgent:
451
 
452
  if rag_used:
453
  print(f" ✅ [RAG] LLM response appears to reference RAG context!")
454
- rag_logger.info(f"✅ RAG CONTEXT USED in commentary for {track.get('title')}")
455
  else:
456
  print(f" ℹ️ [RAG] LLM response doesn't explicitly reference context (may still be influenced)")
457
 
@@ -474,30 +483,59 @@ class RadioAgent:
474
  if self.client:
475
  try:
476
  # Generate full news script (1-2 minutes of speech)
477
- news_text = "\n".join([f"- {item['title']}: {item['summary']}" for item in news_items[:3]])
478
- prompt = f"""You are a professional radio news presenter named Lera. Say that this is news time on the show.
479
- Present these news items in a conversational, engaging way. This should be about 1 minute of speech (about 200-250 words, max_tokens = 400):
 
 
480
 
481
- News text:
482
- {news_text}
 
483
 
484
- Rewrite news to be nice to listen.
 
485
 
486
- Requirements:
487
- - Provide context and background for each story
488
- - Explain why these stories matter
489
- - Use smooth transitions between stories
490
- - Be informative but friendly and conversational
491
- - Add brief analysis or implications when relevant
492
- - Sound natural, like a real radio news anchor"""
 
 
 
 
 
 
 
493
 
 
494
  response = self.client.chat.completions.create(
495
  model=self.config.nebius_model,
496
  messages=[{"role": "user", "content": prompt}],
497
  temperature=0.9,
498
- max_tokens=100 #400
499
  )
500
- full_script = response.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
  llm_logger.info(
502
  "NEWS | model=%s | prompt=%r | response=%r",
503
  self.config.nebius_model,
@@ -523,12 +561,21 @@ class RadioAgent:
523
 
524
  return batches if batches else [full_script]
525
  except Exception as e:
526
- print(f"Error generating news script: {e}")
527
-
528
- # Fallback
529
- script = "In the news today: "
530
- for item in news_items[:2]:
531
- script += f"{item['title']}. "
 
 
 
 
 
 
 
 
 
532
  return [script]
533
 
534
  def _generate_podcast_intro(self, podcast: Dict[str, Any], preferences: Dict[str, Any]) -> str:
@@ -549,9 +596,12 @@ class RadioAgent:
549
  model=self.config.nebius_model,
550
  messages=[{"role": "user", "content": prompt}],
551
  temperature=0.8,
552
- max_tokens=100 #200
553
  )
554
- text = response.choices[0].message.content.strip()
 
 
 
555
  llm_logger.info(
556
  "PODCAST | model=%s | prompt=%r | response=%r",
557
  self.config.nebius_model,
@@ -576,9 +626,12 @@ class RadioAgent:
576
  model=self.config.nebius_model,
577
  messages=[{"role": "user", "content": prompt}],
578
  temperature=0.9,
579
- max_tokens=100 #600
580
  )
581
- text = response.choices[0].message.content.strip()
 
 
 
582
  llm_logger.info(
583
  "STORY | model=%s | prompt=%r | response=%r",
584
  self.config.nebius_model,
@@ -610,9 +663,12 @@ Don't use emojis. Just speak naturally like a real DJ."""
610
  model=self.config.nebius_model,
611
  messages=[{"role": "user", "content": prompt}],
612
  temperature=0.9,
613
- max_tokens=100 #200 # Keep it short for fast response
614
  )
615
- text = response.choices[0].message.content.strip()
 
 
 
616
  llm_logger.info(
617
  "SONG_REQUEST | model=%s | prompt=%r | response=%r",
618
  self.config.nebius_model,
 
219
  model=self.config.nebius_model,
220
  messages=[{"role": "user", "content": prompt}],
221
  temperature=0.9,
222
+ max_tokens=200 #200
223
  )
224
+ content = response.choices[0].message.content
225
+ if not content:
226
+ return "Welcome to AI Radio! Let's get started with some great music."
227
+ text = content.strip()
228
  llm_logger.info(
229
  "INTRO | model=%s | prompt=%r | response=%r",
230
  self.config.nebius_model,
 
252
  model=self.config.nebius_model,
253
  messages=[{"role": "user", "content": prompt}],
254
  temperature=0.9,
255
+ max_tokens=200 #200
256
  )
257
+ content = response.choices[0].message.content
258
+ if not content:
259
+ return "Welcome to AI Radio! Let's get started with some great music."
260
+ text = content.strip()
261
  llm_logger.info(
262
  "OUTRO | model=%s | prompt=%r | response=%r",
263
  self.config.nebius_model,
 
438
  model=self.config.nebius_model,
439
  messages=[{"role": "user", "content": prompt}],
440
  temperature=0.9,
441
+ max_tokens=400 #400
442
  )
443
+ content = response.choices[0].message.content
444
+ if not content:
445
+ return f"Coming up: {track['title']} by {track['artist']}! This is a fantastic {track['genre']} track that's perfect for your {mood} mood. Let's enjoy this one!"
446
+ text = content.strip()
447
 
448
  # Check if response references RAG context
449
  rag_used = False
 
460
 
461
  if rag_used:
462
  print(f" ✅ [RAG] LLM response appears to reference RAG context!")
463
+ llm_logger.info(f"✅ RAG CONTEXT USED in commentary for {track.get('title')}")
464
  else:
465
  print(f" ℹ️ [RAG] LLM response doesn't explicitly reference context (may still be influenced)")
466
 
 
483
  if self.client:
484
  try:
485
  # Generate full news script (1-2 minutes of speech)
486
+ # Check if news items have required fields
487
+ valid_items = []
488
+ for item in news_items[:3]:
489
+ if item.get('title') and item.get('summary'):
490
+ valid_items.append(item)
491
 
492
+ if not valid_items:
493
+ print("⚠️ No valid news items with title and summary, using fallback")
494
+ raise ValueError("No valid news items")
495
 
496
+ news_text = "\n".join([f"- {item['title']}: {item['summary']}" for item in valid_items])
497
+ print(f"📰 Generating news script from {len(valid_items)} items")
498
 
499
+ prompt = f"""You are a professional radio news presenter named Lera. Say that this is news time on the show.
500
+ Present these news items in a conversational, engaging way. Text includes only what Lera needs to say. This should be about 1 minute of speech (about 150-200 words):
501
+
502
+ News items:
503
+ {news_text}
504
+
505
+ Requirements:
506
+ - Start by saying "It's news time on the show"
507
+ - Present each story in a conversational, engaging way
508
+ - Provide brief context for each story
509
+ - Use smooth transitions between stories
510
+ - Be informative but friendly and conversational
511
+ - Sound natural, like a real radio news anchor
512
+ - Keep it concise - about 150-200 words total"""
513
 
514
+ print(f"📝 News prompt length: {len(prompt)} chars")
515
  response = self.client.chat.completions.create(
516
  model=self.config.nebius_model,
517
  messages=[{"role": "user", "content": prompt}],
518
  temperature=0.9,
519
+ max_tokens=400 # Increased from 100 to allow proper response
520
  )
521
+
522
+ # Check response structure
523
+ if not response or not response.choices:
524
+ print("⚠️ LLM returned empty response object")
525
+ raise ValueError("Empty response object from LLM")
526
+
527
+ if not response.choices[0].message:
528
+ print("⚠️ LLM response has no message")
529
+ raise ValueError("No message in LLM response")
530
+
531
+ content = response.choices[0].message.content
532
+ if not content:
533
+ print("⚠️ LLM returned empty content for news script, using fallback")
534
+ print(f" Response object: {response}")
535
+ raise ValueError("Empty content from LLM")
536
+
537
+ full_script = content.strip()
538
+ print(f"✅ Generated news script: {len(full_script)} chars, {len(full_script.split())} words")
539
  llm_logger.info(
540
  "NEWS | model=%s | prompt=%r | response=%r",
541
  self.config.nebius_model,
 
561
 
562
  return batches if batches else [full_script]
563
  except Exception as e:
564
+ print(f"Error generating news script: {e}")
565
+ import traceback
566
+ traceback.print_exc()
567
+
568
+ # Fallback - create a simple script from available news items
569
+ print("📰 Using fallback news script")
570
+ script = "It's news time on the show. "
571
+ valid_items = [item for item in news_items[:2] if item.get('title')]
572
+ if valid_items:
573
+ for item in valid_items:
574
+ title = item.get('title', 'News story')
575
+ script += f"{title}. "
576
+ script += "That's the news for now. Back to music!"
577
+ else:
578
+ script = "That's all for news. Back to music!"
579
  return [script]
580
 
581
  def _generate_podcast_intro(self, podcast: Dict[str, Any], preferences: Dict[str, Any]) -> str:
 
596
  model=self.config.nebius_model,
597
  messages=[{"role": "user", "content": prompt}],
598
  temperature=0.8,
599
+ max_tokens=200 #200
600
  )
601
+ content = response.choices[0].message.content
602
+ if not content:
603
+ return f"Here's an interesting podcast: {podcast.get('title', 'Unknown')}. Let's listen!"
604
+ text = content.strip()
605
  llm_logger.info(
606
  "PODCAST | model=%s | prompt=%r | response=%r",
607
  self.config.nebius_model,
 
626
  model=self.config.nebius_model,
627
  messages=[{"role": "user", "content": prompt}],
628
  temperature=0.9,
629
+ max_tokens=600 #600
630
  )
631
+ content = response.choices[0].message.content
632
+ if not content:
633
+ return "Here's a fascinating story for you. Let me share it with you."
634
+ text = content.strip()
635
  llm_logger.info(
636
  "STORY | model=%s | prompt=%r | response=%r",
637
  self.config.nebius_model,
 
663
  model=self.config.nebius_model,
664
  messages=[{"role": "user", "content": prompt}],
665
  temperature=0.9,
666
+ max_tokens=200 #200 # Keep it short for fast response
667
  )
668
+ content = response.choices[0].message.content
669
+ if not content:
670
+ return f"Great choice! Here's '{title}' by {artist} coming right up!"
671
+ text = content.strip()
672
  llm_logger.info(
673
  "SONG_REQUEST | model=%s | prompt=%r | response=%r",
674
  self.config.nebius_model,