Aniket2006 commited on
Commit
a6e4432
·
1 Parent(s): 504a512

Fix: pass context to _generate_response for persona/zone/trend/weather

Browse files
Files changed (1) hide show
  1. reasoning_engine.py +51 -5
reasoning_engine.py CHANGED
@@ -101,8 +101,8 @@ class ReasoningEngine:
101
  # Stage 3: Multi-stage reasoning
102
  reasoning_result = self._reason(query, intent, staged_context)
103
 
104
- # Stage 4: Generate response
105
- response = self._generate_response(query, reasoning_result)
106
 
107
  # Stage 5: Generate followups
108
  followups = generate_followup_questions(
@@ -319,8 +319,9 @@ class ReasoningEngine:
319
  raw_response=response
320
  )
321
 
322
- def _generate_response(self, query: str, result: ReasoningResult) -> str:
323
- """Generate final user-facing response."""
 
324
  diagnosis_data = {
325
  "diagnosis": result.final_diagnosis,
326
  "confidence": result.final_confidence,
@@ -330,11 +331,56 @@ class ReasoningEngine:
330
  "recommendation": result.recommendation
331
  }
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  prompt = format_stage_prompt(
334
  RESPONSE_PROMPT,
335
  query=query,
336
  diagnosis=json.dumps(diagnosis_data, indent=2),
337
- evidence=json.dumps(result.evidence_summary, indent=2)
 
 
 
 
 
338
  )
339
 
340
  full_prompt = f"{SYSTEM_PROMPT}\n\n{prompt}"
 
101
  # Stage 3: Multi-stage reasoning
102
  reasoning_result = self._reason(query, intent, staged_context)
103
 
104
+ # Stage 4: Generate response (pass full context for persona/weather/zone)
105
+ response = self._generate_response(query, reasoning_result, context)
106
 
107
  # Stage 5: Generate followups
108
  followups = generate_followup_questions(
 
319
  raw_response=response
320
  )
321
 
322
+ def _generate_response(self, query: str, result: ReasoningResult,
323
+ context: Dict = None) -> str:
324
+ """Generate final user-facing response with persona and context."""
325
  diagnosis_data = {
326
  "diagnosis": result.final_diagnosis,
327
  "confidence": result.final_confidence,
 
331
  "recommendation": result.recommendation
332
  }
333
 
334
+ # Extract persona instructions
335
+ persona = context.get("persona", {}) if context else {}
336
+ persona_instructions = persona.get("instructions", "Provide clear, helpful farming advice.")
337
+
338
+ # Format conversation history
339
+ conv_history = context.get("conversation_history", []) if context else []
340
+ if conv_history:
341
+ history_text = "Recent conversation:\n"
342
+ for turn in conv_history[-3:]:
343
+ role = turn.get("role", "")
344
+ content = turn.get("content", "")[:100]
345
+ history_text += f" {role.capitalize()}: {content}\n"
346
+ else:
347
+ history_text = "This is the first message in the conversation."
348
+
349
+ # Format zone context
350
+ zone_data = context.get("zone_analysis", {}) if context else {}
351
+ if zone_data and zone_data.get("priority_zones"):
352
+ zones = zone_data["priority_zones"]
353
+ zone_text = "PRIORITY ZONES:\n"
354
+ for i, z in enumerate(zones[:3], 1):
355
+ zone_text += f" {i}. {z.get('location', 'Zone')} - Stress: {z.get('stress_score', 0):.0%}\n"
356
+ else:
357
+ zone_text = "No zone-specific data available."
358
+
359
+ # Format trend context
360
+ trend_data = context.get("historical_trends", {}) if context else {}
361
+ if trend_data.get("summary"):
362
+ trend_text = trend_data["summary"]
363
+ else:
364
+ trend_text = "No historical trend data available."
365
+
366
+ # Format weather context
367
+ weather = context.get("weather", {}) if context else {}
368
+ if weather:
369
+ from prompts import format_weather_context
370
+ weather_text = format_weather_context(weather)
371
+ else:
372
+ weather_text = "No weather data available."
373
+
374
  prompt = format_stage_prompt(
375
  RESPONSE_PROMPT,
376
  query=query,
377
  diagnosis=json.dumps(diagnosis_data, indent=2),
378
+ evidence=json.dumps(result.evidence_summary, indent=2),
379
+ persona_instructions=persona_instructions,
380
+ conversation_history=history_text,
381
+ zone_context=zone_text,
382
+ trend_context=trend_text,
383
+ weather_context=weather_text
384
  )
385
 
386
  full_prompt = f"{SYSTEM_PROMPT}\n\n{prompt}"