0% found this document useful (0 votes)
60 views2 pages

AI-Powered Text Completion Block

The document defines a class called AIBlock that integrates with Scratch to provide a block for completing prompts using OpenAI's Davinci3 model. It includes a method to send a prompt to the API and retrieve a text completion response. The class is registered as an extension in Scratch for use in projects.

Uploaded by

grant.watt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views2 pages

AI-Powered Text Completion Block

The document defines a class called AIBlock that integrates with Scratch to provide a block for completing prompts using OpenAI's Davinci3 model. It includes a method to send a prompt to the API and retrieve a text completion response. The class is registered as an extension in Scratch for use in projects.

Uploaded by

grant.watt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class AIBlock {

getInfo() {
//Metadata for block
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"
}
}
}],
//don't worry about it
"menus": {}
};
}

async completePrompt({ string }) {


//Remove trailing spaces, required for model to work properly
const text = [Link]();
//Request text completion using Davinci3
const url = `[Link]

const options = {
//Has to be post for some reason
method: "POST",
//Input prompt and a decent length
body: [Link]({
prompt: text,
max_tokens: 300,
}),
//API key, and JSON content type
headers: {
Authorization: "Bearer " + API_KEY,
"Content-type": "application/json; charset=UTF-8"
},
};

[Link]("REQUEST:" + url);

//Fetch and await promise.


const response = await fetch(url, options);
//Get JSON data
const jsonData = await [Link]();

//The ai response will be the first (and only) choices text


const output = [Link][0].text;
return output;
}

//Register block with Scratch


[Link](new AIBlock());a

You might also like