filmov
tv
I Coded Working AI in Scratch!

Показать описание
Not just a bunch of ifs and say blocks - this is real AI.
----
----
I can't publish this extension because it requires an API key. Here are the resources you need to create it yourself. The OpenAI API, ironically, requires real money to use.
JavaScript Code (You need to provide an API key):
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
//Request text completion using Davinci3
const options = {
//Has to be post for some reason
method: "POST",
//Input prompt and a decent length
body: JSON.stringify({
prompt: text,
max_tokens: 300,
}),
//API key, and JSON content type
headers: {
Authorization: "Bearer " + API_KEY,
"Content-type": "application/json; charset=UTF-8"
},
};
//Fetch and await promise.
const response = await fetch(url, options);
//Get JSON data
//The ai response will be the first (and only) choices text
return output;
}
}
//Register block with Scratch
---
Some images generated by DALL-E
Licensed under Creative Commons: By Attribution 4.0 License
Subscribe!
Oh, and if you want to support me financially, consider becoming a member :)
----
----
I can't publish this extension because it requires an API key. Here are the resources you need to create it yourself. The OpenAI API, ironically, requires real money to use.
JavaScript Code (You need to provide an API key):
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
//Request text completion using Davinci3
const options = {
//Has to be post for some reason
method: "POST",
//Input prompt and a decent length
body: JSON.stringify({
prompt: text,
max_tokens: 300,
}),
//API key, and JSON content type
headers: {
Authorization: "Bearer " + API_KEY,
"Content-type": "application/json; charset=UTF-8"
},
};
//Fetch and await promise.
const response = await fetch(url, options);
//Get JSON data
//The ai response will be the first (and only) choices text
return output;
}
}
//Register block with Scratch
---
Some images generated by DALL-E
Licensed under Creative Commons: By Attribution 4.0 License
Subscribe!
Oh, and if you want to support me financially, consider becoming a member :)
Комментарии