-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib.js
executable file
·324 lines (305 loc) · 9.96 KB
/
lib.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import "colors"
import { MODELS } from "./constants.js"
import fs from "fs"
import dotenv from "dotenv"
import child_process from "child_process"
import { escapeShell, concatPath } from "./utils.js"
import { useOpenai, useOpenaiChat } from "./apis/openai/api.js"
import { useBing } from "./apis/bing/api.js"
import { useHuggingface } from "./apis/huggingface/api.js"
import { $ } from "zx"
$.verbose = false
dotenv.config()
// directory of this file
let __dirname = new URL(".", import.meta.url).pathname
// if windows
const isWindows = process.platform === "win32"
if (isWindows) {
// /C:/ -> /c/
__dirname = __dirname.replace(/^\/([A-Z]):/, (match, p1) => {
return "/" + p1.toLowerCase()
})
}
export async function useLlm(args) {
if (args.file === true) {
args.prompt = fs.readFileSync(args.prompt, "utf8")
} else if (typeof args.file === "string") {
args.prompt = fs.readFileSync(args.file, "utf8")
}
args.temperature = args.temperature ?? 0
args.system = args.system ?? ""
args.model = args.model ?? "gpt-3.5-turbo-0613"
args.backoff = args.backoff ?? 2000
let modelDescriptor
if (MODELS[args.model]) {
modelDescriptor = MODELS[args.model]
} else if (args.model.startsWith("gpt-")) {
modelDescriptor = {
kind: "openai-chat",
}
} else {
try {
child_process.execSync(
`python ${concatPath(
__dirname,
"apis/huggingface/check.py"
)} "${escapeShell(args.model)}"`,
{
stdio: "inherit",
cwd: __dirname,
}
)
if (!args.quiet)
console.log(`Using ${args.model.yellow} from huggingface.`)
modelDescriptor = {
kind: "huggingface",
}
} catch (e) {
if (!args.quiet) console.log(`Model ${args.model} not found`)
console.log(e)
process.exit(1)
}
}
const getCompletion = async (args) => {
let completion
let print = (data) => process.stdout.write(data)
if (args.silent === true) print = () => {}
try {
switch (modelDescriptor.kind) {
case "huggingface":
completion = await useHuggingface({ print, args })
break
case "openai":
completion = await useOpenai({ print, args })
break
case "openai-chat":
completion = await useOpenaiChat({ print, args })
break
case "bing":
case "bing-creative":
case "bing-balanced":
case "bing-precise":
completion = await useBing({ print, args })
break
case "wlm":
case "wizardlm-7b-uncensored":
if (!args.modelWasSet) {
args.model = "WizardLM-7B-Uncensored/ggml-model-q4_0.gguf"
args.modelContextSize = 4096
args.modelWasSet = true
}
case "wlm13":
case "wizardlm-13b":
if (!args.modelWasSet) {
args.model =
"WizardLM-1.0-Uncensored-Llama2-13b/ggml-model-q4_0.gguf"
args.modelContextSize = 2048
args.modelWasSet = true
}
case "__wizardlm-anymodel__":
const randInt = Math.floor(Math.random() * 1000000)
const promptPath = `/tmp/llm-prompt.tmp.${randInt}`
await fs.promises.writeFile(promptPath, args.prompt)
const basePath = "/Users/snwfdhmp/Dev/workspaces/ai"
completion =
await $`${basePath}/llama.cpp-custom/main -f "${promptPath}" -m ${basePath}/models/${args.model} -n -2 -c ${args.modelContextSize} -ngl 1 2>/dev/null`
completion = completion.stdout
.slice(1 + args.prompt.length)
.trimStart()
print(completion)
await fs.promises.unlink(promptPath)
break
default:
console.log(`model ${args.model} is known but not supported yet`)
process.exit(1)
break
}
} catch (e) {
// handle network errors with backoff
const errorHandlers = [
{ code: 429, message: "too many requests" },
{ code: 503, message: "service unavailable" },
{ code: 502, message: "bad gateway" },
]
for (const errorHandler of errorHandlers) {
if (!e.message.includes(`${errorHandler.code}`)) continue
if (!args.quiet)
console.log(
`getCompletion: ${errorHandler.message} (${errorHandler.code}), waiting ${args.backoff}ms`
)
await new Promise((resolve) => setTimeout(resolve, args.backoff))
return await getCompletion({ ...args, backoff: args.backoff * 2 })
}
// default error handler
console.error(`Error: ${e.message}`)
console.log(e)
return
}
return completion
}
if (args.interpret) {
let vars = args.vars || {}
const processFile = async (prompt) => {
const runInstructions = prompt.match(/<\|@(run.*)\|>/g) // these regex should be identical except the ()
const parts = prompt.split(/<\|@run.*\|>/g) // these regex should be identical except the ()
parts.pop()
let total = ""
for (let i = 0; i < parts.length; i++) {
// detect <|$var|> pattern
const matches = parts[i].match(/<\|\$.*?\|>/g)
const unique = [...new Set(matches)]
for (let j = 0; j < unique.length; j++) {
// replace var with value
const varName = unique[j].slice(3, -2)
const varValue = vars[varName]
if (varValue === undefined) {
console.log(`Variable ${varName} not found`)
process.exit(1)
}
parts[i] = parts[i].replace(unique[j], varValue)
}
const prompt = total + parts[i]
// console.log(`Running '''${prompt}'''`)
const completion = await getCompletion({
...args,
prompt,
})
total = prompt + completion
const runInstruction = runInstructions[i].slice(3).slice(0, -2)
if (runInstruction === "run") continue
if (runInstruction.split(" ").includes("replace")) total = completion
}
return total
}
return await processFile(args.prompt)
} else if (args.chain) {
const processFile = async (file, silent) => {
// detect <|@var|> pattern
const matches = file.match(/<\|@.*?\|>/g)
const unique = [...new Set(matches)]
for (let i = 0; i < unique.length; i++) {
// process subfile
const output = await processFile(
fs.readFileSync(`./${unique[i].slice(3, -2)}.txt`, "utf-8"),
true
)
file = file.replace(unique[i], output)
}
return await getCompletion({
...args,
prompt: file,
silent: silent && args.quiet,
})
}
return await processFile(args.prompt, false)
} else if (args.plugins) {
const curlPath = child_process
.execSync("which curl", {
encoding: "utf8",
})
.trim()
if (!curlPath) {
console.error(
"curl is not available. Please install curl to use plugins."
)
process.exit(1)
}
if (!args.quiet) console.log("Plugins enabled")
const compileAndRun = async (path, variables) => {
const file = fs.readFileSync(path, "utf8")
const regex = /<\|\$(.*)\|>/g
const compiled = file.replace(regex, (_, variable) => {
if (variables[variable]) {
return variables[variable]
} else {
throw new Error(`Variable ${variable} not found`)
}
})
return await getCompletion({
...args,
prompt: compiled,
silent: !args.verbose,
})
}
const prompt = args.prompt
const step1Output = await compileAndRun(
concatPath(__dirname, "/plugins/plugin_prompt--step1.txt"),
{
prompt,
}
)
const regexpOutput = /<\|output\.start\|>([\s\S]*)<\|output\.end\|>/
if (!step1Output.match(regexpOutput)) {
console.log(step1Output)
process.exit(0)
}
const [_, output] = regexpOutput.exec(step1Output)
const regexpPlugin =
/<\|plugins\["(.*)"\]\.([a-zA-Z0-9_]+)\|>(.*)<\|plugins\.end\|>/
const [__, plugin, fn, payload] = regexpPlugin.exec(output)
console.log(`${`Using plugin: ${plugin}.${fn}`.blue} ${payload}`)
const regexpCurl = /<\|curl\.start\|>(.*)<\|curl\.end\|>/
let [___, curlCommand] = regexpCurl.exec(output)
if (
!curlCommand.trim().startsWith("curl") ||
!curlCommand.trim().split("\n").length > 1
) {
console.log("panic: does not look like a curl command")
console.log("\n\n\t" + curlCommand.trim() + "\n\n")
process.exit(1)
}
let result
try {
curlCommand =
curlCommand.trim().replace(/^curl/, `"${curlPath}"`) +
" -s -H 'WebPilot-Friend-UID: snwfdhmp'"
// put url at the end
const url = curlCommand.match(/"https?:\/\/[^\s]+"/g)
if (!url) {
console.log("panic: does not look like a curl command")
console.log("\n\n\t" + curlCommand.trim() + "\n\n")
process.exit(1)
}
curlCommand = curlCommand.replace(url[0], "")
curlCommand = curlCommand + " " + url[0]
result = child_process.execSync(curlCommand).toString()
} catch (e) {
console.log("Error while executing curl command")
console.log(e)
if (e.stdout) {
console.log(e.stdout.toString())
}
if (e.stderr) {
console.log(e.stderr.toString())
}
process.exit(1)
}
// curl to axios
const finalOutput = await compileAndRun(
concatPath(__dirname, "/plugins/plugin_prompt--step2.txt"),
{
pluginsOutput: JSON.stringify({
plugin,
fn,
payload,
output: result,
}),
prompt,
}
)
const userOutput = regexpOutput.exec(finalOutput)
if (!args.verbose) {
if (!userOutput) {
console.log(finalOutput.trim())
} else {
console.log(userOutput[1].trim())
}
}
return userOutput
} else {
return await getCompletion(args)
}
}
export const llm = async (prompt, options) => {
return await useLlm({ prompt, quiet: true, silent: true, ...options })
}