-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_sim.js
More file actions
36 lines (32 loc) · 1.39 KB
/
ai_sim.js
File metadata and controls
36 lines (32 loc) · 1.39 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
// ai_sim.js
// Simple local LLM/AI simulation for kid-friendly teaching.
export default class AISim{
constructor(){
this.examples = []; // training examples: {label, text}
this.recentTokens = []; // context window
this.maxContext = 6;
this.styleScores = {}; // counts per label
}
addExample(label, text){
this.examples.push({label, text});
this.styleScores[label] = (this.styleScores[label]||0) + 1;
}
feedTokens(tokens){
// tokens = array of strings
this.recentTokens.push(...tokens);
if(this.recentTokens.length>this.maxContext) this.recentTokens = this.recentTokens.slice(-this.maxContext);
}
// rudimentary 'response' generator: uses tokens + learned style
respond(){
// prefer label with highest score
const topStyle = Object.keys(this.styleScores).sort((a,b)=>this.styleScores[b]-this.styleScores[a])[0] || 'neutral';
const tokenText = this.recentTokens.join(' ');
// some playful heuristics for kids
if(tokenText.length===0) return `Hello! Give me some seeds to make a sentence!`;
if(topStyle === 'happy') return `I feel happy when I hear: ${tokenText} 😊`;
if(topStyle === 'sad') return `That sounds a bit sad: ${tokenText} 💧`;
if(topStyle === 'robot') return `ROBOT RESPONSE: ${tokenText.toUpperCase()}`;
// fallback fun response: mirror with small change
return `You said: ${tokenText}. I like that!`;
}
}