Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Step by step guide to run this code
- javascript:(function(){
- var text = window.getSelection().toString();
- if (!text) {
- text = prompt('Please enter the text you want to process with GPT-3.5');
- }
- if (text) {
- fetch('https://api.openai.com/v1/engines/davinci-codex/completions', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer YOUR_ACTUAL_API_KEY_HERE'
- },
- body: JSON.stringify({
- 'prompt': text,
- 'max_tokens': 60
- })
- })
- .then(response => response.json())
- .then(data => {
- alert('GPT-3.5 response: ' + data.choices[0].text.trim());
- })
- .catch(error => {
- console.error('Error:', error);
- });
- }
- })();
- Step 1: Get an OpenAI API Key
- Sign up for an account on the OpenAI platform.
- Navigate to your account settings or API section to obtain your API key.
- Step 2: Prepare Your Browser
- Open your web browser (Chrome, Firefox, etc.).
- Navigate to a webpage where you want to use the code.
- Right-click anywhere on the page and select "Inspect" or "Inspect Element" to open the developer tools.
- Step 3: Open the Console
- In the developer tools, locate the "Console" tab. This is where you'll paste and run the JavaScript code.
- Step 4: Paste and Run the Code
- Copy the provided JavaScript code.
- Paste the code into the console.
- Replace 'YOUR_ACTUAL_API_KEY_HERE' with your actual OpenAI API key.
- Step 5: Select Text
- On the webpage, select the text you want to process with GPT-3.5. This can be any text you find on the page.
- Step 6: Run the Code
- After replacing the API key and selecting text, press the "Enter" key to execute the code.
- If you haven't selected text, a prompt will appear asking you to enter the text manually.
- Step 7: Observe the Result
- Once you run the code, the GPT-3.5 response will be displayed in an alert box.
- The response will be the AI-generated completion of the provided text.
- Step 8: Adjust max_tokens
- The max_tokens parameter controls the length of the generated response. You can adjust this value to get shorter or longer responses.
- Step 9: Handle Errors
- If there's an error while executing the code (e.g., invalid API key), you'll see an error message in the console.
- Note:
- This code is designed to be run directly in the browser's developer console on a webpage.
- The code interacts with the OpenAI API to generate AI responses based on the selected or manually entered text.
- Be careful with your API key. Do not share it publicly or expose it in your code.
- This guide assumes you have basic familiarity with browser developer tools and JavaScript.
- Remember that using the OpenAI API and interacting with external services via JavaScript requires understanding security concerns and proper usage of API keys. Always follow best practices for API usage and keep your API key secure.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement