Code viewer for World: Chat with GPT model (clone...

// Cloned by Sharjil Dhanani on 27 Nov 2023 from World "Chat with GPT model" by Starter user 
// Please leave this clone trail here.
 


// talk to OpenAI GPT model (ChatGPT)
// adapted from:
//  https://platform.openai.com/docs/api-reference/making-requests


const rapidAPIUrl = "https://robomatic-ai.p.rapidapi.com/api";           // can POST to this 3rd party URL
  
//const themodel = "gpt-3.5-turbo";       // the OpenAI model we are going to talk to 
    

// default API key and prompt:

var apikey = "";
var theprompt = "hello";


 
// default body is margin 0 and padding 0 
// give it more whitespace:

  $('body').css( "margin", "20px" );
  $('body').css( "padding", "20px" );


 
document.write (`
        <h1>Interact with Another API</h1>

        <div id="response-container" class="container" style="background-color: #ffffcc; border: 1px solid black;">
            <h3>API Response</h3>
            <div id="api-response"></div>
        </div>

        <div class="container" style="background-color: white; border: 1px solid black;">
            <h3>Make an API Request</h3>
            <label for="user-input">Enter a query:</label>
            <input id="user-input" style="width: 50vw;" value="What's 2 plus 5?">
            <button onclick="makeApiRequest()" class="ab-normbutton">Send Request</button>
        </div>

        <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
        <script>
            function makeApiRequest() {
                const userInput = $('#user-input').val();

                const settings = {
                    async: true,
                    crossDomain: true,
                    url: 'https://robomatic-ai.p.rapidapi.com/api',
                    method: 'POST',
                    headers: {
                        'content-type': 'application/x-www-form-urlencoded',
                        'X-RapidAPI-Key': '3835be4181msha063475a102fcc8p1250bajsnfaa48379b019',
                        'X-RapidAPI-Host': 'robomatic-ai.p.rapidapi.com'
                    },
                    data: {
                        in: userInput,
                        op: 'in',
                        cbot: '1',
                        SessionID: 'RapidAPI1',
                        cbid: '1',
                        key: 'RHMN5hnQ4wTYZBGCF3dfxzypt68rVP',
                        ChatSource: 'RapidAPI',
                        duration: '1'
                    }
                };

                $.ajax(settings)
                    .done(function (response) {
                        displayApiResponse(response);
                    })
                    .fail(function () {
                        displayError();
                    });
            }

            function displayApiResponse(response) {
                const outContent = response.out;
                $('#api-response').html('<p>' + outContent + '</p>');
            }

            function displayError() {
                $('#api-response').html('<font color="red"><b>Error in API request.</b></font>');
            }
        </script>

` );