Skip to content

Instantly share code, notes, and snippets.

@dhruv-anand-aintech
Last active November 14, 2023 12:48
Show Gist options
  • Save dhruv-anand-aintech/e56219d16bb0b5d6d8321b4b15b85d07 to your computer and use it in GitHub Desktop.
Save dhruv-anand-aintech/e56219d16bb0b5d6d8321b4b15b85d07 to your computer and use it in GitHub Desktop.
OpenAI finetune example generator bookmarklet
javascript:(function()%7Bjavascript%3A (function () %7B%0A %2F%2F Function to display a toast message%0A function showToast(message) %7B%0A var toast %3D document.createElement('div')%3B%0A toast.textContent %3D message%3B%0A Object.assign(toast.style%2C %7B%0A position%3A 'fixed'%2C%0A bottom%3A '20px'%2C%0A left%3A '50%25'%2C%0A transform%3A 'translateX(-50%25)'%2C%0A backgroundColor%3A 'black'%2C%0A color%3A 'white'%2C%0A padding%3A '10px'%2C%0A borderRadius%3A '5px'%2C%0A zIndex%3A '1000'%2C%0A fontSize%3A '14px'%0A %7D)%3B%0A document.body.appendChild(toast)%3B%0A setTimeout(function () %7B document.body.removeChild(toast)%3B %7D%2C 3000)%3B%0A %7D%0A%0A console.log('Checking URL...')%3B%0A %2F%2F Check the URL%0A if (window.location.href.startsWith('https%3A%2F%2Fplatform.openai.com%2Fplayground')) %7B%0A console.log('URL is correct.')%3B%0A %2F%2F Select the button with XPath%0A var xpath %3D "%2F%2Fbutton%5Bcontains(.%2C 'View code')%5D"%3B%0A var viewCodeButton %3D document.evaluate(xpath%2C document%2C null%2C XPathResult.FIRST_ORDERED_NODE_TYPE%2C null).singleNodeValue%3B%0A%0A if (viewCodeButton) %7B%0A console.log('View code button found.')%3B%0A viewCodeButton.click()%3B%0A%0A setTimeout(function () %7B%0A var modalContent %3D document.querySelector('.modal-body')%3B%0A if (modalContent) %7B%0A console.log('Modal content found.')%3B%0A %2F%2F Select JSON in the dropdown%0A var select %3D document.querySelector('.code-sample-select.api-code-lang-select')%3B%0A if (select) %7B%0A console.log('Dropdown found.')%3B%0A select.value %3D 'json'%3B%0A var event %3D new Event('change'%2C %7B bubbles%3A true %7D)%3B%0A select.dispatchEvent(event)%3B%0A %7D%0A%0A var text %3D modalContent.innerText%3B%0A %2F%2F find first %7B and last %7D%0A var jsonPart %3D text.substring(text.indexOf('%7B')%2C text.lastIndexOf('%7D') %2B 1)%3B%0A jsonPart %3D JSON.parse(jsonPart)%3B%0A %2F%2F stringify the messages array%0A var messages %3D JSON.stringify(jsonPart%5B'messages'%5D)%3B%0A console.log('messages'%2C messages)%3B%0A %2F%2F formattedContent is a JSON object with a messages array as value for the key "messages"%0A var formattedContent %3D JSON.stringify(%7B "messages"%3A JSON.parse(messages) %7D)%3B%0A %2F%2F minify the JSON%0A console.log(JSON.parse(formattedContent))%3B%0A%0A var textArea %3D document.createElement('textarea')%3B%0A textArea.value %3D formattedContent%3B%0A document.body.appendChild(textArea)%3B%0A textArea.select()%3B%0A navigator.clipboard.writeText(textArea.value)%0A .then(() %3D> %7B%0A console.log('Content copied to clipboard.')%3B%0A document.body.removeChild(textArea)%3B%0A showToast('Finetune example was copied to clipboard')%3B%0A %2F%2F Dismiss the modal%0A var closeButton %3D document.querySelector('.modal-footer button')%3B%0A if (closeButton) closeButton.click()%3B%0A%0A %2F%2F download a text file with the content%0A var element %3D document.createElement('a')%3B%0A element.setAttribute('href'%2C 'data%3Atext%2Fplain%3Bcharset%3Dutf-8%2C' %2B encodeURIComponent(formattedContent))%3B%0A %2F%2F create a hash for the filename based on the text%0A var hash %3D 0%2C i%2C chr%3B%0A if (formattedContent.length %3D%3D%3D 0) hash %3D 1%3B%0A for (i %3D 0%3B i < formattedContent.length%3B i%2B%2B) %7B%0A chr %3D formattedContent.charCodeAt(i)%3B%0A hash %3D ((hash << 5) - hash) %2B chr%3B%0A hash %7C%3D 0%3B %2F%2F Convert to 32bit integer%0A %7D%0A element.setAttribute('download'%2C 'openai-ft-example-'%2Bhash%2B'.json')%3B%0A element.style.display %3D 'none'%3B%0A document.body.appendChild(element)%3B%0A element.click()%3B%0A document.body.removeChild(element)%3B%0A %7D)%0A .catch((error) %3D> %7B%0A console.error('Failed to copy text%3A '%2C error)%3B%0A %7D)%3B%0A %7D%0A %7D%2C 1000)%3B%0A %7D else %7B%0A showToast('Button with "View Code" not found')%3B%0A %7D%0A %7D else %7B%0A showToast('This bookmarklet only works on https%3A%2F%2Fplatform.openai.com%2Fplayground')%3B%0A %7D%0A%7D)()%3B%7D)()%3B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment