4.6 Exercice : historique de conversation

7. 4.6 Exercice : historique de conversation

Modifiez askLLM() pour conserver les 3 derniers échanges. Cela permet au LLM de "se souvenir" du contexte entre les questions.

#define MAX_HISTORY 3

struct Exchange {
  String question;
  String answer;
};

Exchange history[MAX_HISTORY];
int historyCount = 0;
int historyIdx   = 0;

String askWithHistory(const String& question) {
  // Build context from history
  String ctx =
    "Tu es Zacus, un professeur excentrique. "
    "Reponds en francais, de maniere concise.\n\n";

  int count = min(historyCount, MAX_HISTORY);
  int start = (historyCount < MAX_HISTORY) ? 0 : historyIdx;
  for (int i = 0; i < count; i++) {
    int idx = (start + i) % MAX_HISTORY;
    ctx += "Humain: " + history[idx].question + "\n";
    ctx += "Zacus: "  + history[idx].answer   + "\n\n";
  }
  ctx += "Humain: " + question + "\nZacus: ";

  String answer = askLLM(ctx);

  // Store exchange in circular buffer
  history[historyIdx].question = question;
  history[historyIdx].answer   = answer;
  historyIdx = (historyIdx + 1) % MAX_HISTORY;
  historyCount++;

  return answer;
}

Comportement attendu :

[Q1] Quelle est la capitale de la France ?
[A1] La capitale de la France est Paris.

[Q2] Et sa population ?
[A2] Paris compte environ 2,1 millions d'habitants intra-muros.
     ("sa" est resolu grace au contexte)

Se connecter pour suivre votre progression.