Thunder-Menu

Chatbot_ScrollBack_Davinci2.html

Mar 22nd, 2023 (edited)
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.93 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <title>Chatbot</title>
  6.   </head>
  7.   <body>
  8.     <div>
  9.       <textarea id="message-box" rows="10" cols="50" readonly></textarea>
  10.     </div>
  11.     <div>
  12.       <textarea id="message-input" rows="5" cols="50"></textarea>
  13.       <button id="send-button">Send</button>
  14.     </div>
  15. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  16. <script>
  17.   const OPENAI_API_KEY = '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';
  18.   const PROXY_URL = 'https://api.openai.com/v1';
  19.   const MAX_LINES = 2048;
  20.   const GPT_MODEL = 'text-davinci-003';
  21.  
  22.   function sendMessage() {
  23.     const messageInput = document.getElementById('message-input');
  24.     const messageBox = document.getElementById('message-box');
  25.     const message = messageInput.value.trim();
  26.  
  27.     if (!message) {
  28.       return;
  29.     }
  30.  
  31.     messageInput.value = '';
  32.     messageBox.innerHTML += `You: ${message}\n`;
  33.  
  34.     const prompt = `${message}\nAI:`;
  35.  
  36.     $.ajax({
  37.       url: `${PROXY_URL}/engines/${GPT_MODEL}/completions`,
  38.       type: 'POST',
  39.       beforeSend: function (xhr) {
  40.         xhr.setRequestHeader('Content-Type', 'application/json');
  41.         xhr.setRequestHeader('Authorization', `Bearer ${OPENAI_API_KEY}`);
  42.       },
  43.       data: JSON.stringify({
  44.         prompt: prompt,
  45.         max_tokens: 2048,
  46.         temperature: 1,
  47.         n: 25,
  48.         stop: 'AI:',
  49.       }),
  50.     })
  51.       .done(function (response) {
  52.         const answer = response.choices[0].text.trim();
  53.  
  54.         messageBox.innerHTML += `AI: ${answer}\n`;
  55.  
  56.         // Limit the number of lines in the message box
  57.         const lines = messageBox.value.split('\n');
  58.         if (lines.length > MAX_LINES) {
  59.           messageBox.value = lines.slice(lines.length - MAX_LINES).join('\n');
  60.         }
  61.  
  62.         // Scroll to the bottom of the message box
  63.         messageBox.scrollTop = messageBox.scrollHeight;
  64.       })
  65.       .fail(function (jqXHR, textStatus, errorThrown) {
  66.         console.error(`Request failed: ${textStatus}, ${errorThrown}`);
  67.       });
  68.   }
  69.  
  70.   document.addEventListener('DOMContentLoaded', function () {
  71.     const sendButton = document.getElementById('send-button');
  72.     sendButton.addEventListener('click', sendMessage);
  73.  
  74.     const messageInput = document.getElementById('message-input');
  75.     messageInput.addEventListener('keydown', function (event) {
  76.       if (event.shiftKey && event.keyCode === 13) {
  77.        // Shift + Enter
  78.        return;
  79.       }
  80.  
  81.       if (event.keyCode === 13) {
  82.         // Enter
  83.         event.preventDefault();
  84.         sendMessage();
  85.       }
  86.     });
  87.  
  88.     const messageBox = document.getElementById('message-box');
  89.     messageBox.addEventListener('wheel', function (event) {
  90.     if (event.deltaY < 0) {
  91.      // Scrolling up
  92.      messageBox.scrollTop -= 10;
  93.    } else {
  94.      // Scrolling down
  95.      messageBox.scrollTop += 10;
  96.    }
  97.  });
  98. });
  99. </script>
  100.  
  101. </body>
  102. </html>
  103.  
Add Comment
Please, Sign In to add comment