LiamKhoaLe commited on
Commit
48a0d5a
·
1 Parent(s): 6f12b05

Upd report CoT loop and fix bug

Browse files
Files changed (2) hide show
  1. routes/reports.py +75 -12
  2. utils/api/router.py +14 -6
routes/reports.py CHANGED
@@ -122,12 +122,12 @@ async def generate_report(
122
  logger.info("[REPORT] Starting CoT planning phase")
123
  update_report_status(session_id, "planning", "Planning action...", 25)
124
  # Use enhanced instructions for better CoT planning
125
- cot_plan = await generate_cot_plan(enhanced_instructions, file_summary, context_text, web_context_block, nvidia_rotator)
126
 
127
  # Step 2: Execute detailed subtasks based on CoT plan
128
  logger.info("[REPORT] Executing detailed subtasks")
129
  update_report_status(session_id, "processing", "Processing data...", 40)
130
- detailed_analysis = await execute_detailed_subtasks(cot_plan, context_text, web_context_block, eff_name, nvidia_rotator)
131
 
132
  # Step 3: Synthesize comprehensive report from detailed analysis
133
  logger.info("[REPORT] Synthesizing comprehensive report")
@@ -184,7 +184,7 @@ async def generate_report_pdf(
184
 
185
  # ────────────────────────────── Chain of Thought Report Generation ──────────────────
186
 
187
- async def generate_cot_plan(instructions: str, file_summary: str, context_text: str, web_context: str, nvidia_rotator) -> Dict[str, Any]:
188
  """Generate a detailed Chain of Thought plan for report generation using NVIDIA."""
189
  sys_prompt = """You are an expert research analyst and report planner. Given a user's request and available materials, create a comprehensive plan for generating a detailed, professional report.
190
 
@@ -262,23 +262,86 @@ Create a detailed plan for generating a comprehensive report that addresses the
262
 
263
  try:
264
  selection = {"provider": "nvidia", "model": "meta/llama-3.1-8b-instruct"}
265
- response = await generate_answer_with_model(selection, sys_prompt, user_prompt, None, nvidia_rotator)
266
 
267
  # Parse JSON response
268
  import json
269
  json_text = response.strip()
 
 
 
270
  if json_text.startswith('```json'):
271
  json_text = json_text[7:-3].strip()
272
  elif json_text.startswith('```'):
273
  json_text = json_text[3:-3].strip()
274
 
 
 
 
275
  plan = json.loads(json_text)
276
  logger.info(f"[REPORT] CoT plan generated with {len(plan.get('report_structure', {}).get('sections', []))} sections")
277
  return plan
278
 
279
  except Exception as e:
280
  logger.warning(f"[REPORT] CoT planning failed: {e}")
281
- # Fallback plan
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  return {
283
  "analysis": {
284
  "user_intent": instructions,
@@ -309,7 +372,7 @@ Create a detailed plan for generating a comprehensive report that addresses the
309
  }
310
 
311
 
312
- async def execute_detailed_subtasks(cot_plan: Dict[str, Any], context_text: str, web_context: str, filename: str, nvidia_rotator) -> Dict[str, Any]:
313
  """Execute detailed analysis for each subtask identified in the CoT plan."""
314
  detailed_analysis = {}
315
  synthesis_strategy = cot_plan.get("synthesis_strategy", {})
@@ -338,7 +401,7 @@ async def execute_detailed_subtasks(cot_plan: Dict[str, Any], context_text: str,
338
  # Generate comprehensive analysis for this subtask
339
  subtask_result = await analyze_subtask_comprehensive(
340
  task, reasoning, sources_needed, depth, sub_actions, expected_output,
341
- quality_checks, context_text, web_context, filename, nvidia_rotator
342
  )
343
 
344
  section_analysis["subtask_results"].append({
@@ -353,7 +416,7 @@ async def execute_detailed_subtasks(cot_plan: Dict[str, Any], context_text: str,
353
 
354
  # Generate section-level synthesis
355
  section_synthesis = await synthesize_section_analysis(
356
- section_analysis, synthesis_strategy, nvidia_rotator
357
  )
358
  section_analysis["section_synthesis"] = section_synthesis
359
 
@@ -365,7 +428,7 @@ async def execute_detailed_subtasks(cot_plan: Dict[str, Any], context_text: str,
365
 
366
  async def analyze_subtask_comprehensive(task: str, reasoning: str, sources_needed: List[str], depth: str,
367
  sub_actions: List[str], expected_output: str, quality_checks: List[str],
368
- context_text: str, web_context: str, filename: str, nvidia_rotator) -> str:
369
  """Analyze a specific subtask with comprehensive detail and sub-actions."""
370
 
371
  # Select appropriate context based on sources_needed
@@ -423,7 +486,7 @@ Perform the comprehensive analysis as specified, following all sub-actions and m
423
 
424
  try:
425
  selection = {"provider": "nvidia", "model": "meta/llama-3.1-8b-instruct"}
426
- analysis = await generate_answer_with_model(selection, sys_prompt, user_prompt, None, nvidia_rotator)
427
  return analysis.strip()
428
 
429
  except Exception as e:
@@ -431,7 +494,7 @@ Perform the comprehensive analysis as specified, following all sub-actions and m
431
  return f"Analysis for '{task}' could not be completed due to processing error."
432
 
433
 
434
- async def synthesize_section_analysis(section_analysis: Dict[str, Any], synthesis_strategy: Dict[str, str], nvidia_rotator) -> str:
435
  """Synthesize all subtask results within a section into a coherent analysis."""
436
 
437
  section_title = section_analysis.get("title", "Unknown Section")
@@ -474,7 +537,7 @@ Synthesize these analyses into a comprehensive, coherent section that fulfills t
474
 
475
  try:
476
  selection = {"provider": "nvidia", "model": "meta/llama-3.1-8b-instruct"}
477
- synthesis = await generate_answer_with_model(selection, sys_prompt, user_prompt, None, nvidia_rotator)
478
  return synthesis.strip()
479
 
480
  except Exception as e:
 
122
  logger.info("[REPORT] Starting CoT planning phase")
123
  update_report_status(session_id, "planning", "Planning action...", 25)
124
  # Use enhanced instructions for better CoT planning
125
+ cot_plan = await generate_cot_plan(enhanced_instructions, file_summary, context_text, web_context_block, nvidia_rotator, gemini_rotator)
126
 
127
  # Step 2: Execute detailed subtasks based on CoT plan
128
  logger.info("[REPORT] Executing detailed subtasks")
129
  update_report_status(session_id, "processing", "Processing data...", 40)
130
+ detailed_analysis = await execute_detailed_subtasks(cot_plan, context_text, web_context_block, eff_name, nvidia_rotator, gemini_rotator)
131
 
132
  # Step 3: Synthesize comprehensive report from detailed analysis
133
  logger.info("[REPORT] Synthesizing comprehensive report")
 
184
 
185
  # ────────────────────────────── Chain of Thought Report Generation ──────────────────
186
 
187
+ async def generate_cot_plan(instructions: str, file_summary: str, context_text: str, web_context: str, nvidia_rotator, gemini_rotator) -> Dict[str, Any]:
188
  """Generate a detailed Chain of Thought plan for report generation using NVIDIA."""
189
  sys_prompt = """You are an expert research analyst and report planner. Given a user's request and available materials, create a comprehensive plan for generating a detailed, professional report.
190
 
 
262
 
263
  try:
264
  selection = {"provider": "nvidia", "model": "meta/llama-3.1-8b-instruct"}
265
+ response = await generate_answer_with_model(selection, sys_prompt, user_prompt, gemini_rotator, nvidia_rotator)
266
 
267
  # Parse JSON response
268
  import json
269
  json_text = response.strip()
270
+ logger.info(f"[REPORT] Raw CoT response length: {len(json_text)}")
271
+ logger.info(f"[REPORT] Raw CoT response preview: {json_text[:200]}...")
272
+
273
  if json_text.startswith('```json'):
274
  json_text = json_text[7:-3].strip()
275
  elif json_text.startswith('```'):
276
  json_text = json_text[3:-3].strip()
277
 
278
+ if not json_text:
279
+ raise ValueError("Empty response from model")
280
+
281
  plan = json.loads(json_text)
282
  logger.info(f"[REPORT] CoT plan generated with {len(plan.get('report_structure', {}).get('sections', []))} sections")
283
  return plan
284
 
285
  except Exception as e:
286
  logger.warning(f"[REPORT] CoT planning failed: {e}")
287
+ # Try a simpler fallback approach
288
+ try:
289
+ logger.info("[REPORT] Attempting simplified CoT planning")
290
+ simple_sys_prompt = """You are a report planner. Create a simple plan for a report based on the user's request.
291
+
292
+ Return a JSON object with this structure:
293
+ {
294
+ "analysis": {
295
+ "user_intent": "What the user wants to know",
296
+ "key_requirements": ["requirement1", "requirement2"],
297
+ "complexity_level": "intermediate",
298
+ "focus_areas": ["area1", "area2"]
299
+ },
300
+ "report_structure": {
301
+ "sections": [
302
+ {
303
+ "title": "Introduction",
304
+ "purpose": "Provide overview",
305
+ "subtasks": [{"task": "Summarize key points", "reasoning": "Set foundation", "sources_needed": ["local"], "depth": "detailed"}]
306
+ },
307
+ {
308
+ "title": "Main Analysis",
309
+ "purpose": "Address user's request",
310
+ "subtasks": [{"task": "Detailed analysis", "reasoning": "Core content", "sources_needed": ["local"], "depth": "comprehensive"}]
311
+ },
312
+ {
313
+ "title": "Conclusion",
314
+ "purpose": "Synthesize findings",
315
+ "subtasks": [{"task": "Summarize insights", "reasoning": "Provide closure", "sources_needed": ["local"], "depth": "detailed"}]
316
+ }
317
+ ]
318
+ },
319
+ "reasoning_flow": ["Analyze materials", "Extract insights", "Synthesize findings"]
320
+ }"""
321
+
322
+ simple_user_prompt = f"""USER REQUEST: {instructions}
323
+
324
+ FILE SUMMARY: {file_summary[:500]}
325
+
326
+ Create a simple plan for this report."""
327
+
328
+ simple_response = await generate_answer_with_model(selection, simple_sys_prompt, simple_user_prompt, gemini_rotator, nvidia_rotator)
329
+ simple_json_text = simple_response.strip()
330
+
331
+ if simple_json_text.startswith('```json'):
332
+ simple_json_text = simple_json_text[7:-3].strip()
333
+ elif simple_json_text.startswith('```'):
334
+ simple_json_text = simple_json_text[3:-3].strip()
335
+
336
+ if simple_json_text:
337
+ simple_plan = json.loads(simple_json_text)
338
+ logger.info("[REPORT] Simplified CoT plan generated successfully")
339
+ return simple_plan
340
+ except Exception as simple_e:
341
+ logger.warning(f"[REPORT] Simplified CoT planning also failed: {simple_e}")
342
+
343
+ # Final fallback plan
344
+ logger.info("[REPORT] Using hardcoded fallback plan")
345
  return {
346
  "analysis": {
347
  "user_intent": instructions,
 
372
  }
373
 
374
 
375
+ async def execute_detailed_subtasks(cot_plan: Dict[str, Any], context_text: str, web_context: str, filename: str, nvidia_rotator, gemini_rotator) -> Dict[str, Any]:
376
  """Execute detailed analysis for each subtask identified in the CoT plan."""
377
  detailed_analysis = {}
378
  synthesis_strategy = cot_plan.get("synthesis_strategy", {})
 
401
  # Generate comprehensive analysis for this subtask
402
  subtask_result = await analyze_subtask_comprehensive(
403
  task, reasoning, sources_needed, depth, sub_actions, expected_output,
404
+ quality_checks, context_text, web_context, filename, nvidia_rotator, gemini_rotator
405
  )
406
 
407
  section_analysis["subtask_results"].append({
 
416
 
417
  # Generate section-level synthesis
418
  section_synthesis = await synthesize_section_analysis(
419
+ section_analysis, synthesis_strategy, nvidia_rotator, gemini_rotator
420
  )
421
  section_analysis["section_synthesis"] = section_synthesis
422
 
 
428
 
429
  async def analyze_subtask_comprehensive(task: str, reasoning: str, sources_needed: List[str], depth: str,
430
  sub_actions: List[str], expected_output: str, quality_checks: List[str],
431
+ context_text: str, web_context: str, filename: str, nvidia_rotator, gemini_rotator) -> str:
432
  """Analyze a specific subtask with comprehensive detail and sub-actions."""
433
 
434
  # Select appropriate context based on sources_needed
 
486
 
487
  try:
488
  selection = {"provider": "nvidia", "model": "meta/llama-3.1-8b-instruct"}
489
+ analysis = await generate_answer_with_model(selection, sys_prompt, user_prompt, gemini_rotator, nvidia_rotator)
490
  return analysis.strip()
491
 
492
  except Exception as e:
 
494
  return f"Analysis for '{task}' could not be completed due to processing error."
495
 
496
 
497
+ async def synthesize_section_analysis(section_analysis: Dict[str, Any], synthesis_strategy: Dict[str, str], nvidia_rotator, gemini_rotator) -> str:
498
  """Synthesize all subtask results within a section into a coherent analysis."""
499
 
500
  section_title = section_analysis.get("title", "Unknown Section")
 
537
 
538
  try:
539
  selection = {"provider": "nvidia", "model": "meta/llama-3.1-8b-instruct"}
540
+ synthesis = await generate_answer_with_model(selection, sys_prompt, user_prompt, gemini_rotator, nvidia_rotator)
541
  return synthesis.strip()
542
 
543
  except Exception as e:
utils/api/router.py CHANGED
@@ -54,9 +54,13 @@ async def generate_answer_with_model(selection: Dict[str, Any], system_prompt: s
54
  headers = {"Content-Type": "application/json"}
55
  data = await robust_post_json(url, headers, payload, gemini_rotator)
56
  try:
57
- return data["candidates"][0]["content"]["parts"][0]["text"]
58
- except Exception:
59
- logger.warning(f"Unexpected Gemini response: {data}")
 
 
 
 
60
  return "I couldn't parse the model response."
61
 
62
  elif provider == "nvidia":
@@ -74,9 +78,13 @@ async def generate_answer_with_model(selection: Dict[str, Any], system_prompt: s
74
  headers = {"Content-Type": "application/json", "Authorization": f"Bearer {key}"}
75
  data = await robust_post_json(url, headers, payload, nvidia_rotator)
76
  try:
77
- return data["choices"][0]["message"]["content"]
78
- except Exception:
79
- logger.warning(f"Unexpected NVIDIA response: {data}")
 
 
 
 
80
  return "I couldn't parse the model response."
81
 
82
  return "Unsupported provider."
 
54
  headers = {"Content-Type": "application/json"}
55
  data = await robust_post_json(url, headers, payload, gemini_rotator)
56
  try:
57
+ content = data["candidates"][0]["content"]["parts"][0]["text"]
58
+ if not content or content.strip() == "":
59
+ logger.warning(f"Empty content from Gemini model: {data}")
60
+ return "I received an empty response from the model."
61
+ return content
62
+ except Exception as e:
63
+ logger.warning(f"Unexpected Gemini response: {data}, error: {e}")
64
  return "I couldn't parse the model response."
65
 
66
  elif provider == "nvidia":
 
78
  headers = {"Content-Type": "application/json", "Authorization": f"Bearer {key}"}
79
  data = await robust_post_json(url, headers, payload, nvidia_rotator)
80
  try:
81
+ content = data["choices"][0]["message"]["content"]
82
+ if not content or content.strip() == "":
83
+ logger.warning(f"Empty content from NVIDIA model: {data}")
84
+ return "I received an empty response from the model."
85
+ return content
86
+ except Exception as e:
87
+ logger.warning(f"Unexpected NVIDIA response: {data}, error: {e}")
88
  return "I couldn't parse the model response."
89
 
90
  return "Unsupported provider."