· 2 years ago · Aug 14, 2023, 02:15 AM
1User
2Step by step guide to run this code
3
4javascript:(function(){
5 var text = window.getSelection().toString();
6 if (!text) {
7 text = prompt('Please enter the text you want to process with GPT-3.5');
8 }
9 if (text) {
10 fetch('https://api.openai.com/v1/engines/davinci-codex/completions', {
11 method: 'POST',
12 headers: {
13 'Content-Type': 'application/json',
14 'Authorization': 'Bearer YOUR_ACTUAL_API_KEY_HERE'
15 },
16 body: JSON.stringify({
17 'prompt': text,
18 'max_tokens': 60
19 })
20 })
21 .then(response => response.json())
22 .then(data => {
23 alert('GPT-3.5 response: ' + data.choices[0].text.trim());
24 })
25 .catch(error => {
26 console.error('Error:', error);
27 });
28 }
29})();
30ChatGPT
31
32
33
34Step 1: Get an OpenAI API Key
35
36 Sign up for an account on the OpenAI platform.
37 Navigate to your account settings or API section to obtain your API key.
38
39Step 2: Prepare Your Browser
40
41 Open your web browser (Chrome, Firefox, etc.).
42 Navigate to a webpage where you want to use the code.
43 Right-click anywhere on the page and select "Inspect" or "Inspect Element" to open the developer tools.
44
45Step 3: Open the Console
46
47 In the developer tools, locate the "Console" tab. This is where you'll paste and run the JavaScript code.
48
49Step 4: Paste and Run the Code
50
51 Copy the provided JavaScript code.
52 Paste the code into the console.
53 Replace 'YOUR_ACTUAL_API_KEY_HERE' with your actual OpenAI API key.
54
55Step 5: Select Text
56
57 On the webpage, select the text you want to process with GPT-3.5. This can be any text you find on the page.
58
59Step 6: Run the Code
60
61 After replacing the API key and selecting text, press the "Enter" key to execute the code.
62 If you haven't selected text, a prompt will appear asking you to enter the text manually.
63
64Step 7: Observe the Result
65
66 Once you run the code, the GPT-3.5 response will be displayed in an alert box.
67 The response will be the AI-generated completion of the provided text.
68
69Step 8: Adjust max_tokens
70
71 The max_tokens parameter controls the length of the generated response. You can adjust this value to get shorter or longer responses.
72
73Step 9: Handle Errors
74
75 If there's an error while executing the code (e.g., invalid API key), you'll see an error message in the console.
76
77Note:
78
79 This code is designed to be run directly in the browser's developer console on a webpage.
80 The code interacts with the OpenAI API to generate AI responses based on the selected or manually entered text.
81 Be careful with your API key. Do not share it publicly or expose it in your code.
82 This guide assumes you have basic familiarity with browser developer tools and JavaScript.
83
84Remember 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.