|
| 1 | +// We can define here the behavior for the elements on the website... |
| 2 | + |
| 3 | +// URL for POST requests |
| 4 | +const gptEndpoint = 'https://api.openai.com/v1/completions'; |
| 5 | + |
| 6 | +// Fetch DOM elements |
| 7 | +const reqButton = document.getElementById('button-request'); |
| 8 | +const reqStatus = document.getElementById('request-status'); |
| 9 | + |
| 10 | +// Attach click behavior to the button |
| 11 | +reqButton.onclick = function () { |
| 12 | + // Give some feedback to user |
| 13 | + reqStatus.innerHTML = "Request started..."; |
| 14 | + |
| 15 | + // Fetch image request data |
| 16 | + const key = document.getElementById('api-key').value; |
| 17 | + const prompt = document.getElementById('text-prompt').value; |
| 18 | + const radios = document.getElementsByName('text-model'); |
| 19 | + let model; |
| 20 | + for (let i = 0; i < radios.length; i++) { |
| 21 | + if (radios[i].checked) { |
| 22 | + model = radios[i].value; |
| 23 | + break; |
| 24 | + } |
| 25 | + } |
| 26 | + const tokens = Number(document.getElementById('token-count').value); |
| 27 | + const temp = Number(document.getElementById('temperature').value); |
| 28 | + |
| 29 | + // We won't do error-checking here, will leave that up to the server... |
| 30 | + |
| 31 | + // Form the request body according to the API: |
| 32 | + // https://api.openai.com/v1/completions |
| 33 | + const reqBody = { |
| 34 | + model: model, |
| 35 | + prompt: prompt, |
| 36 | + max_tokens: tokens, |
| 37 | + temperature: temp, |
| 38 | + top_p: 0.5, |
| 39 | + stream: false, |
| 40 | + logprobs: null, |
| 41 | + // stop: "\n" // this was returning empty completions |
| 42 | + }; |
| 43 | + |
| 44 | + // Form the data for a POST request: |
| 45 | + const reqParams = { |
| 46 | + method: 'POST', |
| 47 | + headers: { |
| 48 | + 'Content-Type': 'application/json', |
| 49 | + 'Authorization': `Bearer ${key}`, |
| 50 | + }, |
| 51 | + body: JSON.stringify(reqBody) |
| 52 | + } |
| 53 | + |
| 54 | + // Use the Fetch API to do an async POST request to OpenAI: |
| 55 | + // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API |
| 56 | + fetch(gptEndpoint, reqParams) |
| 57 | + .then(res => res.json()) |
| 58 | + .then(json => addText(json, prompt)) |
| 59 | + .catch(error => reqStatus.innerHTML = error) |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +/** |
| 65 | + * Add prompts + answers to the page. |
| 66 | + * @param {Object} jsonData The text completion API response |
| 67 | + * @param {String} prompt The original prompt that generated the text completion |
| 68 | + * @returns |
| 69 | + */ |
| 70 | +function addText(jsonData, prompt) { |
| 71 | + console.log(jsonData); |
| 72 | + |
| 73 | + // Handle a possible error response from the API |
| 74 | + if (jsonData.error) |
| 75 | + { |
| 76 | + reqStatus.innerHTML = 'ERROR: ' + jsonData.error.message; |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Parse the response object and attach a new text field to the page. |
| 81 | + const container = document.getElementById('text-container'); |
| 82 | + for (let i = 0; i < jsonData.choices.length; i++) |
| 83 | + { |
| 84 | + // Prompt text box |
| 85 | + const questionDiv = document.createElement('div'); |
| 86 | + questionDiv.className = "question"; |
| 87 | + const questionP = document.createElement('p'); |
| 88 | + questionP.innerHTML = prompt; |
| 89 | + questionDiv.appendChild(questionP); |
| 90 | + |
| 91 | + // Answer text box |
| 92 | + const textData = jsonData.choices[i].text; |
| 93 | + const answerDiv = document.createElement('div'); |
| 94 | + answerDiv.className = "answer"; |
| 95 | + const answerP = document.createElement('p'); |
| 96 | + answerP.innerHTML = textData; |
| 97 | + answerDiv.appendChild(answerP); |
| 98 | + |
| 99 | + // Reason text box |
| 100 | + let reasonData; |
| 101 | + switch (jsonData.choices[i].finish_reason) |
| 102 | + { |
| 103 | + case "length": |
| 104 | + reasonData = "(Text generation stopped due to text length)" |
| 105 | + break; |
| 106 | + case "stop": |
| 107 | + reasonData = "(Model decided this length of an answer was sufficient)" |
| 108 | + break; |
| 109 | + default: |
| 110 | + reasonData = "(Text generation stopped due to unknown reasons)" |
| 111 | + break; |
| 112 | + } |
| 113 | + const reasonDiv = document.createElement('div'); |
| 114 | + reasonDiv.className = "reason"; |
| 115 | + const reasonP = document.createElement('p'); |
| 116 | + reasonP.innerHTML = reasonData; |
| 117 | + reasonDiv.appendChild(reasonP); |
| 118 | + |
| 119 | + // Prepend them at the top of the container |
| 120 | + container.prepend( |
| 121 | + questionDiv, |
| 122 | + answerDiv, |
| 123 | + reasonDiv |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + // Log some feedback |
| 128 | + reqStatus.innerHTML = jsonData.choices.length +' responses received for "' + prompt + '"'; |
| 129 | +} |
0 commit comments