llm-exe 2.2.0 → 2.3.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/dist/index.d.mts +130 -50
- package/dist/index.d.ts +130 -50
- package/dist/index.js +525 -240
- package/dist/index.mjs +521 -240
- package/package.json +6 -5
- package/readme.md +122 -52
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "llm-exe",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Simplify building LLM-powered apps with easy-to-use base components, supporting text and chat-based prompts with handlebars template engine, output parsers, and flexible function calling capabilities.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"typecheck": "tsc --noEmit",
|
|
39
39
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --config jest.config.ts --detectOpenHandles --coverage --forceExit",
|
|
40
40
|
"test-examples": "eval $(cat .env) NODE_OPTIONS=--experimental-vm-modules jest --config jest.config.examples.ts --detectOpenHandles --coverage --forceExit",
|
|
41
|
-
"test-one": "eval $(cat .env) NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --coverage --forceExit --config jest.config.examples.ts examples/
|
|
41
|
+
"test-one": "eval $(cat .env) NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --coverage --forceExit --config jest.config.examples.ts examples/intentBot.test.ts",
|
|
42
|
+
"run-one": "eval $(cat .env) ts-node ./examples/chains/self-refinement/usage.ts",
|
|
42
43
|
"build": "(concurrently \"tsc --p ./tsconfig.json -w\" \"tsc-alias -p tsconfig.json -w\")",
|
|
43
44
|
"build:ci": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
44
45
|
"build:browser": "tsup src/script.ts --format iife --platform browser --target es6 --sourcemap --clean --external '**'",
|
|
@@ -46,8 +47,8 @@
|
|
|
46
47
|
"build:docs-examples": "./node_modules/.bin/esbuild examples/prompt/index.ts examples/state/index.ts --bundle --outdir=docs/.vitepress/components/examples --platform=node --target=es6 --format=esm",
|
|
47
48
|
"build:watch:docs-examples": "./node_modules/.bin/esbuild examples/prompt/index.ts examples/state/index.ts --watch --bundle --outdir=docs/.vitepress/components/examples --platform=node --target=es6 --format=esm",
|
|
48
49
|
"predocs:build": "npm run build:docs-examples",
|
|
49
|
-
"docs:dev": "(concurrently \"vitepress dev docs\" \"npm run build:watch:docs-examples\"
|
|
50
|
-
"docs:build": "vitepress build docs",
|
|
50
|
+
"docs:dev": "eval $(cat docs/.env) && concurrently \"vitepress dev docs\" \"npm run build:watch:docs-examples\"",
|
|
51
|
+
"docs:build": "eval $(cat docs/.env) && vitepress build docs",
|
|
51
52
|
"lint": "eslint .",
|
|
52
53
|
"format:check": "prettier --check \"src\"",
|
|
53
54
|
"format:write": "prettier --write \"src\"",
|
|
@@ -88,7 +89,7 @@
|
|
|
88
89
|
"tsconfig-paths": "4.2.0",
|
|
89
90
|
"tsup": "^8.4.0",
|
|
90
91
|
"typescript": "5.8.3",
|
|
91
|
-
"vitepress": "^1.3
|
|
92
|
+
"vitepress": "^1.6.3"
|
|
92
93
|
},
|
|
93
94
|
"repository": {
|
|
94
95
|
"type": "git",
|
package/readme.md
CHANGED
|
@@ -6,81 +6,151 @@ A package that provides simplified base components to make building and maintain
|
|
|
6
6
|
|
|
7
7
|
- Write functions powered by LLM's with easy to use building blocks.
|
|
8
8
|
- Pure Javascript and Typescript. Allows you to pass and infer types.
|
|
9
|
+
- Supercharge your prompts by using handlebars within prompt template.
|
|
9
10
|
- Support for text-based (llama-3) and chat-based prompts. (gpt-4o, claude-3.5, grok-3, Gemini, Bedrock, Ollama, etc)
|
|
10
11
|
- Call LLM's from different providers without changing your code. (OpenAi/Anthropic/xAI/Google/AWS Bedrock/Ollama/Deepseek)
|
|
11
|
-
- Supercharge your prompts by using handlebars within prompt template.
|
|
12
12
|
- Allow LLM's to call functions (or call other LLM executors).
|
|
13
13
|
- Not very opinionated. You have control on how you use it.
|
|
14
14
|
|
|
15
|
-

|
|
17
16
|
|
|
18
17
|
See full docs here: [https://llm-exe.com](https://llm-exe.com)
|
|
19
18
|
|
|
20
|
-
|
|
21
19
|
---
|
|
20
|
+
|
|
22
21
|
# Install
|
|
23
22
|
|
|
24
23
|
Install llm-exe using npm.
|
|
24
|
+
|
|
25
25
|
```
|
|
26
26
|
npm i llm-exe
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
ESM-first. CommonJS works too.
|
|
30
|
+
|
|
29
31
|
```typescript
|
|
32
|
+
// ESM
|
|
30
33
|
import * as llmExe from "llm-exe";
|
|
34
|
+
// or specific modules
|
|
35
|
+
import { useLlm, createChatPrompt, createParser } from "llm-exe";
|
|
36
|
+
|
|
37
|
+
// CommonJS
|
|
38
|
+
const llmExe = require("llm-exe");
|
|
39
|
+
```
|
|
31
40
|
|
|
32
|
-
|
|
41
|
+
## Overview
|
|
33
42
|
|
|
34
|
-
|
|
43
|
+
```ts
|
|
44
|
+
// Prompt
|
|
45
|
+
const prompt = createChatPrompt("You are a support agent. Help the user.");
|
|
46
|
+
prompt.addUserMessage("I need help with my order.");
|
|
47
|
+
|
|
48
|
+
// LLM
|
|
49
|
+
const llm = useLlm("openai.gpt-4o");
|
|
50
|
+
|
|
51
|
+
// Parser
|
|
52
|
+
const parser = createParser("json", { schema: mySchema });
|
|
53
|
+
|
|
54
|
+
// Executor
|
|
55
|
+
const executor = createLlmExecutor({ llm, prompt, parser });
|
|
56
|
+
await executor.execute({ input: "..." });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### Prompt Helpers
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
const prompt = createChatPrompt(`
|
|
63
|
+
{{#if user.isFirstTime}}
|
|
64
|
+
Welcome!
|
|
65
|
+
{{else}}
|
|
66
|
+
Welcome back!
|
|
67
|
+
{{/if}}
|
|
68
|
+
`);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### Built-In Parsers
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
createParser("stringExtract", { enum: ["yes", "no"] });
|
|
75
|
+
createParser("listToJson");
|
|
76
|
+
createParser("listToArray");
|
|
77
|
+
createParser("markdownCodeBlock");
|
|
78
|
+
// ...etc
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Custom Parsers
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const parser = createCustomParser("MyUppercaseParser", (output, input) => {
|
|
85
|
+
return output.toUpperCase();
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### State
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
const dialogue = createDialogue("chat");
|
|
93
|
+
dialogue.setUserMessage("Hi");
|
|
94
|
+
dialogue.setAssistantMessage("Hello!");
|
|
95
|
+
dialogue.getHistory(); // returns chat array
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### Hooks
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
executor.on("onSuccess", console.log);
|
|
102
|
+
executor.on("onError", console.error);
|
|
35
103
|
```
|
|
36
104
|
|
|
37
105
|
## Basic Example
|
|
106
|
+
|
|
38
107
|
Below is simple example:
|
|
108
|
+
|
|
39
109
|
```typescript
|
|
40
|
-
|
|
110
|
+
// 1. Use the model you want
|
|
111
|
+
const llm = useLlm("openai.gpt-4o");
|
|
112
|
+
|
|
113
|
+
// 2. Create a parameterized prompt
|
|
114
|
+
const instruction = `
|
|
115
|
+
You are a classifier. Given a user message, reply with the category it belongs to.
|
|
116
|
+
Pick from only the following options:
|
|
117
|
+
|
|
118
|
+
{{#each options}}- {{this}}
|
|
119
|
+
{{/each}}
|
|
120
|
+
|
|
121
|
+
Respond with only one of the options.`;
|
|
122
|
+
|
|
123
|
+
const prompt = createChatPrompt<{ options: string[]; input: string }>(
|
|
124
|
+
instruction
|
|
125
|
+
).addUserMessage("{{input}}"); // placeholder for message content
|
|
126
|
+
|
|
127
|
+
// 3. Create a parser that ensures a clean match
|
|
128
|
+
const parser = createParser("stringExtract", {
|
|
129
|
+
enum: ["billing", "support", "cancel", "unknown"],
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// 4. Create the executor
|
|
133
|
+
const classifyMessage = createLlmExecutor({
|
|
134
|
+
llm,
|
|
135
|
+
prompt,
|
|
136
|
+
parser,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// 5. Pass in options and a message — like a real function!
|
|
140
|
+
// classifyMessage.execute is typed based on the prompt/parser!
|
|
141
|
+
const result = await classifyMessage.execute({
|
|
142
|
+
input: "Hi, I'm moving and no longer need this service.",
|
|
143
|
+
options: ["billing", "support", "cancel", "unknown"],
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
console.log(result); // => "cancel"
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Further Reading
|
|
150
|
+
|
|
151
|
+
[Find llm-exe on Medium](https://medium.com/llm-exe)
|
|
41
152
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const llm = llmExe.useLlm("openai.gpt-4o-mini");
|
|
47
|
-
|
|
48
|
-
const instruction = `You are not an assistant, I need you to reply with only
|
|
49
|
-
'yes' or 'no' as an answer to the question below. Do not explain yourself
|
|
50
|
-
or ask questions. Answer with only yes or no.`;
|
|
51
|
-
|
|
52
|
-
const prompt = llmExe
|
|
53
|
-
.createChatPrompt(instruction)
|
|
54
|
-
.addUserMessage(input)
|
|
55
|
-
.addUserMessage(`yes or no:`);
|
|
56
|
-
|
|
57
|
-
const parser = llmExe.createParser("stringExtract", { enum: ["yes", "no"] });
|
|
58
|
-
return llmExe.createLlmExecutor({ llm, prompt, parser }).execute({ input });
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const isTheSkyBlue = await YesOrNoBot(`Is AI cool?`)
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
*
|
|
65
|
-
* The prompt sent to the LLM would be:
|
|
66
|
-
* (line breaks added for readability)
|
|
67
|
-
*
|
|
68
|
-
* [{
|
|
69
|
-
* role: 'system',
|
|
70
|
-
* content: 'You are not an assistant, I need you to reply with only
|
|
71
|
-
'yes' or 'no' as an answer to the question asked by the user. Do not explain yourself
|
|
72
|
-
or ask questions. Answer only with yes or no.'
|
|
73
|
-
* },
|
|
74
|
-
* {
|
|
75
|
-
* role: 'user',
|
|
76
|
-
* content: 'Is AI cool?'
|
|
77
|
-
* }]
|
|
78
|
-
*
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
*
|
|
83
|
-
* console.log(isTheSkyBlue)
|
|
84
|
-
* yes
|
|
85
|
-
* /
|
|
86
|
-
```
|
|
153
|
+
- [Prompt: Create Typed, Modular Prompt Templates in TypeScript](https://medium.com/llm-exe/llm-exe-intro-prompt-3d9d40dc923d)
|
|
154
|
+
- [LLM: Keep Your Code Clean While Switching Models](https://medium.com/llm-exe/llm-exe-intro-llm-2f5f35e60caf)
|
|
155
|
+
- [Parser: Parse, Validate, and Structure AI Responses](https://medium.com/llm-exe/llm-exe-intro-parser-aed787f81082)
|
|
156
|
+
- [Executor: Prompt, Parse, and Execute with Type Safety](https://medium.com/llm-exe/llm-exe-intro-llm-executor-52bb95c76c84)
|