Priyanshi Saxena commited on
Commit
923b4b3
Β·
1 Parent(s): a4b05e9

fix: Add comprehensive response cleaning validation

Browse files

- Added version tracking for deployment debugging
- Enhanced logging to track content extraction process
- Added critical safety check for LangChain metadata in final responses
- Improved debugging for Hugging Face Spaces deployment issues
- Added explicit validation to prevent raw metadata from being returned

Files changed (2) hide show
  1. src/agent/research_agent.py +16 -0
  2. version.py +2 -0
src/agent/research_agent.py CHANGED
@@ -16,6 +16,13 @@ from src.utils.ai_safety import ai_safety
16
 
17
  logger = get_logger(__name__)
18
 
 
 
 
 
 
 
 
19
  class Web3ResearchAgent:
20
  def __init__(self):
21
  self.llm = None
@@ -513,8 +520,10 @@ Respond with only the tool names, comma-separated (no explanations)."""
513
  # Extract content from Gemini response object
514
  if hasattr(final_response, 'content'):
515
  response_content = final_response.content
 
516
  else:
517
  response_content = str(final_response)
 
518
 
519
  # AI Safety Check: Validate response
520
  clean_response, response_safe, response_reason = ai_safety.validate_gemini_response(response_content)
@@ -526,6 +535,7 @@ Respond with only the tool names, comma-separated (no explanations)."""
526
  })
527
  clean_response = f"## Cryptocurrency Analysis\n\nBased on the available data:\n\n{context[:1000]}\n\n*Response filtered for safety*"
528
 
 
529
  final_response = clean_response
530
 
531
  except asyncio.TimeoutError:
@@ -534,6 +544,12 @@ Respond with only the tool names, comma-separated (no explanations)."""
534
 
535
  logger.info("βœ… Research successful with Gemini + tools")
536
 
 
 
 
 
 
 
537
  return {
538
  "success": True,
539
  "query": query,
 
16
 
17
  logger = get_logger(__name__)
18
 
19
+ # Add version logging for debugging
20
+ try:
21
+ from version import VERSION
22
+ logger.info(f"πŸ”§ Research Agent Version: {VERSION}")
23
+ except ImportError:
24
+ logger.info("πŸ”§ Research Agent Version: Unknown")
25
+
26
  class Web3ResearchAgent:
27
  def __init__(self):
28
  self.llm = None
 
520
  # Extract content from Gemini response object
521
  if hasattr(final_response, 'content'):
522
  response_content = final_response.content
523
+ logger.info(f"βœ… Extracted clean content: {response_content[:200]}...")
524
  else:
525
  response_content = str(final_response)
526
+ logger.warning(f"⚠️ Fallback to str() conversion: {response_content[:200]}...")
527
 
528
  # AI Safety Check: Validate response
529
  clean_response, response_safe, response_reason = ai_safety.validate_gemini_response(response_content)
 
535
  })
536
  clean_response = f"## Cryptocurrency Analysis\n\nBased on the available data:\n\n{context[:1000]}\n\n*Response filtered for safety*"
537
 
538
+ logger.info(f"πŸ”’ Final clean response: {clean_response[:200]}...")
539
  final_response = clean_response
540
 
541
  except asyncio.TimeoutError:
 
544
 
545
  logger.info("βœ… Research successful with Gemini + tools")
546
 
547
+ # Final safety check: ensure we're not returning raw LangChain objects
548
+ if isinstance(final_response, str):
549
+ if "additional_kwargs" in final_response or "response_metadata" in final_response:
550
+ logger.error("🚨 CRITICAL: Raw LangChain metadata detected in final response!")
551
+ final_response = "Response contains technical metadata and has been filtered for safety."
552
+
553
  return {
554
  "success": True,
555
  "query": query,
version.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # Version identifier for debugging
2
+ VERSION = "1.2.1-response-cleanup-fix"