Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AIBlock {
- getInfo() {
- return {
- "id": "AI",
- "name": "AI",
- "blocks": [{
- "opcode": "completePrompt",
- "blockType": "reporter",
- "text": "complete prompt [string]",
- "arguments": {
- "string": {
- "type": "string",
- "defaultValue": "Explain quantum computing in simple terms"
- }
- }
- }],
- "menus": {}
- };
- }
- async completePrompt({ string }) {
- const text = string.trim();
- const url = `https://api.openai.com/v1/engines/davinci/completions`;
- const options = {
- method: "POST",
- body: JSON.stringify({
- prompt: text,
- max_tokens: 300,
- }),
- headers: {
- Authorization: "Bearer " + API_KEY,
- "Content-type": "application/json; charset=UTF-8"
- },
- };
- console.log("REQUEST:" + url);
- const response = await fetch(url, options);
- const jsonData = await response.json();
- const output = jsonData.choices[0].text;
- return output;
- }
- }
- // If you're not building a Scratch extension, you might want to simply create an instance of the class like this:
- let aiBlock = new AIBlock();
Advertisement
Comments
-
- Thx ALL
-
- SCRATCH mod:
- https://sheeptester.github.io/scratch-gui/
-
- Free API KEY: sk-...Dcci, sk-...pqUU, sk-...M8Ff,
-
- How to add block
- https://youtu.be/hSnFf_YT5qk
Add Comment
Please, Sign In to add comment
Advertisement