IZERE HIRWA Roger commited on
Commit
0705327
·
1 Parent(s): b429bfa
Files changed (1) hide show
  1. app.py +31 -31
app.py CHANGED
@@ -41,29 +41,48 @@ def _retry_openai_call(func, *args, _retries=1, _delay=0.5, **kwargs):
41
  last_err = e
42
  app.logger.warning(f"OpenAI call attempt {attempt + 1} failed: {e}")
43
  if attempt < _retries:
44
- time.sleep(_delay) # Use time.sleep instead of _time.sleep
45
  else:
 
46
  raise last_err
47
 
48
  # --- Minimal retry helper for Ollama style calls (keeps API parity with older code) ---
49
  def _retry_ollama_call(func, *args, _retries=1, _delay=0.5, **kwargs):
50
  """
51
- Retry wrapper for calls to ollama.* functions to mirror the behavior of _retry_openai_call.
52
- Accepts the target callable as 'func' and forwards args/kwargs; retries on exceptions.
53
  """
54
  last_err = None
55
  for attempt in range(_retries + 1):
56
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  return func(*args, **kwargs)
58
  except Exception as e:
59
  last_err = e
60
  try:
61
- app.logger.warning(f"Ollama call attempt {attempt + 1} failed: {e}")
62
  except Exception:
63
  pass
64
  if attempt < _retries:
65
- time.sleep(_delay) # Use time.sleep instead of _time.sleep
66
  else:
 
67
  raise last_err
68
 
69
  # --- Helper Functions ---
@@ -2076,7 +2095,12 @@ CONTEXT:
2076
 
2077
  # Use retry wrapper (now fixed to remove timeout parameter)
2078
  app.logger.info(f"Sending messages to Ollama: {len(messages)} messages")
2079
- reply = _retry_ollama_call(ollama.chat, model=chat_model, messages=messages, options={"temperature": 0.2, "top_p": 0.9})
 
 
 
 
 
2080
  answer = reply.get("message", {}).get("content", "") or ""
2081
  app.logger.info(f"Ollama response received: {answer[:100]}...")
2082
 
@@ -2100,31 +2124,7 @@ CONTEXT:
2100
  elif target_language == 'fr':
2101
  answer = f"Bonjour! Je suis AIMHSA, votre compagnon de santé mentale pour le Rwanda. Comment puis-je vous aider aujourd'hui? Pour une aide immédiate, contactez la ligne d'assistance en santé mentale au 105."
2102
  elif target_language == 'rw':
2103
- answer = f"Muraho! Nitwa AIMHSA, umufasha wawe w'ubuzima bw'ubwoba mu Rwanda. Nshobora gufasha ute uyu munsi? Niba ukeneye ubufasha buhagije, hamagara 105."
2104
- elif target_language == 'sw':
2105
- answer = f"Hujambo! Mimi ni AIMHSA, msaidizi wako wa afya ya akili wa Rwanda. Ninawezaje kukusaidia leo? Ikiwa unahitaji msaada wa haraka, piga simu 105."
2106
- else:
2107
- answer = f"Hello! I'm AIMHSA, your mental health companion for Rwanda. How can I support you today? If you need immediate help, contact the Mental Health Hotline at 105."
2108
-
2109
- # Enforce final-language safety: if model mixed languages or replied in wrong language,
2110
- # translate to target_language before returning
2111
- try:
2112
- detected_answer_lang = translation_service.detect_language(answer)
2113
- if detected_answer_lang != target_language and answer.strip():
2114
- app.logger.info(f"Answer language {detected_answer_lang} != target {target_language}; translating")
2115
- answer = translation_service.translate_text(answer, target_language)
2116
- except Exception as _e:
2117
- app.logger.warning(f"Post-translate guard failed: {_e}")
2118
-
2119
- # If target is Kinyarwanda, apply normalization to clean any leaked phrases
2120
- try:
2121
- if target_language == 'rw' and answer.strip():
2122
- answer = translation_service.normalize_kinyarwanda(answer)
2123
- except Exception as _e:
2124
- app.logger.warning(f"Kinyarwanda normalization failed: {_e}")
2125
- app.logger.info(f"Generated response in {target_language}: {answer[:50]}...")
2126
-
2127
- # Ensure we never return an empty answer
2128
  if not isinstance(answer, str) or not answer.strip():
2129
  app.logger.warning("Empty answer received, using language-specific fallback")
2130
 
 
41
  last_err = e
42
  app.logger.warning(f"OpenAI call attempt {attempt + 1} failed: {e}")
43
  if attempt < _retries:
44
+ time.sleep(_delay * (attempt + 1))
45
  else:
46
+ app.logger.error(f"All {_retries + 1} OpenAI call attempts failed")
47
  raise last_err
48
 
49
  # --- Minimal retry helper for Ollama style calls (keeps API parity with older code) ---
50
  def _retry_ollama_call(func, *args, _retries=1, _delay=0.5, **kwargs):
51
  """
52
+ Retry wrapper for OpenAI API calls that maintains compatibility with old Ollama calls.
53
+ Maps Ollama-style parameters to OpenAI format.
54
  """
55
  last_err = None
56
  for attempt in range(_retries + 1):
57
  try:
58
+ # Map Ollama chat parameters to OpenAI format
59
+ if func == openai_client.chat.completions.create:
60
+ # Extract options if present
61
+ options = kwargs.pop('options', {})
62
+ # Merge options into kwargs
63
+ kwargs.update({
64
+ 'temperature': options.get('temperature', 0.7),
65
+ 'top_p': options.get('top_p', 0.9),
66
+ 'max_tokens': options.get('max_tokens', 1024)
67
+ })
68
+ response = func(*args, **kwargs)
69
+ # Map OpenAI response to Ollama format
70
+ return {
71
+ 'message': {
72
+ 'content': response.choices[0].message.content
73
+ }
74
+ }
75
  return func(*args, **kwargs)
76
  except Exception as e:
77
  last_err = e
78
  try:
79
+ app.logger.error(f"API call failed (attempt {attempt + 1}): {str(e)}")
80
  except Exception:
81
  pass
82
  if attempt < _retries:
83
+ time.sleep(_delay * (attempt + 1))
84
  else:
85
+ app.logger.error(f"All {_retries + 1} API call attempts failed")
86
  raise last_err
87
 
88
  # --- Helper Functions ---
 
2095
 
2096
  # Use retry wrapper (now fixed to remove timeout parameter)
2097
  app.logger.info(f"Sending messages to Ollama: {len(messages)} messages")
2098
+ reply = _retry_ollama_call(
2099
+ openai_client.chat.completions.create,
2100
+ model=chat_model,
2101
+ messages=messages,
2102
+ options={"temperature": 0.2, "top_p": 0.9}
2103
+ )
2104
  answer = reply.get("message", {}).get("content", "") or ""
2105
  app.logger.info(f"Ollama response received: {answer[:100]}...")
2106
 
 
2124
  elif target_language == 'fr':
2125
  answer = f"Bonjour! Je suis AIMHSA, votre compagnon de santé mentale pour le Rwanda. Comment puis-je vous aider aujourd'hui? Pour une aide immédiate, contactez la ligne d'assistance en santé mentale au 105."
2126
  elif target_language == 'rw':
2127
+ answer = f"Muraho! Nitwa AIMHSA, umufasha wawe w'ubuzima bw'ubwoba bw'u Rwanda. Nakora iki ngo ngufashe uyu munsi? Niba ukeneye ubufasha bwihuse, hamagara Ligne d'assistance en santé mentale ku 105."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2128
  if not isinstance(answer, str) or not answer.strip():
2129
  app.logger.warning("Empty answer received, using language-specific fallback")
2130