@saltcorn/large-language-model 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Saltcorn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # large-language-model
2
+ LLM models and functionality for Saltcorn
package/generate.js ADDED
@@ -0,0 +1,77 @@
1
+ const axios = require("axios");
2
+ const util = require("util");
3
+ const exec = util.promisify(require("child_process").exec);
4
+ const db = require("@saltcorn/data/db");
5
+
6
+ const getCompletion = async (config, opts) => {
7
+ switch (config.backend) {
8
+ case "OpenAI":
9
+ return await getCompletionOpenAICompatible(
10
+ {
11
+ chatCompleteEndpoint: "https://api.openai.com/v1/chat/completions",
12
+ bearer: config.api_key,
13
+ model: config.model,
14
+ },
15
+ opts
16
+ );
17
+ case "OpenAI-compatible API":
18
+ return await getCompletionOpenAICompatible(
19
+ {
20
+ chatCompleteEndpoint: config.endpoint,
21
+ bearer: config.bearer,
22
+ model: config.model,
23
+ },
24
+
25
+ opts
26
+ );
27
+ case "Local llama.cpp":
28
+ const isRoot = db.getTenantSchema() === db.connectObj.default_schema;
29
+ if (!isRoot)
30
+ throw new Error(
31
+ "llama.cpp inference is not permitted on subdomain tenants"
32
+ );
33
+ let hyperStr = "";
34
+ if (opts.temperature) hyperStr += ` --temp ${opts.temperature}`;
35
+ let nstr = "";
36
+ if (opts.ntokens) nstr = `-n ${opts.ntokens}`;
37
+ console.log("running llama with prompt: ", opts.prompt);
38
+
39
+ const { stdout, stderr } = await exec(
40
+ `./main -m ${config.model_path} -p "${opts.prompt}" ${nstr}${hyperStr}`,
41
+ { cwd: config.llama_dir }
42
+ );
43
+ return stdout;
44
+ default:
45
+ break;
46
+ }
47
+ };
48
+
49
+ const getCompletionOpenAICompatible = async (
50
+ { chatCompleteEndpoint, bearer, model },
51
+ { systemPrompt, prompt, temperature }
52
+ ) => {
53
+ const headers = {
54
+ "Content-Type": "application/json",
55
+ };
56
+ if (bearer) headers.Authorization = "Bearer " + bearer;
57
+ const client = axios.create({
58
+ headers,
59
+ });
60
+ const params = {
61
+ //prompt: "How are you?",
62
+ model,
63
+ messages: [
64
+ {
65
+ role: "system",
66
+ content: systemPrompt || "You are a helpful assistant.",
67
+ },
68
+ { role: "user", content: prompt },
69
+ ],
70
+ temperature: temperature || 0.7,
71
+ };
72
+
73
+ const results = await client.post(chatCompleteEndpoint, params);
74
+ return results?.data?.choices?.[0]?.message?.content;
75
+ };
76
+
77
+ module.exports = { getCompletion };
package/index.js ADDED
@@ -0,0 +1,105 @@
1
+ const Workflow = require("@saltcorn/data/models/workflow");
2
+ const Form = require("@saltcorn/data/models/form");
3
+ const { getCompletion } = require("./generate");
4
+ const db = require("@saltcorn/data/db");
5
+
6
+ const configuration_workflow = () =>
7
+ new Workflow({
8
+ steps: [
9
+ {
10
+ name: "API key",
11
+ form: async (context) => {
12
+ const isRoot = db.getTenantSchema() === db.connectObj.default_schema;
13
+ return new Form({
14
+ fields: [
15
+ {
16
+ name: "backend",
17
+ label: "Inference backend",
18
+ type: "String",
19
+ required: true,
20
+ attributes: {
21
+ options: [
22
+ "OpenAI",
23
+ "OpenAI-compatible API",
24
+ ...(isRoot ? ["Local llama.cpp"] : []),
25
+ ],
26
+ },
27
+ },
28
+ {
29
+ name: "api_key",
30
+ label: "API key",
31
+ sublabel: "From your OpenAI account",
32
+ type: "String",
33
+ required: true,
34
+ showIf: { backend: "OpenAI" },
35
+ },
36
+ {
37
+ name: "llama_dir",
38
+ label: "llama.cpp directory",
39
+ type: "String",
40
+ required: true,
41
+ showIf: { backend: "Local llama.cpp" },
42
+ },
43
+ {
44
+ name: "model_path",
45
+ label: "Model path",
46
+ type: "String",
47
+ required: true,
48
+ showIf: { backend: "Local llama.cpp" },
49
+ },
50
+ {
51
+ name: "model",
52
+ label: "Model", //gpt-3.5-turbo
53
+ type: "String",
54
+ required: true,
55
+ showIf: { backend: "OpenAI" },
56
+ attributes: {
57
+ options: [
58
+ "gpt-3.5-turbo",
59
+ "gpt-3.5-turbo-16k",
60
+ "gpt-4",
61
+ "gpt-4-32k",
62
+ ],
63
+ },
64
+ },
65
+ {
66
+ name: "bearer_auth",
67
+ label: "Bearer Auth",
68
+ sublabel: "HTTP Header authorization with bearer token",
69
+ type: "String",
70
+ showIf: { backend: "OpenAI-compatible API" },
71
+ },
72
+ {
73
+ name: "model",
74
+ label: "Model",
75
+ type: "String",
76
+ showIf: { backend: "OpenAI-compatible API" },
77
+ },
78
+ {
79
+ name: "endpoint",
80
+ label: "Chat completions endpoint",
81
+ type: "String",
82
+ sublabel: "Example: http://localhost:8080/v1/chat/completions",
83
+ showIf: { backend: "OpenAI-compatible API" },
84
+ },
85
+ ],
86
+ });
87
+ },
88
+ },
89
+ ],
90
+ });
91
+ const functions = (config) => ({
92
+ llm_generate: {
93
+ run: async (prompt, opts) => {
94
+ return await getCompletion(config, { prompt, ...opts });
95
+ },
96
+ isAsync: true,
97
+ description: "Generate text with GPT",
98
+ arguments: [{ name: "prompt", type: "String" }],
99
+ },
100
+ });
101
+ module.exports = {
102
+ sc_plugin_api_version: 1,
103
+ configuration_workflow,
104
+ functions,
105
+ };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@saltcorn/large-language-model",
3
+ "version": "0.1.0",
4
+ "description": "Large language models and functionality for Saltcorn",
5
+ "main": "index.js",
6
+ "dependencies": {
7
+ "@saltcorn/data": "^0.9.0",
8
+ "axios": "0.16.2",
9
+ "underscore": "1.13.6"
10
+ },
11
+ "author": "Tom Nielsen",
12
+ "license": "MIT",
13
+ "repository": "github:saltcorn/large-language-model",
14
+ "eslintConfig": {
15
+ "extends": "eslint:recommended",
16
+ "parserOptions": {
17
+ "ecmaVersion": 2020
18
+ },
19
+ "env": {
20
+ "node": true,
21
+ "es6": true
22
+ },
23
+ "rules": {
24
+ "no-unused-vars": "off",
25
+ "no-case-declarations": "off",
26
+ "no-empty": "warn",
27
+ "no-fallthrough": "warn"
28
+ }
29
+ }
30
+ }