-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (86 loc) · 6.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const blocksIcon = "data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPScxLjEnIHhtbG5zPSdodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZycgeG1sbnM6eGxpbms9J2h0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsnIHdpZHRoPSczNS43MjcwNycgaGVpZ2h0PSczNi44OTk5OScgdmlld0JveD0nMCwwLDM1LjcyNzA3LDM2Ljg5OTk5Jz48ZGVmcz48bGluZWFyR3JhZGllbnQgeDE9JzI0MCcgeTE9JzE2MS41NScgeDI9JzI0MCcgeTI9JzE5OC40NScgZ3JhZGllbnRVbml0cz0ndXNlclNwYWNlT25Vc2UnIGlkPSdjb2xvci0xJz48c3RvcCBvZmZzZXQ9JzAnIHN0b3AtY29sb3I9JyM3ZDQ1ZmYnLz48c3RvcCBvZmZzZXQ9JzEnIHN0b3AtY29sb3I9JyNmZjhlNDUnLz48L2xpbmVhckdyYWRpZW50PjwvZGVmcz48ZyB0cmFuc2Zvcm09J3RyYW5zbGF0ZSgtMjIyLjEzNjQ3LC0xNjEuNTUwMDEpJz48ZyBkYXRhLXBhcGVyLWRhdGE9J3smcXVvdDtpc1BhaW50aW5nTGF5ZXImcXVvdDs6dHJ1ZX0nIGZpbGw9J3VybCgjY29sb3ItMSknIGZpbGwtcnVsZT0nbm9uemVybycgc3Ryb2tlPScjMDAwMDAwJyBzdHJva2Utd2lkdGg9JzAnIHN0cm9rZS1saW5lY2FwPSdidXR0JyBzdHJva2UtbGluZWpvaW49J21p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NTUwMiwtMC4xNjYzMmMtMC4zNjY0NiwxLjU1NTcyIC0wLjE1NTAzLDMuMjExMzkgMC42NjUyNiw0LjYzMTljMS43NTI3NSwzLjAzNTczIDUuNjUxNTEsNC4wODExOCA4LjY4NzI1LDIuMzI4NDNsNi42NjA5NiwtMy44NDc0NGwwLjAxNjY0LC0zLjUwNjV6Jy8+PC9nPjwvZz48L3N2Zz4=";
class GPTExtension {
getInfo() {
return {
id: 'gpt3',
name: 'OpenAI Integration',
menuIconURI: blocksIcon,
docsURI: 'https://platform.openai.com/docs/guides/completion/introduction',
color1: '#312252',
color2: '#0e0044',
color3: '#0e0044',
blocks: [
{
opcode: 'generateText',
text: '[MODEL] Role: [CHARACTER] Prompt: [PROMPT], API key [APIKEY], Max Tokens: [MAXTOKENS], Temperature (0.0-1.0): [TEMPERATURE]',
blockType: Scratch.BlockType.REPORTER,
arguments: {
CHARACTER: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Respond as a happy AI bot to',
},
MODEL: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'text-davinci-003',
menu: 'MODEL_MENU'
},
PROMPT: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hello'
},
APIKEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'sk-AbCdEf.....'
},
MAXTOKENS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 50
},
TEMPERATURE: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 0.7
}
}
}
],
menus: {
MODEL_MENU: {
acceptReporters: true,
items:['text-davinci-003', 'text-davinci-002', 'text-curie-001', 'text-babbage-001', 'text-ada-001']
}
}
};
}
async generateText(args) {
const model = args.MODEL;
const prompt = args.CHARACTER + `{${args.CHARACTER}${args.PROMPT}}`;
const apiKey = args.APIKEY;
const maxTokens = args.MAXTOKENS;
const temperature = args.TEMPERATURE;
try {
const response = await fetch(`https://api.openai.com/v1/engines/${model}/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
prompt: prompt,
max_tokens: maxTokens,
temperature: temperature
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to generate text from GPT-3: ${response.status} ${response.statusText}\n${errorText}`);
}
const result = await response.json();
const text = result.choices[0].text.trim();
return text;
} catch (error) {
console.error(error);
return 'Oops, an error occured. Check your key or parameters!';
}
}
}
Scratch.extensions.register(new GPTExtension());