ankanghosh commited on
Commit
112dc64
·
verified ·
1 Parent(s): 2c85e1a

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +15 -9
agent.py CHANGED
@@ -11,6 +11,7 @@ import time
11
  import logging
12
  import traceback
13
  import json
 
14
  from langchain_core.tools import tool
15
  from langchain.prompts import PromptTemplate
16
  from langgraph.prebuilt import create_react_agent
@@ -94,6 +95,9 @@ def truth_classifier(query, evidence):
94
  "confidence": confidence,
95
  "results": classification_results
96
  }
 
 
 
97
 
98
  # Convert to JSON string for consistent handling
99
  return json.dumps(result)
@@ -266,6 +270,14 @@ def process_claim(claim, agent=None, recursion_limit=20):
266
  # Log performance
267
  elapsed = time.time() - start_time
268
  logger.info(f"Claim processed in {elapsed:.2f} seconds")
 
 
 
 
 
 
 
 
269
 
270
  return result
271
 
@@ -349,7 +361,6 @@ def format_response(response):
349
  result["evidence_count"] = len(message.content)
350
  elif isinstance(message.content, str) and message.content.startswith("[") and message.content.endswith("]"):
351
  try:
352
- import ast
353
  parsed_content = ast.literal_eval(message.content)
354
  if isinstance(parsed_content, list):
355
  result["evidence"] = parsed_content
@@ -372,10 +383,9 @@ def format_response(response):
372
  logger.info(f"Truth classifier content type: {type(message.content)}")
373
  logger.info(f"Truth classifier content: {message.content}")
374
 
375
- # Handle JSON formatted result from truth_classifier
376
  if isinstance(message.content, str):
377
  try:
378
- import json
379
  # Parse the JSON string
380
  parsed_content = json.loads(message.content)
381
 
@@ -416,9 +426,7 @@ def format_response(response):
416
  if found_tools["evidence_retriever"] and not found_tools["truth_classifier"]:
417
  logger.info("Truth classifier was not called by the agent, executing fallback classification")
418
 
419
- try:
420
- from modules.classification import classify_with_llm, aggregate_evidence
421
-
422
  # Get the evidence from the results
423
  evidence = result["evidence"]
424
  claim = result["claim"] or "Unknown claim"
@@ -451,9 +459,7 @@ def format_response(response):
451
  if (found_tools["truth_classifier"] or result["classification"] != "Uncertain") and not found_tools["explanation_generator"]:
452
  logger.info("Explanation generator was not called by the agent, using fallback explanation generation")
453
 
454
- try:
455
- from modules.explanation import generate_explanation
456
-
457
  # Get the necessary inputs for explanation generation
458
  claim = result["claim"] or "Unknown claim"
459
  evidence = result["evidence"]
 
11
  import logging
12
  import traceback
13
  import json
14
+ import ast
15
  from langchain_core.tools import tool
16
  from langchain.prompts import PromptTemplate
17
  from langgraph.prebuilt import create_react_agent
 
95
  "confidence": confidence,
96
  "results": classification_results
97
  }
98
+
99
+ # Log confidence score
100
+ performance_tracker.log_confidence_score(confidence)
101
 
102
  # Convert to JSON string for consistent handling
103
  return json.dumps(result)
 
270
  # Log performance
271
  elapsed = time.time() - start_time
272
  logger.info(f"Claim processed in {elapsed:.2f} seconds")
273
+
274
+ # Track processing time and overall success
275
+ performance_tracker.log_processing_time(start_time)
276
+ performance_tracker.log_claim_processed()
277
+
278
+ # Track confidence if available
279
+ if result and "confidence" in result:
280
+ performance_tracker.log_confidence_score(result["confidence"])
281
 
282
  return result
283
 
 
361
  result["evidence_count"] = len(message.content)
362
  elif isinstance(message.content, str) and message.content.startswith("[") and message.content.endswith("]"):
363
  try:
 
364
  parsed_content = ast.literal_eval(message.content)
365
  if isinstance(parsed_content, list):
366
  result["evidence"] = parsed_content
 
383
  logger.info(f"Truth classifier content type: {type(message.content)}")
384
  logger.info(f"Truth classifier content: {message.content}")
385
 
386
+ # Handle JSON formatted result from truth_classifier()
387
  if isinstance(message.content, str):
388
  try:
 
389
  # Parse the JSON string
390
  parsed_content = json.loads(message.content)
391
 
 
426
  if found_tools["evidence_retriever"] and not found_tools["truth_classifier"]:
427
  logger.info("Truth classifier was not called by the agent, executing fallback classification")
428
 
429
+ try:
 
 
430
  # Get the evidence from the results
431
  evidence = result["evidence"]
432
  claim = result["claim"] or "Unknown claim"
 
459
  if (found_tools["truth_classifier"] or result["classification"] != "Uncertain") and not found_tools["explanation_generator"]:
460
  logger.info("Explanation generator was not called by the agent, using fallback explanation generation")
461
 
462
+ try:
 
 
463
  # Get the necessary inputs for explanation generation
464
  claim = result["claim"] or "Unknown claim"
465
  evidence = result["evidence"]