Claude commited on
Commit
60bcd69
·
unverified ·
1 Parent(s): 00e3d64

Make companion respond in the selected language (EN/FR)

Browse files

The frontend now sends the current language to the backend.
The system prompt includes a language instruction so the LLM
responds in English or French matching the UI language.

https://claude.ai/code/session_015z3yZxNNfXF63JuQDuPbEG

Files changed (3) hide show
  1. app/llm.py +8 -1
  2. app/main.py +2 -1
  3. static/app.js +2 -0
app/llm.py CHANGED
@@ -93,7 +93,13 @@ Produis un JSON strict avec cette structure :
93
  Réponds UNIQUEMENT avec le JSON, sans texte autour."""
94
 
95
 
96
- def build_system_prompt(mode: str, topic: str, phase: int, rag_chunks: list[str]) -> str:
 
 
 
 
 
 
97
  """Build the full system prompt with mode, phase guidance, and RAG context."""
98
  template = SYSTEM_TUTOR if mode == "TUTOR" else SYSTEM_CRITIC
99
 
@@ -104,6 +110,7 @@ def build_system_prompt(mode: str, topic: str, phase: int, rag_chunks: list[str]
104
  .replace("{phase}", str(phase)))
105
 
106
  prompt += f"\n\n{PHASE_GUIDANCE.get(phase, PHASE_GUIDANCE[0])}"
 
107
 
108
  return prompt
109
 
 
93
  Réponds UNIQUEMENT avec le JSON, sans texte autour."""
94
 
95
 
96
+ LANG_INSTRUCTION = {
97
+ "en": "\n\nIMPORTANT: You MUST respond entirely in English.",
98
+ "fr": "\n\nIMPORTANT: Tu DOIS répondre entièrement en français.",
99
+ }
100
+
101
+
102
+ def build_system_prompt(mode: str, topic: str, phase: int, rag_chunks: list[str], lang: str = "en") -> str:
103
  """Build the full system prompt with mode, phase guidance, and RAG context."""
104
  template = SYSTEM_TUTOR if mode == "TUTOR" else SYSTEM_CRITIC
105
 
 
110
  .replace("{phase}", str(phase)))
111
 
112
  prompt += f"\n\n{PHASE_GUIDANCE.get(phase, PHASE_GUIDANCE[0])}"
113
+ prompt += LANG_INSTRUCTION.get(lang, LANG_INSTRUCTION["en"])
114
 
115
  return prompt
116
 
app/main.py CHANGED
@@ -42,6 +42,7 @@ class ChatRequest(BaseModel):
42
  topic: str = ""
43
  phase: int = 0
44
  phase_turns: int = 0
 
45
  history: list[dict] = []
46
 
47
 
@@ -113,7 +114,7 @@ async def api_chat(req: ChatRequest):
113
  logger.info(f"Chat request: mode={req.mode}, topic={req.topic[:50]}, phase={req.phase}->{new_phase}, turns={req.phase_turns}->{new_phase_turns}, model={model}")
114
 
115
  rag_chunks = retrieve(req.message)
116
- system_prompt = build_system_prompt(req.mode, req.topic, new_phase, rag_chunks)
117
 
118
  messages = [{"role": m["role"], "content": m["content"]} for m in req.history]
119
  messages.append({"role": "user", "content": req.message})
 
42
  topic: str = ""
43
  phase: int = 0
44
  phase_turns: int = 0
45
+ lang: str = "en"
46
  history: list[dict] = []
47
 
48
 
 
114
  logger.info(f"Chat request: mode={req.mode}, topic={req.topic[:50]}, phase={req.phase}->{new_phase}, turns={req.phase_turns}->{new_phase_turns}, model={model}")
115
 
116
  rag_chunks = retrieve(req.message)
117
+ system_prompt = build_system_prompt(req.mode, req.topic, new_phase, rag_chunks, req.lang)
118
 
119
  messages = [{"role": m["role"], "content": m["content"]} for m in req.history]
120
  messages.append({"role": "user", "content": req.message})
static/app.js CHANGED
@@ -360,6 +360,7 @@
360
  topic: state.topic,
361
  phase: state.phase,
362
  phase_turns: state.phaseTurns,
 
363
  history: state.history.slice(0, -1)
364
  })
365
  })
@@ -580,6 +581,7 @@
580
  topic: state.topic,
581
  phase: state.phase,
582
  phase_turns: state.phaseTurns,
 
583
  history: []
584
  })
585
  })
 
360
  topic: state.topic,
361
  phase: state.phase,
362
  phase_turns: state.phaseTurns,
363
+ lang: currentLang,
364
  history: state.history.slice(0, -1)
365
  })
366
  })
 
581
  topic: state.topic,
582
  phase: state.phase,
583
  phase_turns: state.phaseTurns,
584
+ lang: currentLang,
585
  history: []
586
  })
587
  })