|
4 | 4 | [](https://www.npmjs.com/package/ts-llmt) |
5 | 5 |
|
6 | 6 | This repository holds experimental code aimed at providing native TypeScript |
7 | | -support for Large Language Model Templates. The key idea being explored is that |
8 | | -templates have a type that corresponds to the names of the variables within the |
9 | | -template. |
| 7 | +support for Large Language Model Templates. |
10 | 8 |
|
11 | | -For example: |
| 9 | +The key idea is to track "named-holes" in templates at type-checking (typing) |
| 10 | +time. This means: |
| 11 | + |
| 12 | +- You never need to debug an accidental substitution for the wrong variable name. |
| 13 | +- You can substitute for some variables but not others, in any order you like, and the remaining |
| 14 | + variables are tracked in the type. |
| 15 | +- You can substitute templates with more holes into a named-hole in a template, and you get the |
| 16 | + correct remaining holes in the right places in the final template. |
| 17 | +- The same hole can appear in multiple places, substitution substitutes it everywhere, as you would |
| 18 | + expect. |
| 19 | +- There is support for few-shot prompts (where you have some iterated sub-template over a data |
| 20 | + structure). |
| 21 | + |
| 22 | +Here's a mini-example: |
12 | 23 |
|
13 | 24 | ```ts |
14 | 25 | import { nv, template } from 'ts-llmt'; |
15 | 26 |
|
16 | 27 | const thingVar = nv('thing'); |
17 | 28 | const thing2Var = nv('thing2'); |
18 | | -// The type of `whatIsAtoB` is inferred to be `Template<"thing" | "thing2">` |
| 29 | +// *NOTE*: type of `whatIsAtoB` is magically inferred to be: |
| 30 | +// `Template<"thing" | "thing2">` |
19 | 31 | const whatIsAtoB = template`what is a ${thingVar} to a ${thing2Var}?`; |
20 | 32 |
|
21 | | -// Replacing the thing variable give type: `Template<"thing2">` |
| 33 | +// Replacing the `thing` variable in whatIsAtoB, gives the type: |
| 34 | +// `Template<"thing2">` |
22 | 35 | const whatIsTabletoB = whatIsAtoB.vars.thing.substStr('table'); |
23 | 36 |
|
24 | 37 | // The escaped raw form of this template is as so: |
25 | 38 | expect(whatIsTabletoB.escaped).toEqual('what is a table to a {{thing2}}?'); |
26 | 39 | ``` |
27 | 40 |
|
28 | | -A nice feature of this is that you get as "as-you-type" error checking, and |
29 | | -arguments can be auto-completed by the IDE. e.g. you can directly reference the |
30 | | -variables from the 'vars' parameter of a template, and anything else is an |
31 | | -as-you-type error in your editor. |
| 41 | +The key nice feature of this is that you get _as "as-you-type" error checking_, |
| 42 | +and _arguments can be auto-completed by the IDE_. You can never substitute for a |
| 43 | +variable that is not there. |
32 | 44 |
|
33 | | -You can do multi-variable replacement nicely, and still have all the wonderful |
34 | | -type-checking like so: |
| 45 | +You can do multi-variable replacement nicely too, and still have all the |
| 46 | +wonderful type-checking like so: |
35 | 47 |
|
36 | 48 | ```ts |
37 | 49 | whatIsAtoB.substs({ thing: 'table', thing2: 'chair' }); |
|
0 commit comments