-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathaimodel.js
More file actions
143 lines (118 loc) · 2.77 KB
/
aimodel.js
File metadata and controls
143 lines (118 loc) · 2.77 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
143
// AI
// The MIT License
// Copyright 2026 (c) Peter Širka <petersirka@gmail.com>
function AI(model) {
const t = this;
t.options = {};
t.options.callback = NOOP;
t.config = {};
t.payload = {};
t.payload.model = model;
t.payload.messages = [];
}
// Enables think mode
AI.prototype.think = function () {
const t = this;
t.payload.think = true;
return t;
};
// Internal function
// Appends content message
AI.prototype.message = function(role, content, merge) {
const t = this;
if (merge) {
for (let m of t.payload.messages) {
if (m.role === role) {
if (typeof(content) === 'object') {
for (let key in content)
m[key] = content[key];
} else
m.content += merge + content;
return this;
}
}
}
const msg = { role: role };
if (typeof(content) === 'object') {
for (let key in content)
msg[key] = content[key];
} else
msg.content = content;
t.payload.messages.push(msg);
return t;
};
AI.prototype.configure = function(opt) {
const t = this;
for (let key in opt)
t.config[key] = opt[key];
return t;
};
AI.prototype.system = function(content, merge) {
return this.message('system', content, merge);
};
AI.prototype.user = function(content, merge) {
return this.message('user', content, merge);
};
AI.prototype.prompt = function(content) {
this.payload.prompt = content;
return this;
};
AI.prototype.assistant = function(content, merge) {
return this.message('assistant', content, merge);
};
AI.prototype.promise = function($) {
const t = this;
return new Promise(function(resolve, reject) {
t.callback(function(err, response) {
if (err) {
if ($ && $.invalid)
$.invalid(err);
else
reject(F.TUtils.toError(err));
} else
resolve(response);
});
});
};
AI.prototype.stream = function(fn) {
const t = this;
t.options.stream = fn;
t.options.$running && clearImmediate(t.options.$running);
t.options.$running = setImmediate(() => t.run());
return t;
};
AI.prototype.callback = function(fn) {
const t = this;
t.options.callback = fn;
t.options.$running && clearImmediate(t.options.$running);
t.options.$running = setImmediate(() => t.run());
return t;
};
// Internal function
AI.prototype.run = function() {
const t = this;
let ai = F.aimodels[t.payload.model];
if (ai) {
if (ai.config) {
for (let key in ai.config) {
if (t.config[key] === undefined)
t.config[key] = ai.config[key];
}
}
ai.callback(t, t.options.callback);
} else
t.options.callback('AI model not found.');
return t;
};
exports.exec = function(model) {
return new AI(model);
};
exports.newai = function(model, config, callback) {
if (typeof(config) === 'function') {
callback = config;
config = null;
}
const models = model.split(/,/).trim();
for (const m of models)
F.aimodels[m] = { config, callback };
};