drrobot9 commited on
Commit
5c1f411
·
verified ·
1 Parent(s): 0702aa1

Update app/agents/crew_pipeline.py

Browse files
Files changed (1) hide show
  1. app/agents/crew_pipeline.py +30 -26
app/agents/crew_pipeline.py CHANGED
@@ -249,7 +249,7 @@ def run_pipeline(user_query: str, session_id: str = None):
249
  Each session_id keeps its own history.
250
  """
251
  if session_id is None:
252
- session_id = str(uuid.uuid4()) # fallback unique session
253
 
254
  # Language detection
255
  lang_label, prob = detect_language(user_query, top_k=1)[0]
@@ -264,42 +264,45 @@ def run_pipeline(user_query: str, session_id: str = None):
264
 
265
  intent, extra = detect_intent(translated_query)
266
 
267
- # Load conversation history
268
  history = memory_store.get_history(session_id) or []
269
  if len(history) > MAX_HISTORY_MESSAGES:
270
  history = history[-MAX_HISTORY_MESSAGES:]
271
 
272
 
273
- history.append({"role": "user", "content": translated_query})
274
-
275
-
276
  system_prompt = (
277
  "You are FarmLingua, an AI assistant for Nigerian farmers. "
278
- "Answer directly without repeating the question. "
279
- "Use clear farmer-friendly English with emojis . "
280
- "Avoid jargon and irrelevant details. "
281
  "If asked who built you, say: 'KawaFarm LTD developed me to help farmers.'"
282
-
283
  )
284
 
 
 
285
 
286
  if intent == "weather" and extra:
287
  weather_text = get_weather(extra)
288
- history.append({"role": "user", "content": f"Rewrite this weather update simply for farmers:\n{weather_text}"})
289
- messages_for_qwen = build_messages_from_history(history, system_prompt)
290
- english_answer = run_qwen(messages_for_qwen, max_new_tokens=256)
291
- else:
292
- if intent == "live_update":
293
- context = retrieve_docs(translated_query, config.LIVE_VS_PATH)
294
- if context:
295
- history.append({"role": "user", "content": f"Latest agricultural updates:\n{context}"})
296
- if intent == "low_confidence":
297
- context = retrieve_docs(translated_query, config.STATIC_VS_PATH)
298
- if context:
299
- history.append({"role": "user", "content": f"Reference information:\n{context}"})
300
-
301
- messages_for_qwen = build_messages_from_history(history, system_prompt)
302
- english_answer = run_qwen(messages_for_qwen, max_new_tokens=700)
 
 
 
 
 
303
 
304
  # Save assistant reply
305
  history.append({"role": "assistant", "content": english_answer})
@@ -307,15 +310,16 @@ def run_pipeline(user_query: str, session_id: str = None):
307
  history = history[-MAX_HISTORY_MESSAGES:]
308
  memory_store.save_history(session_id, history)
309
 
310
- # Translate back if needed
311
  final_answer = (
312
  translate_text(english_answer, src_lang="eng_Latn", tgt_lang=lang_label)
313
  if lang_label != "eng_Latn"
314
  else english_answer
315
  )
316
  final_answer = strip_markdown(final_answer)
 
317
  return {
318
  "session_id": session_id,
319
  "detected_language": SUPPORTED_LANGS.get(lang_label, "Unknown"),
320
  "answer": final_answer
321
- }
 
249
  Each session_id keeps its own history.
250
  """
251
  if session_id is None:
252
+ session_id = str(uuid.uuid4())
253
 
254
  # Language detection
255
  lang_label, prob = detect_language(user_query, top_k=1)[0]
 
264
 
265
  intent, extra = detect_intent(translated_query)
266
 
267
+ # Load conversation history
268
  history = memory_store.get_history(session_id) or []
269
  if len(history) > MAX_HISTORY_MESSAGES:
270
  history = history[-MAX_HISTORY_MESSAGES:]
271
 
272
 
 
 
 
273
  system_prompt = (
274
  "You are FarmLingua, an AI assistant for Nigerian farmers. "
275
+ "Answer questions directly and accurately with helpful farming advice. "
276
+ "Use clear, simple language with occasional emojis 🌾. "
277
+ "Be concise and focus on practical, actionable information. "
278
  "If asked who built you, say: 'KawaFarm LTD developed me to help farmers.'"
 
279
  )
280
 
281
+
282
+ context_info = ""
283
 
284
  if intent == "weather" and extra:
285
  weather_text = get_weather(extra)
286
+ context_info = f"\n\nCurrent weather information:\n{weather_text}"
287
+ elif intent == "live_update":
288
+ rag_context = retrieve_docs(translated_query, config.LIVE_VS_PATH)
289
+ if rag_context:
290
+ context_info = f"\n\nLatest agricultural updates:\n{rag_context}"
291
+ elif intent == "low_confidence":
292
+ rag_context = retrieve_docs(translated_query, config.STATIC_VS_PATH)
293
+ if rag_context:
294
+ context_info = f"\n\nRelevant information:\n{rag_context}"
295
+
296
+
297
+ user_message = translated_query + context_info
298
+ history.append({"role": "user", "content": user_message})
299
+
300
+
301
+ messages_for_qwen = build_messages_from_history(history, system_prompt)
302
+
303
+
304
+ max_tokens = 256 if intent == "weather" else 700
305
+ english_answer = run_qwen(messages_for_qwen, max_new_tokens=max_tokens)
306
 
307
  # Save assistant reply
308
  history.append({"role": "assistant", "content": english_answer})
 
310
  history = history[-MAX_HISTORY_MESSAGES:]
311
  memory_store.save_history(session_id, history)
312
 
313
+
314
  final_answer = (
315
  translate_text(english_answer, src_lang="eng_Latn", tgt_lang=lang_label)
316
  if lang_label != "eng_Latn"
317
  else english_answer
318
  )
319
  final_answer = strip_markdown(final_answer)
320
+
321
  return {
322
  "session_id": session_id,
323
  "detected_language": SUPPORTED_LANGS.get(lang_label, "Unknown"),
324
  "answer": final_answer
325
+ }