Advertisement
A_GUES

SCRATCH ChatGPT

Jul 5th, 2023 (edited)
1,066
1
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class AIBlock {
  2.     getInfo() {
  3.         return {
  4.             "id": "AI",
  5.             "name": "AI",
  6.             "blocks": [{
  7.                 "opcode": "completePrompt",
  8.                 "blockType": "reporter",
  9.                 "text": "complete prompt [string]",
  10.                 "arguments": {
  11.                     "string": {
  12.                         "type": "string",
  13.                         "defaultValue": "Explain quantum computing in simple terms"
  14.                     }
  15.                 }
  16.             }],
  17.             "menus": {}
  18.         };
  19.     }
  20.  
  21.     async completePrompt({ string }) {
  22.         const text = string.trim();
  23.         const url = `https://api.openai.com/v1/engines/davinci/completions`;
  24.  
  25.         const options = {
  26.             method: "POST",
  27.             body: JSON.stringify({
  28.                 prompt: text,
  29.                 max_tokens: 300,
  30.             }),
  31.             headers: {
  32.                 Authorization: "Bearer " + API_KEY,
  33.                 "Content-type": "application/json; charset=UTF-8"
  34.             },
  35.         };
  36.  
  37.         console.log("REQUEST:" + url);
  38.  
  39.         const response = await fetch(url, options);
  40.         const jsonData = await response.json();
  41.         const output = jsonData.choices[0].text;
  42.         return output;
  43.     }
  44. }
  45.  
  46. // If you're not building a Scratch extension, you might want to simply create an instance of the class like this:
  47. let aiBlock = new AIBlock();
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement