@agent-smith/task 0.0.2
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/dist/inferparams.d.ts +5 -0
- package/dist/inferparams.js +17 -0
- package/dist/interfaces.d.ts +86 -0
- package/dist/interfaces.js +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.js +2 -0
- package/dist/task.d.ts +12 -0
- package/dist/task.js +108 -0
- package/dist/templates.d.ts +4 -0
- package/dist/templates.js +43 -0
- package/dist/variables.d.ts +3 -0
- package/dist/variables.js +25 -0
- package/package.json +54 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { InferenceParams } from "@locallm/types";
|
|
2
|
+
import { PromptTemplate } from "modprompt/dist/cls";
|
|
3
|
+
import { TaskConf } from "./interfaces";
|
|
4
|
+
declare function formatInferParams(ip: InferenceParams, conf: TaskConf, tpl?: PromptTemplate): InferenceParams;
|
|
5
|
+
export { formatInferParams, };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function formatInferParams(ip, conf, tpl) {
|
|
2
|
+
if (!ip?.stop) {
|
|
3
|
+
ip.stop = [];
|
|
4
|
+
}
|
|
5
|
+
if (tpl?.stop) {
|
|
6
|
+
ip.stop.push(...tpl.stop);
|
|
7
|
+
}
|
|
8
|
+
const _ip = ip;
|
|
9
|
+
// override infer params
|
|
10
|
+
if (conf?.inferParams) {
|
|
11
|
+
for (const [k, v] of Object.entries(conf.inferParams)) {
|
|
12
|
+
_ip[k] = v;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return _ip;
|
|
16
|
+
}
|
|
17
|
+
export { formatInferParams, };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { InferenceParams, InferenceResult, InferenceOptions, ToolSpec, HistoryTurn, ToolCallSpec } from "@locallm/types";
|
|
2
|
+
import { PromptTemplate } from "modprompt";
|
|
3
|
+
interface ModelSpec {
|
|
4
|
+
name: string;
|
|
5
|
+
template?: string;
|
|
6
|
+
ctx?: number;
|
|
7
|
+
system?: string;
|
|
8
|
+
afterSystem?: string;
|
|
9
|
+
assistant?: string;
|
|
10
|
+
inferParams?: InferenceParams;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Represents the input configuration for a language model task.
|
|
14
|
+
*
|
|
15
|
+
* @interface TaskInput
|
|
16
|
+
* @param {string} prompt - The user's input prompt for the task.
|
|
17
|
+
*/
|
|
18
|
+
interface TaskInput {
|
|
19
|
+
prompt: string;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Configuration interface for a language model task.
|
|
24
|
+
*
|
|
25
|
+
* @interface TaskConf
|
|
26
|
+
* @param {ModelSpec} [model] - Optional model configuration.
|
|
27
|
+
* @param {ModelSpec} [modelname] - Optional model name for the task models list.
|
|
28
|
+
* @param {InferenceParams} [inferParams] - Optional inference parameters.
|
|
29
|
+
* @param {boolean} [debug] - Optional debug flag.
|
|
30
|
+
* @param {boolean} [quiet] - Optional quiet flag.
|
|
31
|
+
*/
|
|
32
|
+
interface TaskConf {
|
|
33
|
+
model?: ModelSpec;
|
|
34
|
+
modelname?: string;
|
|
35
|
+
inferParams?: InferenceParams;
|
|
36
|
+
options?: InferenceOptions;
|
|
37
|
+
debug?: boolean;
|
|
38
|
+
verbose?: boolean;
|
|
39
|
+
onToolCall?: (tc: ToolCallSpec) => void;
|
|
40
|
+
onToolCallEnd?: (tr: any) => void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Represents a template specification for a language model task.
|
|
44
|
+
*
|
|
45
|
+
* @interface TemplateSpec
|
|
46
|
+
* @param {string} [system] - The system message for the template.
|
|
47
|
+
* @param {Array<string>} [stop] - Extra stop sequences for the template.
|
|
48
|
+
* @param {string} [assistant] - The assistant start message for the template.
|
|
49
|
+
* @example
|
|
50
|
+
* const template: TemplateSpec = {
|
|
51
|
+
* system: "You are a helpful AI",
|
|
52
|
+
* stop: ["\n", "</s>"],
|
|
53
|
+
* };
|
|
54
|
+
*/
|
|
55
|
+
interface TemplateSpec {
|
|
56
|
+
system?: string;
|
|
57
|
+
afterSystem?: string;
|
|
58
|
+
stop?: Array<string>;
|
|
59
|
+
assistant?: string;
|
|
60
|
+
}
|
|
61
|
+
interface TaskVariableDef {
|
|
62
|
+
type: string | Array<string>;
|
|
63
|
+
description: string;
|
|
64
|
+
}
|
|
65
|
+
interface TaskDef {
|
|
66
|
+
name: string;
|
|
67
|
+
description: string;
|
|
68
|
+
prompt: string;
|
|
69
|
+
template?: TemplateSpec;
|
|
70
|
+
inferParams?: InferenceParams;
|
|
71
|
+
model: ModelSpec;
|
|
72
|
+
models?: Record<string, ModelSpec>;
|
|
73
|
+
shots?: Array<HistoryTurn>;
|
|
74
|
+
variables?: {
|
|
75
|
+
required?: Record<string, TaskVariableDef>;
|
|
76
|
+
optional?: Record<string, TaskVariableDef>;
|
|
77
|
+
};
|
|
78
|
+
tools?: Array<ToolSpec>;
|
|
79
|
+
toolsList?: Array<string>;
|
|
80
|
+
}
|
|
81
|
+
interface TaskOutput {
|
|
82
|
+
answer: InferenceResult;
|
|
83
|
+
errors: Record<string, string>;
|
|
84
|
+
template?: PromptTemplate;
|
|
85
|
+
}
|
|
86
|
+
export { ModelSpec, TaskInput, TaskDef, TemplateSpec, TaskConf, TaskOutput, TaskVariableDef, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
package/dist/task.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ToolSpec } from '@locallm/types';
|
|
2
|
+
import { Agent } from "../../agent/dist/agent.js";
|
|
3
|
+
import { TaskConf, TaskDef, TaskInput, TaskOutput } from "./interfaces.js";
|
|
4
|
+
declare class Task {
|
|
5
|
+
def: TaskDef;
|
|
6
|
+
agent: Agent;
|
|
7
|
+
constructor(agent: Agent, def: TaskDef);
|
|
8
|
+
static fromYaml(agent: Agent, txt: string): Task;
|
|
9
|
+
run(params: TaskInput, conf?: TaskConf): Promise<TaskOutput>;
|
|
10
|
+
addTools(tools: Array<ToolSpec>): Task;
|
|
11
|
+
}
|
|
12
|
+
export { Task };
|
package/dist/task.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { PromptTemplate } from 'modprompt';
|
|
2
|
+
import YAML from 'yaml';
|
|
3
|
+
import { formatInferParams } from './inferparams.js';
|
|
4
|
+
import { formatTaskTemplate } from './templates.js';
|
|
5
|
+
import { applyVariables } from './variables.js';
|
|
6
|
+
class Task {
|
|
7
|
+
def;
|
|
8
|
+
agent;
|
|
9
|
+
constructor(agent, def) {
|
|
10
|
+
this.agent = agent;
|
|
11
|
+
this.def = def;
|
|
12
|
+
}
|
|
13
|
+
static fromYaml(agent, txt) {
|
|
14
|
+
const data = YAML.parse(txt);
|
|
15
|
+
return new Task(agent, data);
|
|
16
|
+
}
|
|
17
|
+
async run(params, conf) {
|
|
18
|
+
//console.log("P", params);
|
|
19
|
+
//console.log("TOOLS", this.agent.tools);
|
|
20
|
+
if (!params?.prompt) {
|
|
21
|
+
throw new Error("Please provide a prompt parameter");
|
|
22
|
+
}
|
|
23
|
+
let model = this.def.model;
|
|
24
|
+
const useTemplates = this.agent.lm.providerType !== "openai";
|
|
25
|
+
if (conf) {
|
|
26
|
+
if (conf?.model) {
|
|
27
|
+
model = conf.model;
|
|
28
|
+
}
|
|
29
|
+
if (conf?.modelname) {
|
|
30
|
+
let found = false;
|
|
31
|
+
if (this.def?.models) {
|
|
32
|
+
for (const [modelName, _mod] of Object.entries(this.def.models)) {
|
|
33
|
+
if (modelName == conf.modelname) {
|
|
34
|
+
found = true;
|
|
35
|
+
const m = _mod;
|
|
36
|
+
model = m;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!found) {
|
|
42
|
+
throw new Error(`No model found for ${conf.modelname}. Available models:\n${params?.models}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (this.agent.lm.providerType == "ollama") {
|
|
47
|
+
await this.agent.lm.loadModel(model.name, model.ctx);
|
|
48
|
+
}
|
|
49
|
+
this.def = applyVariables(this.def, params);
|
|
50
|
+
let tpl = new PromptTemplate("none");
|
|
51
|
+
let finalPrompt;
|
|
52
|
+
const options = {};
|
|
53
|
+
const agentToolsList = Object.values(this.agent.tools);
|
|
54
|
+
if (useTemplates) {
|
|
55
|
+
tpl = formatTaskTemplate(this.def, model?.template ? model.template : undefined);
|
|
56
|
+
this.def.inferParams = formatInferParams(this.def.inferParams ?? {}, conf ?? {}, tpl);
|
|
57
|
+
tpl.replacePrompt(this.def.prompt);
|
|
58
|
+
if (agentToolsList.length > 0) {
|
|
59
|
+
if (!tpl?.toolsDef) {
|
|
60
|
+
throw new Error(`The template ${tpl.name} does not have tools and the task ${this.def.name} specifies some`);
|
|
61
|
+
}
|
|
62
|
+
agentToolsList.forEach((t) => tpl.addTool(t));
|
|
63
|
+
}
|
|
64
|
+
;
|
|
65
|
+
finalPrompt = tpl.prompt(params.prompt);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.def.inferParams = formatInferParams(this.def.inferParams ?? {}, conf ?? {});
|
|
69
|
+
finalPrompt = this.def.prompt.replace("{prompt}", params.prompt);
|
|
70
|
+
}
|
|
71
|
+
if (agentToolsList.length > 0) {
|
|
72
|
+
options.tools = agentToolsList;
|
|
73
|
+
}
|
|
74
|
+
if (conf?.debug) {
|
|
75
|
+
console.log("-----------", model.name, "/", model.template, "-----------");
|
|
76
|
+
console.log(finalPrompt);
|
|
77
|
+
console.log("----------------------------------------------");
|
|
78
|
+
console.log("Infer params:", this.def.inferParams);
|
|
79
|
+
console.log("----------------------------------------------");
|
|
80
|
+
}
|
|
81
|
+
if (!this.def.inferParams?.extra) {
|
|
82
|
+
this.def.inferParams["extra"] = {};
|
|
83
|
+
}
|
|
84
|
+
if (this.agent.lm.providerType == "ollama") {
|
|
85
|
+
// tell Ollama to apply no template
|
|
86
|
+
this.def.inferParams["extra"]["raw"] = true;
|
|
87
|
+
}
|
|
88
|
+
let answer;
|
|
89
|
+
if (!useTemplates) {
|
|
90
|
+
if (this.def.template?.system) {
|
|
91
|
+
options.system = this.def.template.system;
|
|
92
|
+
}
|
|
93
|
+
if (this.def?.shots) {
|
|
94
|
+
options.history = this.def.shots;
|
|
95
|
+
}
|
|
96
|
+
answer = await this.agent.run(finalPrompt, this.def.inferParams, options);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
answer = await this.agent.run(finalPrompt, this.def.inferParams, options, tpl);
|
|
100
|
+
}
|
|
101
|
+
return { answer: answer, errors: {}, template: useTemplates ? tpl : undefined };
|
|
102
|
+
}
|
|
103
|
+
addTools(tools) {
|
|
104
|
+
tools.forEach(t => this.agent.tools[t.name] = t);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export { Task };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { PromptTemplate } from "modprompt";
|
|
2
|
+
import { useTemplateForModel } from "@agent-smith/tfm";
|
|
3
|
+
const tfm = useTemplateForModel();
|
|
4
|
+
function formatTaskTemplate(taskDef, templateName) {
|
|
5
|
+
if ((!taskDef.model?.template)) {
|
|
6
|
+
const gt = tfm.guess(taskDef.model.name);
|
|
7
|
+
if (gt == "none") {
|
|
8
|
+
throw new Error(`Unable to guess the template for ${taskDef.model}: please provide a template in the taskDef definition`);
|
|
9
|
+
}
|
|
10
|
+
taskDef.model.template = gt;
|
|
11
|
+
}
|
|
12
|
+
const tpl = new PromptTemplate(templateName ?? taskDef.model.template);
|
|
13
|
+
if (taskDef?.template) {
|
|
14
|
+
if (taskDef.template?.system) {
|
|
15
|
+
tpl.replaceSystem(taskDef.template.system);
|
|
16
|
+
}
|
|
17
|
+
if (taskDef.template?.afterSystem) {
|
|
18
|
+
tpl.afterSystem(taskDef.template.afterSystem);
|
|
19
|
+
}
|
|
20
|
+
if (taskDef.template?.assistant) {
|
|
21
|
+
tpl.afterAssistant(" " + taskDef.template.assistant);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// model overrides
|
|
25
|
+
if (taskDef.model?.system) {
|
|
26
|
+
tpl.replaceSystem(taskDef.model.system);
|
|
27
|
+
}
|
|
28
|
+
if (taskDef.model?.afterSystem) {
|
|
29
|
+
tpl.afterSystem(taskDef.model.afterSystem);
|
|
30
|
+
}
|
|
31
|
+
if (taskDef.model?.assistant) {
|
|
32
|
+
tpl.afterAssistant(taskDef.model.assistant);
|
|
33
|
+
}
|
|
34
|
+
// shots
|
|
35
|
+
if (taskDef?.shots) {
|
|
36
|
+
taskDef.shots.forEach((s) => {
|
|
37
|
+
//console.log("** SHOT", s);
|
|
38
|
+
tpl.addShot(s);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return tpl;
|
|
42
|
+
}
|
|
43
|
+
export { formatTaskTemplate, };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
function applyVariables(taskDef, taskInput) {
|
|
2
|
+
// check taskDef variables
|
|
3
|
+
if (taskDef?.variables) {
|
|
4
|
+
if (taskDef.variables?.required) {
|
|
5
|
+
for (const name of Object.keys(taskDef.variables.required)) {
|
|
6
|
+
if (!(name in taskInput)) {
|
|
7
|
+
throw new Error(`The variable ${name} is required to run this taskDef`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
if (taskDef.variables?.optional) {
|
|
12
|
+
for (const name of Object.keys(taskDef.variables.optional)) {
|
|
13
|
+
if (!(name in taskInput)) {
|
|
14
|
+
taskDef.prompt = taskDef.prompt.replaceAll(`{${name}}`, "");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
// apply variables
|
|
20
|
+
for (const [k, v] of Object.entries(taskInput)) {
|
|
21
|
+
taskDef.prompt = taskDef.prompt.replaceAll(`{${k}}`, v);
|
|
22
|
+
}
|
|
23
|
+
return taskDef;
|
|
24
|
+
}
|
|
25
|
+
export { applyVariables, };
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agent-smith/task",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "A toolkit to create human friendly agents: the language model tasks module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/synw/agent-smith.git"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"buildrl": "rm -rf dist/* && rollup -c",
|
|
11
|
+
"build": "rm -rf dist/* && tsc",
|
|
12
|
+
"watch": "tsc -w",
|
|
13
|
+
"test": "jest --coverage",
|
|
14
|
+
"docs": "typedoc --entryPointStrategy expand"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@agent-smith/agent": "^0.0.2",
|
|
18
|
+
"@agent-smith/tfm": "^0.1.2",
|
|
19
|
+
"@locallm/api": "0.4.2",
|
|
20
|
+
"modprompt": "^0.12.0",
|
|
21
|
+
"yaml": "^2.8.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@locallm/types": "^0.4.2",
|
|
25
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
26
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
27
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
28
|
+
"@types/node": "^24.4.0",
|
|
29
|
+
"markdown-it-replace-link": "^1.2.2",
|
|
30
|
+
"restmix": "^0.5.0",
|
|
31
|
+
"rollup": "^4.50.1",
|
|
32
|
+
"tslib": "^2.8.1",
|
|
33
|
+
"typedoc": "^0.28.12",
|
|
34
|
+
"typedoc-plugin-markdown": "^4.8.1",
|
|
35
|
+
"typedoc-plugin-rename-defaults": "^0.7.3",
|
|
36
|
+
"typescript": "^5.9.2"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"module": "./dist/main.js",
|
|
42
|
+
"types": "./dist/main.d.ts",
|
|
43
|
+
"type": "module",
|
|
44
|
+
"exports": {
|
|
45
|
+
".": {
|
|
46
|
+
"import": "./dist/main.js"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"registry": "https://registry.npmjs.org/"
|
|
52
|
+
},
|
|
53
|
+
"license": "MIT"
|
|
54
|
+
}
|