-
Notifications
You must be signed in to change notification settings - Fork 0
/
PromptManager.js
141 lines (139 loc) · 4.83 KB
/
PromptManager.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
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
import Interaction from './Interaction.js'
import DalleInteraction from './DalleInteraction.js'
import TimestampInteraction from './TimestampInteraction.js'
export class PromptManager {
constructor() {
this.interactions = [];
}
getCount() {
// return the number of interactions.
return this.interactions.length;
}
getType(i) {
// check invalid input
if (this.interactions.length === 0) {
console.log(`Oops! We don't have any interaction now.`);
return;
}
if (i < 0 || i >= this.interactions.length) {
console.log(`Oops! This interactions id is invalid! You must enter an integer in [0, ${this.interactions.length - 1}].`);
return;
}
return this.interactions[i].getType();
}
isDalle(i) {
// check invalid input
if (this.interactions.length === 0) {
console.log(`Oops! We don't have any interaction now.`);
return;
}
if (i < 0 || i >= this.interactions.length) {
console.log(`Oops! This interactions id is invalid! You must enter an integer in [0, ${this.interactions.length - 1}].`);
return;
}
return this.interactions[i].isDalle();
}
show(i) {
// check invalid input
if (this.interactions.length === 0) {
console.log(`Oops! We don't have any interaction now.`);
return;
}
if (i < 0 || i >= this.interactions.length) {
console.log(`Oops! This interactions id is invalid! You must enter an integer in [0, ${this.interactions.length - 1}].`);
return;
}
this.interactions[i].show();
}
showAll() {
console.log(`Now we have ${this.interactions.length} interaction(s).`);
for (let interaction of this.interactions) {
interaction.show();
}
}
showAllDefault() {
const defaults = this.interactions.filter(interaction => interaction.getType() === 'Default');
console.log(`Now we have ${defaults.length} default interaction(s)`);
for (let interaction of defaults) {
interaction.show();
}
}
showAllDalle() {
const dalles = this.interactions.filter(interaction => interaction.getType() === 'Dalle');
console.log(`Now we have ${dalles.length} dalle interaction(s)`);
for (let interaction of dalles) {
interaction.show();
}
}
showAllTimestamp() {
const timestamps = this.interactions.filter(interaction => interaction.getType() === 'Timestamp');
console.log(`Now we have ${timestamps.length} timestamp interaction(s)`);
for (let interaction of timestamps) {
interaction.show();
}
}
remove(i) {
// check invalid input
if (this.interactions.length === 0) {
console.log(`Oops! We don't have any interaction now.`);
return;
}
if (i < 0 || i >= this.interactions.length) {
console.log(`Oops! This interactions id is invalid! You must enter an integer in [0, ${this.interactions.length - 1}].`);
return;
}
// delete the i-th interaction
this.interactions.splice(i, 1);
// reset the id of interactions following the removed one.
while (i < this.interactions.length) {
this.interactions[i].setId(i);
i += 1;
}
console.log(`Removed interaction ${i}!`);
}
removeAll() {
this.interactions = [];
console.log(`Removed all!`);
}
setFeedback(i, x) {
// check invalid input
if (this.interactions.length === 0) {
console.log(`Oops! We don't have any interaction now.`);
return;
}
if (i < 0 || i >= this.interactions.length) {
console.log(`Oops! This interactions id is invalid! You must enter an integer in [0, ${this.interactions.length - 1}].`);
return;
}
if (x < -1 || x > 1) {
console.log(`Oops! You have to enter an integer in [-1, 0, 1] (1 for good, -1 for bad, 0 for null or normal) to set the feedback`);
return;
}
this.interactions[i].setFeedback(x);
}
addInteraction({prompt, response, feedback}) {
if (prompt === '' || response === '') {
console.log(`The prompt and response shouldn't be empty`);
return;
}
const p = new Interaction({id: this.interactions.length, prompt, response, feedback});
this.interactions.push(p);
}
addDalleInteraction({prompt, promptImgUrl, response, responseImgUrl, feedback}) {
if (prompt === '' || response === '') {
console.log(`The prompt and response shouldn't be empty`);
return;
}
const p = new DalleInteraction({id: this.interactions.length, prompt, promptImgUrl, response, responseImgUrl, feedback});
this.interactions.push(p);
}
addTimestampInteraction({prompt, response, timestamp = Date.now(), feedback}) {
if (prompt === '' || response === '') {
console.log(`The prompt and response shouldn't be empty`);
return;
}
const p = new TimestampInteraction({id: this.interactions.length, prompt, response, timestamp ,feedback});
this.interactions.push(p);
}
}
export default PromptManager;