| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title>Agent AI </title>
- <style>
- body { font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 20px; }
- textarea, input, button { width: 100%; margin-bottom: 10px; padding: 8px; box-sizing: border-box; }
- button { cursor: pointer; }
- .response { background: #f0f0f0; padding: 10px; white-space: pre-wrap; border: 1px solid #ccc; min-height: 150px; }
- label { font-weight: bold; }
- </style>
- </head>
- <body>
- <h1>Agent AI </h1>
- <label for="username">Nom d'utilisateur :</label>
- <input type="text" id="username" placeholder="Entrez votre nom d'utilisateur" />
- <label for="password">Mot de passe :</label>
- <input type="password" id="password" placeholder="Entrez votre mot de passe" />
- <label for="userQuestion">Ta question :</label>
- <textarea id="userQuestion" rows="4" placeholder="Écris ta question ici..."></textarea>
- <button onclick="runFullChain()">Envoyer</button>
- <h2>Réponse :</h2>
- <div id="finalResponse" class="response" style="white-space: normal;"></div>
- <script>
- async function runFullChain() {
- const username = document.getElementById('username').value.trim();
- const password = document.getElementById('password').value.trim();
- const question = document.getElementById('userQuestion').value.trim();
- if (!username || !password) {
- alert('Merci d\'entrer votre nom d\'utilisateur et mot de passe.');
- return;
- }
- if (!question) {
- alert('Merci d\'écrire une question.');
- return;
- }
- const finalResponseElem = document.getElementById('finalResponse');
- finalResponseElem.textContent = 'Chargement...';
- try {
- // 1. Login
- const resLogin = await fetch('/login', {
- method: 'POST',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({username, password})
- });
- if (!resLogin.ok) {
- const err = await resLogin.json();
- finalResponseElem.textContent = 'Erreur login: ' + (err.error || resLogin.statusText);
- return;
- }
- // 2. Fetch pages
- const resPages = await fetch('/fetch_pages', { method: 'POST' });
- if (!resPages.ok) {
- finalResponseElem.textContent = 'Erreur récupération pages.';
- return;
- }
- const pages = await resPages.json();
- // 3. Chunk
- let chunks = [];
- for (const page of pages) {
- const url = page.url || "url inconnue";
- const fullText = page.text || "";
- const resChunk = await fetch('/chunk_all', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ text: fullText, url: url })
- });
- if (!resChunk.ok) {
- console.error(`Erreur découpage texte pour ${url}`);
- continue;
- }
- const chunkData = await resChunk.json();
- chunks.push(...chunkData.chunks);
- }
- // Extraire les URLs des pages pour la requête build_index
- const urls = pages.map(p => p.url || "url inconnue");
- // 4. Build index
- const resIndex = await fetch('/build_index', {
- method: 'POST',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({ texts: chunks, urls: urls }) // <-- urls défini ici
- });
- if (!resIndex.ok) {
- finalResponseElem.textContent = 'Erreur création index.';
- return;
- }
- // 5. Search index for relevant context
- const resSearch = await fetch('/search_index', {
- method: 'POST',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({ question: question })
- });
- if (!resSearch.ok) {
- finalResponseElem.textContent = 'Erreur recherche dans index.';
- return;
- }
- const searchResults = await resSearch.json();
- // Construire contexte à partir des meilleurs chunks
- let context = '';
- for (const r of searchResults.results) {
- context += `Extrait provenant de : ${r.url}\n${r.text}\n\n`;
- }
- // 6. QA chain
- const resQA = await fetch('/qa_chain', {
- method: 'POST',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({ context: context, question: question })
- });
- if (!resQA.ok) {
- finalResponseElem.textContent = 'Erreur lors de la réponse.';
- return;
- }
- const qaResult = await resQA.json();
- const rawAnswer = qaResult.answer || 'Aucune réponse obtenue.';
- const htmlAnswer = marked.parse(rawAnswer);
- finalResponseElem.innerHTML = htmlAnswer;
- } catch (e) {
- finalResponseElem.textContent = 'Erreur interne: ' + e.message;
- }
- }
- </script>
- <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
- </body>
- </html>
|