Advertisement
sEi_

Simple GPT html client

Jun 24th, 2023 (edited)
1,375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>OpenAI Chat Interface</title>
  5.   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  6. </head>
  7. <body>
  8.   <h1>OpenAI Chat Interface</h1>
  9.  
  10.   <div id="chat-container">
  11.     <div id="chat-log"></div>
  12.     <input type="text" id="user-input" placeholder="Type your message...">
  13.     <button id="send-btn">Send</button>
  14.   </div>
  15.  
  16.   <script>
  17.     $(document).ready(function() {
  18.       const API_ENDPOINT = 'https://api.openai.com/v1/chat/completions';
  19.       const MODEL = 'gpt-3.5-turbo'; //or if you have access to 'gpt-4'
  20.      
  21.       function sendMessage() {
  22.         const userInput = $('#user-input').val();
  23.         if (userInput.trim() === '') {
  24.           return;
  25.         }
  26.        
  27.         $('#chat-log').append('<p><strong>You:</strong> ' + userInput + '</p>');
  28.         $('#user-input').val('');
  29.        
  30.         $.ajax({
  31.           url: API_ENDPOINT,
  32.           type: 'POST',
  33.           headers: {
  34.             'Content-Type': 'application/json',
  35.             'Authorization': 'Bearer YOUR_API_KEY'
  36.           },
  37.           data: JSON.stringify({
  38.             model: MODEL,
  39.             messages: [{ role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: userInput }]
  40.           }),
  41.           success: function(response) {
  42.             const answer = response.choices[0].message.content;
  43.             $('#chat-log').append('<p><strong>Assistant:</strong> ' + answer + '</p>');
  44.           },
  45.           error: function(xhr) {
  46.             console.error(xhr.responseText);
  47.           }
  48.         });
  49.       }
  50.      
  51.       $('#send-btn').click(sendMessage);
  52.       $('#user-input').keypress(function(e) {
  53.         if (e.which === 13) {
  54.           sendMessage();
  55.         }
  56.       });
  57.     });
  58.   </script>
  59. </body>
  60. </html>
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement