-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.js
214 lines (203 loc) · 6.25 KB
/
generate.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const fetch = require("node-fetch");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const db = require("@saltcorn/data/db");
const { features, getState } = require("@saltcorn/data/db/state");
let ollamaMod;
if (features.esm_plugins) ollamaMod = require("ollama");
const getEmbedding = async (config, opts) => {
switch (config.backend) {
case "OpenAI":
return await getEmbeddingOpenAICompatible(
{
embeddingsEndpoint: "https://api.openai.com/v1/embeddings",
bearer: opts?.api_key || config.api_key,
embed_model: opts?.model || config.embed_model,
},
opts
);
case "OpenAI-compatible API":
return await getEmbeddingOpenAICompatible(
{
embeddingsEndpoint: opts?.endpoint || config.embed_endpoint,
bearer: opts?.bearer || opts?.api_key || config.api_key,
apiKey: opts?.api_key || config.api_key,
embed_model:
opts?.embed_model ||
opts?.model ||
config.embed_model ||
config.model,
},
opts
);
case "Local Ollama":
if (config.embed_endpoint) {
return await getEmbeddingOpenAICompatible(
{
embeddingsEndpoint: config.embed_endpoint,
embed_model:
opts?.embed_model ||
opts?.model ||
config.embed_model ||
config.model,
},
opts
);
} else {
if (!ollamaMod) throw new Error("Not implemented for this backend");
const { Ollama } = ollamaMod;
const ollama = new Ollama();
const olres = await ollama.embeddings({
model: opts?.model || config.embed_model || config.model,
prompt: opts.prompt,
});
//console.log("embedding response ", olres);
return olres.embedding;
}
default:
throw new Error("Not implemented for this backend");
}
};
const getCompletion = async (config, opts) => {
switch (config.backend) {
case "OpenAI":
return await getCompletionOpenAICompatible(
{
chatCompleteEndpoint: "https://api.openai.com/v1/chat/completions",
bearer: opts?.api_key || opts?.bearer || config.api_key,
model: opts?.model || config.model,
},
opts
);
case "OpenAI-compatible API":
return await getCompletionOpenAICompatible(
{
chatCompleteEndpoint: opts?.endpoint || config.endpoint,
bearer:
opts?.bearer ||
opts?.api_key ||
config.bearer_auth ||
config.bearer,
apiKey: opts?.api_key || config.api_key,
model: opts?.model || config.model,
},
opts
);
case "Local Ollama":
if (!ollamaMod) throw new Error("Not implemented for this backend");
const { Ollama } = ollamaMod;
const ollama = new Ollama();
const olres = await ollama.generate({
model: opts?.model || config.model,
...opts,
});
//console.log("the response ", olres);
return olres.response;
case "Local llama.cpp":
//TODO only check if unsafe plugins not allowed
const isRoot = db.getTenantSchema() === db.connectObj.default_schema;
if (!isRoot)
throw new Error(
"llama.cpp inference is not permitted on subdomain tenants"
);
let hyperStr = "";
if (opts.temperature) hyperStr += ` --temp ${opts.temperature}`;
let nstr = "";
if (opts.ntokens) nstr = `-n ${opts.ntokens}`;
//console.log("running llama with prompt: ", opts.prompt);
const { stdout, stderr } = await exec(
`./main -m ${config.model_path} -p "${opts.prompt}" ${nstr}${hyperStr}`,
{ cwd: config.llama_dir }
);
return stdout;
default:
break;
}
};
const getCompletionOpenAICompatible = async (
{ chatCompleteEndpoint, bearer, apiKey, model },
{
systemPrompt,
prompt,
temperature,
debugResult,
chat = [],
api_key,
endpoint,
...rest
}
) => {
const headers = {
"Content-Type": "application/json",
Accept: "application/json",
};
if (bearer) headers.Authorization = "Bearer " + bearer;
if (apiKey) headers["api-key"] = apiKey;
const body = {
//prompt: "How are you?",
model: rest.model || model,
messages: [
{
role: "system",
content: systemPrompt || "You are a helpful assistant.",
},
...chat,
{ role: "user", content: prompt },
],
temperature: temperature || 0.7,
...rest,
};
if (debugResult)
console.log(
"OpenAI request",
JSON.stringify(body, null, 2),
"to",
chatCompleteEndpoint,
"headers",
JSON.stringify(headers)
);
const rawResponse = await fetch(chatCompleteEndpoint, {
method: "POST",
headers,
body: JSON.stringify(body),
});
const results = await rawResponse.json();
if (debugResult)
console.log("OpenAI response", JSON.stringify(results, null, 2));
if (results.error) throw new Error(`OpenAI error: ${results.error.message}`);
return (
results?.choices?.[0]?.message?.content ||
(results?.choices?.[0]?.message?.tool_calls
? { tool_calls: results?.choices?.[0]?.message?.tool_calls }
: null)
);
};
const getEmbeddingOpenAICompatible = async (
config,
{ prompt, model, debugResult }
) => {
const { embeddingsEndpoint, bearer, apiKey, embed_model } = config;
const headers = {
"Content-Type": "application/json",
Accept: "application/json",
};
if (bearer) headers.Authorization = "Bearer " + bearer;
if (apiKey) headers["api-key"] = apiKey;
const body = {
//prompt: "How are you?",
model: model || embed_model || "text-embedding-3-small",
input: prompt,
};
const rawResponse = await fetch(embeddingsEndpoint, {
method: "POST",
headers,
body: JSON.stringify(body),
});
const results = await rawResponse.json();
if (debugResult)
console.log("OpenAI response", JSON.stringify(results, null, 2));
if (results.error) throw new Error(`OpenAI error: ${results.error.message}`);
if (Array.isArray(prompt)) return results?.data?.map?.((d) => d?.embedding);
return results?.data?.[0]?.embedding;
};
module.exports = { getCompletion, getEmbedding };