index.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!DOCTYPE html>
  2. <html lang="fr">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <title>Agent AI </title>
  7. <style>
  8. body { font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 20px; }
  9. textarea, input, button { width: 100%; margin-bottom: 10px; padding: 8px; box-sizing: border-box; }
  10. button { cursor: pointer; }
  11. .response { background: #f0f0f0; padding: 10px; white-space: pre-wrap; border: 1px solid #ccc; min-height: 150px; }
  12. label { font-weight: bold; }
  13. </style>
  14. </head>
  15. <body>
  16. <h1>Agent AI </h1>
  17. <label for="username">Nom d'utilisateur :</label>
  18. <input type="text" id="username" placeholder="Entrez votre nom d'utilisateur" />
  19. <label for="password">Mot de passe :</label>
  20. <input type="password" id="password" placeholder="Entrez votre mot de passe" />
  21. <label for="userQuestion">Ta question :</label>
  22. <textarea id="userQuestion" rows="4" placeholder="Écris ta question ici..."></textarea>
  23. <button onclick="runFullChain()">Envoyer</button>
  24. <h2>Réponse :</h2>
  25. <div id="finalResponse" class="response" style="white-space: normal;"></div>
  26. <script>
  27. async function runFullChain() {
  28. const username = document.getElementById('username').value.trim();
  29. const password = document.getElementById('password').value.trim();
  30. const question = document.getElementById('userQuestion').value.trim();
  31. if (!username || !password) {
  32. alert('Merci d\'entrer votre nom d\'utilisateur et mot de passe.');
  33. return;
  34. }
  35. if (!question) {
  36. alert('Merci d\'écrire une question.');
  37. return;
  38. }
  39. const finalResponseElem = document.getElementById('finalResponse');
  40. finalResponseElem.textContent = 'Chargement...';
  41. try {
  42. // 1. Login
  43. const resLogin = await fetch('/login', {
  44. method: 'POST',
  45. headers: {'Content-Type': 'application/json'},
  46. body: JSON.stringify({username, password})
  47. });
  48. if (!resLogin.ok) {
  49. const err = await resLogin.json();
  50. finalResponseElem.textContent = 'Erreur login: ' + (err.error || resLogin.statusText);
  51. return;
  52. }
  53. // 2. Fetch pages
  54. const resPages = await fetch('/fetch_pages', { method: 'POST' });
  55. if (!resPages.ok) {
  56. finalResponseElem.textContent = 'Erreur récupération pages.';
  57. return;
  58. }
  59. const pages = await resPages.json();
  60. // 3. Chunk
  61. let chunks = [];
  62. for (const page of pages) {
  63. const url = page.url || "url inconnue";
  64. const fullText = page.text || "";
  65. const resChunk = await fetch('/chunk_all', {
  66. method: 'POST',
  67. headers: { 'Content-Type': 'application/json' },
  68. body: JSON.stringify({ text: fullText, url: url })
  69. });
  70. if (!resChunk.ok) {
  71. console.error(`Erreur découpage texte pour ${url}`);
  72. continue;
  73. }
  74. const chunkData = await resChunk.json();
  75. chunks.push(...chunkData.chunks);
  76. }
  77. // Extraire les URLs des pages pour la requête build_index
  78. const urls = pages.map(p => p.url || "url inconnue");
  79. // 4. Build index
  80. const resIndex = await fetch('/build_index', {
  81. method: 'POST',
  82. headers: {'Content-Type': 'application/json'},
  83. body: JSON.stringify({ texts: chunks, urls: urls }) // <-- urls défini ici
  84. });
  85. if (!resIndex.ok) {
  86. finalResponseElem.textContent = 'Erreur création index.';
  87. return;
  88. }
  89. // 5. Search index for relevant context
  90. const resSearch = await fetch('/search_index', {
  91. method: 'POST',
  92. headers: {'Content-Type': 'application/json'},
  93. body: JSON.stringify({ question: question })
  94. });
  95. if (!resSearch.ok) {
  96. finalResponseElem.textContent = 'Erreur recherche dans index.';
  97. return;
  98. }
  99. const searchResults = await resSearch.json();
  100. // Construire contexte à partir des meilleurs chunks
  101. let context = '';
  102. for (const r of searchResults.results) {
  103. context += `Extrait provenant de : ${r.url}\n${r.text}\n\n`;
  104. }
  105. // 6. QA chain
  106. const resQA = await fetch('/qa_chain', {
  107. method: 'POST',
  108. headers: {'Content-Type': 'application/json'},
  109. body: JSON.stringify({ context: context, question: question })
  110. });
  111. if (!resQA.ok) {
  112. finalResponseElem.textContent = 'Erreur lors de la réponse.';
  113. return;
  114. }
  115. const qaResult = await resQA.json();
  116. const rawAnswer = qaResult.answer || 'Aucune réponse obtenue.';
  117. const htmlAnswer = marked.parse(rawAnswer);
  118. finalResponseElem.innerHTML = htmlAnswer;
  119. } catch (e) {
  120. finalResponseElem.textContent = 'Erreur interne: ' + e.message;
  121. }
  122. }
  123. </script>
  124. <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
  125. </body>
  126. </html>