-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.js
147 lines (138 loc) · 4.6 KB
/
model.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
142
143
144
145
146
147
const { div } = require("@saltcorn/markup/tags");
const Workflow = require("@saltcorn/data/models/workflow");
const Table = require("@saltcorn/data/models/table");
const Form = require("@saltcorn/data/models/form");
const util = require("util");
const path = require("path");
const os = require("os");
const fs = require("fs");
const _ = require("underscore");
const { getCompletion } = require("./generate");
const { OPENAI_MODELS, OLLAMA_MODELS_PATH } = require("./constants");
const configuration_workflow = (config) => (req) =>
new Workflow({
steps: [
{
name: "Predictors",
form: async (context) => {
const table = await Table.findOne(
context.table_id
? { id: context.table_id }
: { name: context.exttable_name }
);
//console.log(context);
const int_field_options = table.fields.filter(
(f) => f.type?.name === "Integer"
);
let models = [];
if (config.backend === "Local llama.cpp") {
models = fs.readdirSync(path.join(config.llama_dir, "models"));
} else if (config.backend === "OpenAI") {
models = OPENAI_MODELS;
} else if (config.backend === "Local Ollama") {
models = fs.readdirSync(path.join(OLLAMA_MODELS_PATH[os.type()], "manifests/registry.ollama.ai/library"));
}
return new Form({
fields: [
{
label: "Prompt template",
name: "prompt_template",
type: "String",
fieldview: "textarea",
sublabel: div(
"Use handlebars to access fields. Example: <code>My name is {{name}}. How are you?</code>. Variables in scope: " +
table.fields.map((f) => `<code>${f.name}</code>`).join(", ")
),
},
...(config.backend === "Local llama.cpp"
? [
{
label: "Num. tokens field",
name: "ntokens_field",
type: "String",
attributes: {
options: int_field_options.map((f) => f.name),
},
sublabel:
"Override number of tokens set in instance parameters with value in this field, if chosen",
},
]
: []),
{
label: "Model",
name: "model",
type: "String",
required: true,
attributes: { options: models },
},
],
});
},
},
],
});
const modelpatterns = (config) => ({
LargeLanguageModel: {
prediction_outputs: ({ configuration }) => [
{ name: "output", type: "String" },
{ name: "prompt", type: "String" },
],
configuration_workflow: configuration_workflow(config),
hyperparameter_fields: ({ table, configuration }) => [
...(config.backend === "Local llama.cpp"
? [
{
name: "ntokens",
label: "Num tokens",
type: "Integer",
attributes: { min: 1 },
required: true,
default: 128,
sublabel: "Can be overridden by number of tokens field, if set",
},
{
name: "repeat_penalty",
label: "Repeat penalty",
type: "Float",
attributes: { min: 0, decimal_places: 1 },
default: 1.1,
},
]
: []),
{
name: "temp",
label: "Temperature",
type: "Float",
attributes: { min: 0, max: 1, decimal_places: 1 },
default: 0.8,
},
],
predict: async ({
id, //instance id
model: {
configuration: { prompt_template, ntokens_field, model },
table_id,
},
hyperparameters,
fit_object,
rows,
}) => {
const results = [];
const template = _.template(prompt_template || "", {
evaluate: /\{\{#(.+?)\}\}/g,
interpolate: /\{\{([^#].+?)\}\}/g,
});
const mdlConfig = { ...config };
if (hyperparameters.temp) mdlConfig.temperature = hyperparameters.temp;
const opts = { ...hyperparameters };
if (model) opts.model = model;
for (const row of rows) {
const prompt = template(row);
const output = getCompletion(mdlConfig, { ...opts, prompt });
results.push({ output, prompt });
}
return results;
},
},
});
module.exports = modelpatterns;