-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting.js
More file actions
142 lines (126 loc) · 4.81 KB
/
testing.js
File metadata and controls
142 lines (126 loc) · 4.81 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const GEMINI_KEY = "your-gemini-key"; // ⚠️ Replace
const OPENAI_KEY = "your-openai-key"; // ⚠️ Replace
class AIBlock {
constructor() {
this.conversationHistory = [];
this.currentModel = "gemini-1.5-flash"; // Default model
}
getInfo() {
return {
id: "AI",
name: "AI",
color1: "#800080", // Set block color to purple
blocks: [
{
opcode: "getText",
blockType: "reporter",
text: "get text [PROMPT] systemPrompt [SYSTEM_PROMPT] model [MODEL]",
arguments: {
PROMPT: {
type: "string",
defaultValue: "Ask me anything"
},
SYSTEM_PROMPT: {
type: "string",
defaultValue: "You are a highly intelligent and helpful assistant. Your goal is to provide accurate and informative responses to user queries. Always be polite, concise, and clear in your explanations."
},
MODEL: {
type: "string",
defaultValue: "gemini-1.5-flash"
}
}
},
{
opcode: "model",
blockType: "reporter",
text: "model",
arguments: {}
},
{
opcode: "resetMemory",
blockType: "command",
text: "reset memory"
}
],
menus: {
providers: {
items: ["gemini", "openai"]
},
models: {
items: [
"gemini-1.5-flash",
"gemini-1.5-pro",
"gpt-3.5-turbo",
"gpt-4"
]
}
}
};
}
async getText({ PROMPT, SYSTEM_PROMPT, MODEL }) {
const userPrompt = PROMPT.trim();
const systemPrompt = SYSTEM_PROMPT.trim();
const model = MODEL.trim();
// Add to history
this.conversationHistory.push({ role: "user", content: userPrompt });
try {
let response;
if (model.startsWith("gemini")) {
response = await this.handleGemini(model, systemPrompt);
} else if (model.startsWith("gpt")) {
response = await this.handleOpenAI(model, systemPrompt);
} else {
throw new Error("Invalid model");
}
// Store AI response
this.conversationHistory.push({ role: "assistant", content: response });
return response;
} catch (error) {
console.error("Error:", error);
return "API Error";
}
}
async handleGemini(model, systemPrompt) {
const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
system_instruction: { parts: [{ text: systemPrompt }] },
contents: this.conversationHistory.map(msg => ({
role: msg.role === "user" ? "user" : "model",
parts: [{ text: msg.content }]
}))
})
});
const data = await response.json();
return data.candidates?.[0]?.content?.parts?.[0]?.text || "No response";
}
async handleOpenAI(model, systemPrompt) {
const url = "https://api.openai.com/v1/chat/completions";
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer YOUR_OPENAI_KEY` // Replace with your OpenAI API key
},
body: JSON.stringify({
model: model,
messages: [
{ role: "system", content: systemPrompt },
...this.conversationHistory
]
})
});
const data = await response.json();
return data.choices?.[0]?.message?.content || "No response";
}
model() {
return this.currentModel; // Return the current model
}
resetMemory() {
this.conversationHistory = [];
}
}
// Register the extension with Scratch
Scratch.extensions.register(new AIBlock());